symplely / logger
一个简单的异步PSR-3日志实现
这个包的官方仓库似乎已经消失,因此该包已被冻结。
Requires
- php: >7.1
- psr/log: ~1.0
- symplely/coroutine: ^1.4.0
Requires (Dev)
- phpunit/phpunit: >5.7
Provides
- psr/log-implementation: 1.0.0
README
一个简单、快速、兼容PSR-3的异步日志记录器。
这个 Logger
库旨在模仿 Python 的 Async aiologger 包的功能。
目录
介绍/用法
logger 可以使用多个后端 writer 创建不同格式的日志条目。使用 logger 的基本要求是同时有一个 writer 和一个 logger 实例。一个 writer 将 log entry/message 存储到后端,而 logger 实例则消耗 writer 来执行日志操作。
日志条目/消息包含要记录的信息。消息可以由一个格式字符串和参数(作为两个单独的参数给出)组成,一个字符串或报告,以及参数,一个键值列表数组。这些参数可以与报告回调和 processor
一起使用,通过 addProcessor()
方法。
报告回调是一个方便的函数,后端 formatter 可以使用它将报告转换为格式字符串和参数,或者直接转换为字符串。如果没有提供回调或需要自定义格式化,defaultFormatter()
方法也可以使用其自己的转换函数。
函数
此 logger
包旨在用于 异步,这需要使用在其中的 underlying 包来运行。以下所有函数都应该在从我们的 Coroutine 库中的 coroutine_run()
函数启动的例程中调用。
/** * Will create or return an logger instance by tag name. * If no name supplied, will use calling class name */ \logger_create($name); /** * Return an logger instance by name. * If no name supplied, will use calling class name */ \logger_instance($name); /** * Close and perform any cleanup actions by name. * Optionally, clear arrayWriter logs. * - This function needs to be prefixed with `yield` */ yield \logger_close($name, $clearLogs); /** * Shutdown Logger. * Commit, close, and clears out ALL logger instances. * - This function needs to be prefixed with `yield` */ yield \logger_shutdown(); /** * Wait for logs to commit and remove finished logs from logging tasks list by name * - This function needs to be prefixed with `yield` */ yield \logger_commit($name) /** * Returns the array of `arrayWriter()` Logs. * - This function needs to be prefixed with `yield` */ yield \logger_arrayLogs($name) /** * Printout the `arrayWriter()` Logs. * - This function needs to be prefixed with `yield` */ yield \logger_printLogs($name)
/** * Will setup an custom backend `writer`, optionally by name * - This function needs to be prefixed with `yield` */ yield \logger_writer($writer, $levels, $interval, $formatter, $name); /** * Will setup the O.S. syslog as backend `writer`, optionally by name */ \logger_syslog($logOpts, $facility, $levels, $formatter, $name); /** * Will setup the O.S. error_log as backend `writer`, optionally by name */ \logger_errorlog($type, $levels, $formatter, $name); /** * Will setup an memory array as backend `writer`, optionally by name */ \logger_array($levels, $interval, $formatter, $name); /** * Will setup an file/stream as backend `writer`, optionally by name * - This function needs to be prefixed with `yield` */ yield \logger_stream($stream, $levels, $interval, $formatter, $name); /** * Will setup to send email as backend `writer`, optionally by name * - This function needs to be prefixed with `yield` */ yield \logger_mail($to, $subject, $headers, $levels, $interval, $formatter, $name);
/** * Will setup and add custom `processor` report callback, for on named `{key}` placeholder, * for the global instance by name */ \logger_processor($key, $processor, $name); /** * Will add prefix to unique id to the message to be logged, on `{unique_id}` placeholder, * for the global instance by name */ \logger_uniqueId($prefix, $name); /** * Will add an PHP process ID to the message to be logged, on `{pid}` placeholder, * for the global instance by name */ \logger_pid($name); /** * Will add timestamp by either microsecond to the message to be logged, on `{timestamp}` placeholder, * for the global instance by name */ \logger_timestamp($micro, $name); /** * Will add type of PHP interface to the message to be logged, on `{php_sapi}` placeholder, * for the global instance by name */ \logger_phpSapi($name); /** * Will add PHP version to the message to be logged, on `{php_version}` placeholder, * for the global instance by name */ \logger_phpVersion($name); /** * Will add memory usage by size[GB|MB|KB] to the message to be logged, on `{memory_usage}` placeholder, * for the global instance by name * - This function needs to be prefixed with `yield` */ yield \logger_memoryUsage($format, $real, $peak, $name);
/** * Will create new background task to Log an EMERGENCY message, optionally by name * - This function needs to be prefixed with `yield` */ yield \log_emergency($message, $context, $name); /** * Will create new background task to Log an ALERT message, optionally by name * - This function needs to be prefixed with `yield` */ yield \log_alert($message, $context, $name); /** * Will create new background task to Log an CRITICAL message, optionally by name * - This function needs to be prefixed with `yield` */ yield \log_critical($message, $context, $name); /** * Will create new background task to Log an ERROR message, optionally by name * - This function needs to be prefixed with `yield` */ yield \log_error($message, $context, $name); /** * Will create new background task to Log an WARNING message, optionally by name * - This function needs to be prefixed with `yield` */ yield \log_warning($message, $context, $name); /** * Will create new background task to Log an NOTICE message, optionally by name * - This function needs to be prefixed with `yield` */ yield \log_notice($message, $context, $name); /** * Will create new background task to Log an INFO message, optionally by name * - This function needs to be prefixed with `yield` */ yield \log_info($message, $context, $name); /** * Will create new background task to Log an DEBUG message, optionally by name * - This function needs to be prefixed with `yield` */ yield \log_debug($message, $context, $name);
安装
composer require symplely/logger
用法/历史
use Async\Logger\Logger; $logger = Logger::getLogger('php-app'); // or $logger = new Logger('php-app');
现在你可以设置后端 writer 来处理日志消息
yield $logger->setWriter('print_r'); // Or print_r every 10 records formatted with default formatter yield $logger->setWriter('print_r', Logger::ALL, 10);
以下是一些预设的后端 writer
yield $logger->streamWriter($stream = 'php://stdout', $levels = Logger::ALL, $interval = 1, callable $formatter = null); yield $logger->mailWriter($to, $subject = null, array $headers = [], $levels = Logger::ALL, $interval = 1, callable $formatter = null); $logger->errorLogWriter($errorType = 0, $levels = Logger::ALL, callable $formatter = null); $logger->syslogWriter($logOpts = \LOG_PID | \LOG_ODELAY | \LOG_CONS, $facility = \LOG_USER, $levels = Logger::ALL, callable $formatter = null); $logger->arrayWriter($levels = Logger::ALL, $interval = 1, callable $formatter = null);
你可以更改默认格式化
$logger->defaultFormatter(function ($level, $message, array $context) { return sprintf("[%s] (%s): %-10s '%s'", date(DATE_RFC822), $logger->getName(), strtoupper($level), $message); });
或者通过 writer 传递自定义格式化器
yield $logger->setWriter('print_r', Logger::ALL, 10, function ($level, $message, array $context) { // });
可以禁用一些级别以进行处理
$logger->disable(Logger::DEBUG | Logger::INFO);
处理器在将日志数据传递给 writer 之前执行。处理器的输入是一个日志事件,包含所有要记录的信息的数组;输出也是一个日志事件,但可以包含修改或附加的值。处理器在发送到 writer 之前修改日志事件。
addProcessor
方法允许添加一些额外的数据到上下文中,这些数据代表 PSR-3 消息占位符。
占位符名称对应于在记录消息时传递的 "context" 数组中的键
$logger->info('User with email {email} registered', ['email' => '[email protected]']);
$logger->addProcessor('some_key', function (array $context) { return 'some_value'; });
以下是一些预设的具体上下文处理器
$logger->addUniqueId($prefix); $logger->addPid(); $logger->addTimestamp($micro); $logger->addMemoryUsage($format, $real, $peak; $logger->addPhpSapi(); $logger->addPhpVersion();
记录一条信息消息
// will be executed in an new background task, will not pause current task yield $logger->info('Informational message'); // will pause current task, do other tasks until logger completes the log entry yield $logger->log(LogLevel::INFO, 'We have a PSR-compatible logger');
贡献
鼓励并欢迎贡献;我总是很高兴收到反馈或Github上的pull请求 :) 为错误和新特性创建Github Issues,并对您感兴趣的进行评论。
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。