kitloong/laravel-app-logger
Composer 安装命令:
composer require kitloong/laravel-app-logger
包简介
Laravel log for your application
README 文档
README
This package provides middleware that generates HTTP request and performance logs from incoming requests.
This package also provides a Database query log to log all executed queries in your application.
Installation
composer require kitloong/laravel-app-logger
Usage
To start using HTTP request and performance logger please add the package's middleware in your app/Http/Kernel.php or routes.
\KitLoong\AppLogger\Middlewares\AppLogger::class
No code modification needed to use the Database query log, you only need to enable it through .env.
By default, HTTP request and performance are enabled while the query log is disabled.
However, you could change each setting respectively to a different environment.
# By default RUN_HTTP_LOG=true RUN_PERFORMANCE_LOG=true RUN_QUERY_LOG=false
Log format
Http request log
[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
Performance log
[2021-01-10 23:35:27] local.INFO: 2725ffb10adeae3f POST /path 201 - Time: 55.82 ms - Memory: 22.12 MiB
Query log
[2021-01-10 23:35:27] local.INFO: Took: 2.45 ms mysql Sql: select * from `users` where `id` = 1
What's more
This package uses https://github.com/spatie/laravel-http-logger as the base for the HTTP request log, as well as the code design pattern.
It is common to receive tons of incoming requests in a real-life production application.
To ease for analysis, a unique string is embedded into HTTP request and performance log to indicate both log entries are related.
# 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
If you found any high memory usage or slow requests you could easily grep request log by the unique string for more information.
Configuration
You could also publish the config file to change more configuration or even use your own implementation:
php artisan vendor:publish --provider="KitLoong\AppLogger\AppLoggerServiceProvider" --tag=config
You could check the content of the config file here.
Config: Logging channel
By default, Laravel App Logger writes logs into your default logging channel.
However, you may implement a new logging channel in Laravel config/logging.php, and overwrite the channel in the published config file.
An example is written for a better explanation.
In 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, ], ]
In config/app-logger.php:
'http' => [ ... 'channel' => 'request' ], 'performance' => [ ... 'channel' => 'performance' ], 'query' => [ ... 'channel' => 'query' ]
Config: Implement own logger
You could even write your own logger implementation and overwrite it in the config file.
Here is the code snippet of HTTP request:
/* * 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,
You could find a similar configuration in the performance and query section.
When you write your own log_profile, you must implement each loggers' own LogProfile interface.
| Logger | Interface |
|---|---|
| http | \KitLoong\AppLogger\HttpLog\HttpLogProfile |
| performance | \KitLoong\AppLogger\PerformanceLog\PerformanceLogProfile |
| query | \KitLoong\AppLogger\QueryLog\QueryLogProfile |
The interface requires shouldLog implementation. This is where you place your log condition.
When you write your own log_writer, you must implement each loggers' own LogWriter interface.
| Logger | Interface |
|---|---|
| http | \KitLoong\AppLogger\HttpLog\HttpLogWriter |
| performance | \KitLoong\AppLogger\PerformanceLog\PerformanceLogWriter |
| query | \KitLoong\AppLogger\QueryLog\QueryLogWriter |
The interface requires log implementation. This is where you define your log body message.
License
The Laravel Application Logger is open-sourced software licensed under the MIT license
kitloong/laravel-app-logger 适用场景与选型建议
kitloong/laravel-app-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.31M 次下载、GitHub Stars 达 10, 最近一次更新时间为 2021 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「log」 「performance」 「query」 「request」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kitloong/laravel-app-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kitloong/laravel-app-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kitloong/laravel-app-logger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
g4 application profiler package
CSS/Javascript Minificator, Compressor and Concatenator for TYPO3 - highly configurable frontend asset optimization for CSS/JS merging, minification and compression with optional body parsing, async/defer loading, inline output, data-ignore exclusions, SRI integrity validation/calculation, external
WordPress mu-plugin to remove jQuery Migrate from the list of jQuery dependencies and to allow jQuery to enqueue before </body> instead of in the <head>.
Create link to static resources with cache-breaking segment based on md5 of the file
Query filtering in your frontend
Anax Database Active Record module for model classes.
统计信息
- 总下载量: 1.31M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 22
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-10