gelembjuk/logger

允许为不同的类添加日志功能,帮助处理错误、记录错误和显示错误屏幕。可以捕获警告和致命错误

1.1.1 2023-11-29 10:49 UTC

This package is not auto-updated.

Last update: 2024-10-02 18:53:17 UTC


README

PHP 包,用于轻松记录和捕获错误。基于 Psr/Log 的 FileLogger 类有助于筛选要记录的内容。 ErrorScreen 类有助于捕获 PHP 警告和致命错误,并向用户显示正确的错误屏幕。《ApplicationLogger》是一个特性,可以非常容易地将记录器包含在不同的类中。

安装

使用 composer: gelembjuk/logger require: {"gelembjuk/logger": "*"}

配置

配置是在运行时通过构造函数选项(作为哈希参数)完成的

配置 FileLogger

logfile 日志文件的路径(在哪里写入日志) groupfilter 要记录的事件组的列表。 all 表示记录一切。组由 | 符号分隔

$logger1 = new Gelembjuk\Logger\FileLogger(
	array(
		'logfile' => $logfile,  // path to your log file (where to write logs)
		'groupfilter' => 'group1|group2|group3'  // list of groups of events to log. `all` means log everything
	));

配置 ErrorScreen

catchwarnings - (true|false) 。如果为 true,则将用户错误处理程序设置为捕获警告

catchfatals - (true|false) 。如果为 true,则捕获致命错误。用于记录错误并显示 normal 错误屏幕

catchexceptions - (true|false) 。如果为 true,则未捕获的异常将被对象捕获。用于捕获在任何 try {} catch 块中遗漏的异常

showwarningmessage - (true|false) 。如果为 true,则在发生警告时显示错误屏幕。如果为 false,则仅记录错误

showfatalmessage - (true|false) 。显示致命错误的错误屏幕。如果为 false,则仅记录错误。在这种情况下,用户将看到 standard 致命错误

viewformat - 为 viewformat 变量设置值。可能的值:html、json、xml、http。默认值为 html

showtrace - (true|false)。开关,用于确定是否在错误屏幕中向用户显示错误跟踪

commonerrormessage - 字符串 当发生错误时向用户显示的通用错误消息

logger - FileLogger 类的对象

loggeroptions - 创建新 FileLogger 对象的选项

$errors = new Gelembjuk\Logger\ErrorScreen(
		array(
			'logger' => $logger1 /*create before*/,
			'viewformat' => 'html',
			'catchwarnings' => true,
			'catchfatals' => true,
			'showfatalmessage' => true,
			'commonerrormessage' => 'Sorry, somethign went wrong. We will solve ASAP'
		)
	);

用法

FileLogger

require '../vendor/autoload.php';

$logger1 = new Gelembjuk\Logger\FileLogger(
	array(
		'logfile' => '/tmp/log.txt',
		'groupfilter' => 'all' // log everything this time
	));

// do test log write
$logger1->debug('Test log',array('group' => 'test'));

$logger1->setGroupFilter('group1|group2'); // after this only group1 and group2 events are logged

$logger1->debug('This message will not be in logs as `test` is out of filter',array('group' => 'test'));

ApplicationLogger 特性

require '../vendor/autoload.php';

class A {
}

class B extends A {
	// include the trait to have logging functionality in this class
	use Gelembjuk\Logger\ApplicationLogger;
	
	public function __construct($logger) {
		$this->setLogger($logger);

		$this->logQ('B object create','construct|B');
	}

	public function doSomething() {
		$this->logQ('doSomething() in B','B');
	}
}

class C {
	use Gelembjuk\Logger\ApplicationLogger;
	
	public function __construct($logger) {
		$this->setLogger($logger);

		$this->logQ('C object create','construct|C');
	}

	public function doOtherThing() {
		$this->logQ('oOtherThing() in C','C');
	}
}

$b = new B($logger1); // $logger1 is instance of FileLogger
$c = new C($logger1);

$b->doSomething();
$c->doOtherThing();

ErrorScreen

require '../vendor/autoload.php';

$errors = new Gelembjuk\Logger\ErrorScreen(
		array(
			'logger' => $logger1 /*created before*/,
			'viewformat' => 'html',
			'catchwarnings' => true,
			'catchfatals' => true,
			'showfatalmessage' => true,
			'commonerrormessage' => 'Sorry, somethign went wrong. We will solve ASAP'
		)
	);

// to catch exceptions on the top level of the app
try {
	// do something 
	
} catch (Exception $e) {
	$errors->processError($e);
}

// presume there was no exception
// now catch warning

// warning is raised and catched in errors object
// error message displayed to a user
include('not_existent_file.php'); 	

作者

Roman Gelembjuk (@gelembjuk)