johndodev/jlog-bundle

简易日志记录。

安装: 41

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

公开问题: 0

类型:symfony-bundle

dev-master 2024-09-23 15:36 UTC

This package is auto-updated.

Last update: 2024-09-23 15:36:17 UTC


README

安装

composer require johndodev/jlog-bundle

配置

.env

JLOG_API_KEY=your_api_key

config/packages/jlog.yaml

jlog:
    project_api_key: '%env(JLOG_API_KEY)%'
    enable_exception_listener: true
    ignore_exceptions:
        - Symfony\Component\HttpKernel\Exception\NotFoundHttpException
    # See chapter about logging commands
    console_channel: console

monolog.yaml

需要使用缓冲处理程序。
有关配置选项,请参阅https://github.com/symfony/monolog-bundle/blob/master/DependencyInjection/Configuration.php#L139
示例

monolog:
    channels:
        - deprecation # Deprecations are logged in the dedicated "deprecation" channel when it exists
        - command # same name as jlog.console_channel
    
    ...
    
    handlers:
        buffer:
            type: buffer
            handler: jlog
            level: debug # or whatever
            channels: ["app"]  # or whatever
            
        jlog:
            type: service
            id: jlog.monolog_handler

用法

记录命令

在你的日志命令中扩展Johndodev\JlogBundle\Console\LoggableOutputCommand
命令的开始和结束将记录在Jlog中,在您配置的通道中,输出作为元数据。

记录异常

未处理的异常已经记录(如果配置中已启用)。您仍然可以像往常一样手动记录它们,使用monolog。

try {
    // ...
} catch (\Exception $e) {
    $this->logger->error('lorem ipsum...', [
        'exception' => $e->getMessage()
    ]);
}

记录消息

使用monologer,就像平常一样。
建议使用占位符,特别是对于触发通知的错误,因为消息用于唯一通知的签名。

$this->logger->error('bla bla: {error}', [
    'error' => $e->getMessage()
]);