vmorozov/laravel_fluentd_logger

提供使用fluentd作为日志驱动器的功能。

v1.1.1 2022-12-15 06:48 UTC

README

Latest Version on Packagist Total Downloads

此包提供了使用fluentd作为日志驱动器的功能。它还添加了额外的日志功能,例如

  • 请求日志
  • 数据库查询日志
  • 队列作业日志
  • 日志跟踪

安装

您可以通过composer安装此包

composer require vmorozov/laravel_fluentd_logger

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="laravel_fluentd_logger-config"

将中间件添加到app/Http/Kernel.php

protected $middleware = [
    // ... other middlewares here
    \Vmorozov\LaravelFluentdLogger\Middleware\LogRequestMiddleware::class,
    \Vmorozov\LaravelFluentdLogger\Middleware\ContinueTraceMiddleware::class,
];

将fluentd日志通道添加到config/logging.php

// ...some exisiting channels

'fluentd' => [
    'driver' => 'fluentd',
    'level' => env('LOG_LEVEL', 'debug'),
],

添加带有fluentd配置的ENV变量

FLUENTD_HOST=127.0.0.1
FLUENTD_PORT=24224

配置

在配置文件laravel_fluentd_logger.php中,您可以进行一些调整

  • 禁用某些功能
    'features_enabled' => [
        'request_log' => false,
        'db_query_log' => false,
        'queue_log' => false,
    ],
  • 覆盖默认的fluentd日志处理器
    // optionally override \Vmorozov\LaravelFluentdLogger\Logs\FluentHandler class to customize behaviour
    'handler' => SomeCustomHandler::class,
  • 更改日志标记格式
'tagFormat' => '{{app_name}}.{{level_name}}',
  • 覆盖fluentd sdk类的一些选项
    /** @see https://github.com/fluent/fluent-logger-php/blob/master/src/FluentLogger.php */
    'options' => [],

    /** @see https://github.com/fluent/fluent-logger-php/blob/master/src/PackerInterface.php */
    // specified class name
    'packer' => null,

Fluentd配置示例

  • 简单的stdout输出
<match your_app_name_from_env.**>
  type stdout
</match>
  • 输出到Elasticsearch + stdout
# sendlog to the elasticsearch
# the host must match to the elasticsearch
# container service
<match your_app_name_from_env.**>
  @type copy
  <store>
    @type elasticsearch
    host elasticsearch
    port 9200
    logstash_format true
    logstash_prefix fluentd
    logstash_dateformat %Y-%m-%d
    include_tag_key true
    type_name access_log
    tag_key @log_name
    flush_interval 1s
    log_es_400_reason false
  </store>
  <store>
    @type stdout
  </store>
</match>
  • fluentd用于接受数据使用的端口配置
# bind fluentd on IP 0.0.0.0
# port 24224
<source>
  @type forward
  port 24224
  bind 0.0.0.0
</source>

有关fluentd配置的更多信息,可以在官方fluentd文档中找到。

Monolog处理器

您可以通过将它们添加到laravel_fluentd_logger.php配置文件中的processors数组中来向Monolog处理器添加处理器。

'processors' => [CustomProcessor::class],

CustomProcessor.php

class CustomProcessor
{
    public function __invoke($record)
    {
        $record['extra']['level'] = $record['level_name'];
        return $record;
    }
}

测试

composer test

鸣谢

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件