senkevich33n/laravel-extended-logging

扩展 Laravel 和 Lumen 的日志功能,以更好地支持容器

1.4.1 2023-08-07 10:13 UTC

This package is not auto-updated.

Last update: 2024-10-01 13:58:43 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

Laravel 和 Lumen 扩展日志

提供一些现成的日志扩展,以便更容易地将 Laravel 和 Lumen 部署到多个容器中。

主要功能包括

  • 许多有用的标准 Monolog 处理器。
  • Laravel 用户 ID(如果可用)。
  • 正在运行的作业的完全限定类名。
  • 一个序列号,以便在日志混乱时可以重新排序。
  • 应用程序名称和子系统名称。
  • 以结构化 JSON 编写的日志。

应用于此处日志功能的是轻量级、有观点的、主要是不可配置的,并且是我们发现对我们自己的项目非常有用的功能。我们很高兴接受有关附加功能的 PR,尽管考虑到这个包不是为了“样样都有”,而是一个快速且易于安装的包,通过最小努力将应用程序日志容器准备好。

安装

对于 Laravel

php composer require consilience/laravel-extended-logging

Lumen 需要在 bootstrap/app.php 中注册提供者,以便包可以跟踪当前正在运行的作业的名称

$app->register(Consilience\Laravel\ExtendedLogging\LoggingServiceProvider::class);

配置

主要的配置是通过 Laravel 的 config/logging.php 配置脚本完成的,通过添加一个通道。

<?php

use Monolog\Handler\StreamHandler;
use Monolog\Formatter\JsonFormatter;
use Consilience\Laravel\ExtendedLogging\Tap as ExtendedTap;

// ...

    'channels' => [
        'my-extended-logging-channel' => [
            //
            // monolog is the underlying driver.
            //
            'driver' => 'monolog',
            //
            // This is the handler to use within monolog, with any parameters to configure it.
            // Handlers can be found in \Monolog\Handler namespace.
            // Here send to a file stream.
            //
            'handler' => StreamHandler::class,
            //
            // Parameters for the monolog handler.
            // Here the file stream is stderr.
            //
            'with' => [
                'stream' => 'php://stderr',
            ],
            //
            // The custom tap to offer additional manipulation of the log output.
            // Other taps from other packages can be added here to extend further.
            //
            'tap' => [
                ExtendedTap::class,
            ],
            //
            // The output formatter.
            // The standard Monolog json formatter has a good structure that is easy to parse.
            //
            'formatter' => JsonFormatter::class,
            'formatter_with' => [],
        ],
    ],

配置条目的更紧凑版本,将放入 config/logging.phpchannels 部分

use Monolog\Handler\StreamHandler; // Most likely already present.
use Monolog\Formatter\JsonFormatter;
use Consilience\Laravel\ExtendedLogging\Tap as ExtendedTap;
    'channels' => [
    
        // Use this channel for running in a container.
        // Sends all logs to stderr in a structured form, with additional metadata.
        // Can be mixed in a stack with other channels.
        'container' => [
            'driver' => 'monolog',
            'handler' => StreamHandler::class,
            'with' => [
                'stream' => 'php://stderr',
            ],
            'tap' => [
                ExtendedTap::class,
            ],
            'formatter' => JsonFormatter::class,
            'formatter_with' => [],
        ],
        
        //...
    ],

然后在容器中运行时设置 LOG_CHANNEL=container,将所有日志发送到 stderr。其他通道可能更适合其他环境。

通过发布包的配置文件(laravel-extended-logging.php),可以获得额外的选项

php artisan vendor:publish --tag=laravel-extended-logging-config

目前支持两种选项

  • json-pretty-print - 设置为 true 以将 JSON 输出格式化为更易读
  • processor - Monolog 处理器类的列表

默认情况下,处理器列表将包括此包提供的自定义处理器以及 Monolog 提供的几个处理器。您可以删除不需要的,并添加您可能需要的任何其他处理器。

如果处理器接受参数,请使用此格式

Processor::class => [parameter1, parameter2, ...],

目前只支持位置参数。

配置文件仍然接受实例化处理器对象以支持遗留安装。您应将这些更改为未实例化的 Processor::class 格式以支持配置缓存。

配置升级

从版本 1.2.0 开始,运行的处理器位于配置文件中。您需要再次发布配置文件才能使用处理器。

示例用法

我们在 Kubernetes/Docker 环境中运行 Laravel 和 Lumen 应用程序,所有日志条目都由 Elasticsearch 索引并由 Kibana 展示。这汇集了我们来自多个应用程序、多个 pod 和容器以及多个作业的所有日志,到一个大数据库中。

为了搜索和过滤这些日志条目,以可过滤的方式记录上下文信息至关重要。

此日志滴答中包含的 PsrLogMessageProcessor 使结合上下文数据和日志消息构造变得非常简单。因此,日志看起来像这样,既有数据上下文数组,又有带有字段替换的日志消息

Log::debug('Product {productId} added to category {categorySlug}', [
    'productId' => $product->id,
    'productName' => $product->name,
    'categorySlug' => $category->slug,
]);

生成的日志消息将类似于以下内容,嵌入到您用于捕获和包装日志消息的任何内容中。

  "_source": {
    "@timestamp": "2020-03-17T11:45:15.573Z",
    "stream": "stderr",
    "time": "2020-03-17T11:45:15.57341869Z",
    "message": "Product 123 added to category good-stuff",
    "context": {
      "productId": 123,
      "productName": "A Nice Slice of Cake",
      "categorySlug": "good-stuff",
    },
    "level": 100,
    "level_name": "DEBUG",
    "channel": "development",
    "datetime": {
      "date": "2020-03-17 11:45:15.572810",
      "timezone_type": 3,
      "timezone": "UTC"
    },
    "extra": {
      "memory_usage": "24 MB",
      "process_id": 1,
      "uid": "58bcec3ef88a7ceb",
      "job_name": "App\\Jobs\\ImportProductCategories",
      "application": "great-shop",
      "subsystem": "admin-app"
    },
  },

待办事项

  • 测试。