seablast/logger

一个符合PSR-3规范的日志记录器,具有可调节的详细程度。

v2.0.1 2024-08-09 22:39 UTC

This package is auto-updated.

Last update: 2024-09-09 22:47:32 UTC


README

一个符合 PSR-3 规范的日志记录器,具有可调节的详细程度。

日志级别的详细程度可以根据不同的环境进行定制。例如,在开发环境中,日志记录器可以配置为记录比生产环境更详细的信息,而无需更改您的代码。只需调整详细程度。

logging_level 是最重要的设置。这些参数可以在实例化日志记录器时进行配置

use Seablast\Logger\Logger;
$conf = array(
    // THESE ARE THE DEFAULT SETTINGS
    // 0 = send message to PHP's system logger; recommended is however 3, i.e. append to the file destination set in the field 'logging_file'
    Logger::CONF_ERROR_LOG_MESSAGE_TYPE => 0,
    // if error_log_message_type equals 3, the message is appended to this file destination (path and name)
    Logger::CONF_LOGGING_FILE => '',
    // verbosity: log up to the level set here, default=5 = debug
    Logger::CONF_LOGGING_LEVEL => 5,
    // rename or renumber, if needed
    Logger::CONF_LOGGING_LEVEL_NAME => array(0 => 'unknown', 1 => 'fatal', 'error', 'warning', 'info', 'debug', 'speed'),
    // the logging level to which the page generation speed (i.e. error_number 6) is to be logged
    Logger::CONF_LOGGING_LEVEL_PAGE_SPEED => 5,
    // false => use logging_file with log extension as destination; true => adds .Y-m.log to the logging file
    Logger::CONF_LOG_MONTHLY_ROTATION => true,
    // prefix message that took longer than profiling step (float) sec from the previous one by SLOWSTEP
    Logger::CONF_LOG_PROFILING_STEP => false,
    // fatal error may just be written in log, on production, it is however recommended to set an e-mail, where to announce fatal errors
    Logger::CONF_MAIL_FOR_ADMIN_ENABLED => false,
);
$logger = new Logger($conf);

请参阅 test.php 了解用法。

默认情况下,日志记录器记录以下信息级别

  • 致命
  • 错误
  • 警告
  • 信息
  • 调试

并且忽略

  • 速度

注意:不支持将日志消息输出到屏幕。

运行时调整

  • 方法 logAtLeastToLevel(int $level) 可以更改在实例化时设置的级别以上的详细程度。
  • 方法 setUser(int|string $user) 可以将用户标识添加到错误消息中

Tracy\Logger::log 包装器

从 Nette\Tracy::v2.6.0 开始,即 "php": ">=7.1",可以使用PSR-3适配器,允许集成 seablast/logger

$logger = new \Seablast\Logger\Logger();
$tracyLogger = new \Tracy\Bridges\Psr\PsrToTracyLoggerAdapter($logger);
Debugger::setLogger($tracyLogger);
Debugger::enable();