azurre / php-simple-logger
简单的PSR-3日志记录器
1.1.0
2021-06-23 18:51 UTC
Requires
- php: >=5.6.0
- psr/log: ^1.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-24 00:14:17 UTC
README
Simple Logger是一个简单易用的PHP PSR-3日志记录器。
特性
- 力量与简单性
- PSR-3日志记录器接口
- 支持多个处理器
- 支持多个日志级别严重性
- 日志通道
- 进程ID记录
- 自定义日志消息
- 自定义上下文数据
- 异常记录
设置
$ composer require azurre/php-simple-logger
使用
20秒快速入门教程
use \Azurre\Component\Logger; use \Azurre\Component\Logger\Handler\File; $logger = new Logger(); $logger->info('Simple Logger really is simple.');
就是这样!您的应用程序已经开始记录日志!日志文件将命名为"default.log"。
扩展示例
use \Azurre\Component\Logger; use \Azurre\Component\Logger\Handler\File; $logfile = '/var/log/events.log'; $channel = 'billing'; $logger = new Logger($channel); $logger->setHandler(new File($logfile)); $logger->info('Begin process that usually fails.', ['process' => 'invoicing', 'user' => $user]); try { invoiceUser($user); // This usually fails } catch (\Exception $e) { $logger->error('Billing failure.', ['process' => 'invoicing', 'user' => $user, 'exception' => $e]); }
日志输出
2017-02-13 00:35:55.426630 [info] [billing] [pid:17415] Begin process that usually fails. {"process":"invoicing","user":"bob"} {}
2017-02-13 00:35:55.430071 [error] [billing] [pid:17415] Billing failure. {"process":"invoicing","user":"bob"} {"message":"Could not process invoice.","code":0,"file":"/path/to/app.php","line":20,"trace":[{"file":"/path/to/app.php","line":13,"function":"invoiceUser","args":["mark"]}]}
日志输出
日志行具有以下格式
YYYY-mm-dd HH:ii:ss.uuuuuu [loglevel] [channel] [pid:##] Log message content {"Optional":"JSON Contextual Support Data"} {"Optional":"Exception Data"}
日志行易于阅读和理解。日志行始终位于一行上。字段由制表符分隔。
日志级别
'Simple Logger基于PSR Log Levels有八个日志级别严重性。
$logger->debug('Detailed information about the application run.'); $logger->info('Informational messages about the application run.'); $logger->notice('Normal but significant events.'); $logger->warning('Information that something potentially bad has occured.'); $logger->error('Runtime error that should be monitored.'); $logger->critical('A service is unavailable or unresponsive.'); $logger->alert('The entire site is down.'); $logger->emergency('The Web site is on fire.');
默认情况下,所有日志级别都会被记录。最低日志级别可以通过以下两种方式更改:
- 可选构造函数参数
- 在任何时候都可以通过设置器方法更改
use \Psr\Log\LogLevel; use \Azurre\Component\Logger; // Optional constructor Parameter (Only error and above are logged [error, critical, alert, emergency]) $logger = new Logger($channel, LogLevel::ERROR); // Setter method (Only warning and above are logged) $logger->setLogLevel(LogLevel::WARNING);
上下文数据
'Simple Logger通过支持上下文数据,使日志记录最佳实践成为通用日志消息,以提供对消息的上下文。
日志消息的第二个参数是一个键值对关联数组,该数组以JSON字符串的形式记录,作为日志消息的上下文支持数据。
// Add context to a Web request. $log->info('Web request initiated', ['method' => 'GET', 'endpoint' => 'user/account', 'queryParameters' => 'id=1234']); // Add context to a disk space warning. $log->warning('Free space is below safe threshold.', ['volume' => '/var/log', 'availablePercent' => 4]);
记录异常
异常通过使用键exception和值异常变量进行记录。
catch (\Exception $e) { $logger->error('Something exceptional has happened', ['exception' => $e]); }
日志通道
将通道视为日志行的命名空间。如果您想要多个记录器或应用程序将日志记录到单个日志文件中,通道就是您的朋友。
通道可以通过以下两种方式设置
- 构造函数参数
- 在任何时候都可以通过设置器方法更改
// Constructor Parameter $channel = 'router'; $logger = new Logger($channel); // Setter method $logger->setChannel('database');
单元测试
$ cd tests
$ phpunit