consilience / laravel-extended-logging
扩展Laravel和Lumen日志以更好地支持容器
Requires
- php: ^8.1
- illuminate/support: ^10.0
- monolog/monolog: ^3.0
This package is auto-updated.
Last update: 2024-09-21 18:24:44 UTC
README
Laravel扩展日志
提供一些现成的日志扩展,以便更容易地将Laravel部署到多个容器中。
主要功能包括
- 一系列有用的标准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.php
中的channels
部分
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和容器以及多个作业的所有日志,形成一个大型数据库。
要搜索和过滤这些日志条目,必须以可过滤的方式记录上下文信息。
此日志tap中包含的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" }, },
待办事项
- 测试。