stefna / log
为 stefna.is 实现的 Psr-3 日志库
2.0.2
2023-12-20 12:56 UTC
Requires
- php: ^8.1
- psr/log: ^3.0
- psr/simple-cache: ^1.0
Requires (Dev)
- bref/bref: ^0.5 || ^1.0
- bugsnag/bugsnag: ^3.15
- monolog/monolog: ^3.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9
- polus/adr: 3.2.0
- polus/router: ^1.0.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- stefna/codestyle: ^1.12
- symfony/console: ^5.4 || ^4.0
This package is auto-updated.
Last update: 2024-09-20 14:50:40 UTC
README
Stefna\Logger\ManagerInterface
处理日志类创建的类。
在这里配置你的主要日志记录器。
方法
pushProcessor(callable $callback, string $channel = null): Manager
如果设置了通道,则仅对该通道应用处理。如果没有通道实例存在,则静默忽略该过程
Stefna\Logger\MonologManager
使用 Monolog 作为主要日志记录器的管理器。
管理器上有一个额外的 pushHandler
方法,这样你可以为选定通道配置自定义处理程序
pushHandler(HandlerInterface $handler, $channel = null): Manager
如果设置了通道,则处理程序仅应用于该通道。如果没有通道实例存在,则静默忽略该过程
Stefna\Logger\Logger Methods
setManager
设置日志管理器,如果尚未设置,则所有对 getLogger
的调用将返回 NullLogger
getManager
获取管理器以将过程或处理程序添加到主要日志记录器
setChannelConfig
添加特定通道的配置
setGlobalConfig
通过一次调用添加多个配置选项
getLogger
检索 channel
的日志实例
如果不存在,该方法将创建日志记录器,并检查指定 channel
的配置
示例
<?php declare(strict_types=1); use Stefna\Logger\Filters\MinLogLevelFilter; use Stefna\Logger\Filters\CallbackFilter; use Stefna\Logger\Filters\TimeLimitFilter; $monolog = new \Monolog\Logger('main-channel', $handlers, $proccess); $manager = new \Stefna\Logger\MonologManager($monolog, new \Stefna\Logger\Filters\FilterFactory()); \Stefna\Logger\Logger::setManager($manager); $filters = [ [MinLogLevelFilter::KEY, ['level' => \Psr\Log\LogLevel::ALERT]], [ CallbackFilter::KEY, [ 'callback' => function(string $level, string $message, array $context) { return isset($context['exception']); }, ], ], [TimeLimitFilter::KEY, ['cache' => $simpleCache, 'interval' => new DateInterval('P1D')]] ]; \Stefna\Logger\Logger::setChannelConfig( 'test-channel', new Stefna\Logger\Config\Config('test-channel', $filters[[, $proccess], $handlers]) ); $logger = \Stefna\Logger\Logger::getLogger('test-channel');
崩溃日志设置
<?php declare(strict_types=1); $logger = new SimpleFileLogger('path/to/save/crash.log'); //or $logger = new SystemErrorLogger(); $crashLogger = new BufferFilterLogger( $logger, new ActivateLevelFilter(LogLevel::ERROR) ); // Will not add to log file $crashLogger->debug('test'); // Will add all message prior and after this to the log // This is so that we get a complete story of what happened during the execution $crashLogger->error('error');
设置去抖动过滤器
此过滤器旨在防止记录相同错误的日志
设置
<?php declare(strict_types=1); use Stefna\Logger\Filters\DebounceFilter; use Stefna\Logger\Logger\FilterLogger $debounceFilter = new DebounceFilter(function($level, $message, $context) use ($cache) { // create cache key $key = md5(serialize([$message, $context])); if ($cache->has($key)) { return false; } $debounceInterval = $context[DebounceFilter::DEBOUNCE_INTERVAL]; $cache->set($key, true, new DateInterval($debounceInterval)); return true; }); $logger = new FilterLogger($mainLogger, $debounceFilter); $logger->alert('Db connect error', [ DebounceFilter::DEBOUNCE_INTERVAL => 'PT1H', // only log once an hour ]);