kitloong / laravel-app-logger
Laravel 应用日志
Requires
- php: >=7.1.3
- ext-json: *
- illuminate/support: ^5.8|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- spatie/laravel-http-logger: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.17|^3.0
- mockery/mockery: ^1.0
- orchestra/testbench: ^3.8|^4.0|^5.0|^6.0|^7.0|^8.0|^9.0
- squizlabs/php_codesniffer: ^3.5
README
此包提供了生成 HTTP 请求 和 性能 日志的中间件。
此包还提供了一个数据库 查询日志,以记录应用程序中执行的所有查询。
安装
composer require kitloong/laravel-app-logger
使用方法
要开始使用 HTTP 请求 和 性能 日志记录器,请将包的中间件添加到您的 app/Http/Kernel.php
或路由中。
\KitLoong\AppLogger\Middlewares\AppLogger::class
使用数据库 查询日志 不需要修改代码,您只需通过 .env
启用它。
默认情况下,HTTP 请求 和 性能 已启用,而 查询日志 已禁用。
但是,您可以将每个设置分别更改为不同的环境。
# By default RUN_HTTP_LOG=true RUN_PERFORMANCE_LOG=true RUN_QUERY_LOG=false
日志格式
HTTP 请求日志
[2021-01-10 23:35:27] local.INFO: 2725ffb10adeae3f POST /path - Body: {"test":true} - Headers: {"cookie":["Phpstorm-12345"],"accept-language":["en-GB"]} - Files: uploaded.txt
性能日志
[2021-01-10 23:35:27] local.INFO: 2725ffb10adeae3f POST /path 201 - Time: 55.82 ms - Memory: 22.12 MiB
查询日志
[2021-01-10 23:35:27] local.INFO: Took: 2.45 ms mysql Sql: select * from `users` where `id` = 1
此外
此包使用 https://github.com/spatie/laravel-http-logger 作为 HTTP 请求 日志的基础,以及代码设计模式。
在实际生产应用中,通常会收到大量的请求。
为了便于分析,将一个唯一的字符串嵌入到 HTTP 请求 和 性能 日志中,以指示这两个日志条目是相关的。
# HTTP request, unique: 2725ffb10adeae3f
[2021-01-10 23:35:25] local.INFO: 2725ffb10adeae3f GET /path - Body ...
# Performance, unique: 2725ffb10adeae3f
[2021-01-10 23:35:27] local.INFO: 2725ffb10adeae3f GET /path 200 - Time: 55.82 ms - Memory: 5.12 MiB
如果您发现任何内存使用量高或请求缓慢的情况,可以轻松地通过唯一字符串grep请求日志以获取更多信息。
配置
您还可以发布配置文件以更改更多配置,甚至使用自己的实现
php artisan vendor:publish --provider="KitLoong\AppLogger\AppLoggerServiceProvider" --tag=config
您可以在此处查看配置文件的内容。
配置:日志通道
默认情况下,Laravel App Logger 将日志写入您默认的日志通道。
但是,您可以在 Laravel 的 config/logging.php
中实现新的日志通道,并覆盖已发布配置文件中的 channel
。
以下是一个示例,以更好地说明。
在 Laravel config/logging.php
'channels' => [ 'request' => [ 'driver' => 'daily', 'path' => storage_path('logs/request.log'), 'level' => 'debug', 'days' => 14, ], 'performance' => [ 'driver' => 'daily', 'path' => storage_path('logs/performance.log'), 'level' => 'debug', 'days' => 14, ], 'query' => [ 'driver' => 'daily', 'path' => storage_path('logs/query.log'), 'level' => 'debug', 'days' => 14, ], ]
在 config/app-logger.php
'http' => [ ... 'channel' => 'request' ], 'performance' => [ ... 'channel' => 'performance' ], 'query' => [ ... 'channel' => 'query' ]
配置:实现自己的日志记录器
您甚至可以编写自己的日志记录器实现,并在配置文件中覆盖它。
以下是 HTTP 请求 的代码片段
/* * The log profile which determines whether a request should be logged. * It should implement `HttpLogProfile`. */ 'log_profile' => \KitLoong\AppLogger\HttpLog\LogProfile::class, /* * The log writer used to write the request to a log. * It should implement `HttpLogWriter`. */ 'log_writer' => \KitLoong\AppLogger\HttpLog\LogWriter::class,
您可以在 performance
和 query
部分找到类似的配置。
当您编写自己的 log_profile
时,您必须实现每个日志记录器的自己的 LogProfile
接口。
该接口要求实现 shouldLog
。这是您放置日志条件的地方。
当您编写自己的 log_writer
时,您必须实现每个日志记录器的自己的 LogWriter
接口。
该接口要求实现 log
。这是您定义日志体消息的地方。
许可证
Laravel 应用日志记录器是开源软件,许可协议为 MIT 许可证