artim / logger
此包已被废弃且不再维护。未建议替代包。
laravel的文件日志记录器
0.2.4
2023-05-16 07:55 UTC
Requires
- php: ^8.1
- ext-json: *
- illuminate/auth: ^9|^10
- illuminate/bus: ^9|^10
- illuminate/config: ^9|^10
- illuminate/console: ^9|^10
- illuminate/database: ^9|^10
- illuminate/http: ^9|^10
- illuminate/log: ^9|^10
- illuminate/pipeline: ^9|^10
- illuminate/support: ^9|^10
- monolog/monolog: ^2.0|^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.11
- laravel/framework: ^9|^10
- nunomaduro/larastan: ^2.2
README
如何开始
首先我们需要安装包
composer require artim/logger
将 LARAVEL_START 常量添加到根目录下的 artisan 文件中
define('LARAVEL_START', microtime(true));
这是为了修正请求处理的时间。
然后我们需要将 ArtimLoggerServiceProvider.php 添加到 app.php 配置文件中。在这里你可以看到配置设置对将要记录的信息的影响。
class ArtimLoggerServiceProvider extends ServiceProvider { public function boot(): void { $this->publishes([ __DIR__ . '/../config/artim-logger.php' => config_path('artim-logger.php'), ], 'config'); } public function register(): void { $this->app->make(LogRegistrator::class)->set(); $this->app->make(HttpRegistrator::class)->set(); if (config('artim-logger.logs.application')) { $this->app->make(AppLogRegistrator::class)->set(); } if (config('artim-logger.logs.db')) { $this->app->make(DBLogRegistrator::class)->set(); } $this->mergeConfigFrom(__DIR__ . '/../config/artim-logger.php', 'artim-logger'); } }
class AppLogRegistrator extends AbstractRegistrator { public function set(): void { $this->app->terminating(function () { $data = [ 'type' => 'application', 'startedAt' => LARAVEL_START, 'endedAt' => microtime(true), 'peakMemoryUsage' => get_formatted_peak_memory_usage(), ]; if (config('artim-logger.logs.request')) { $data['request'] = request(); } \Log::info('App terminating', $data); }); } }
然后我们需要将 Http 门面添加到 app.php 中的别名列表中
'Http' => \Artim\Logger\Http\Http::class,
现在你可以将日志记录器的配置添加到 logging.php 中
'artim' => [ 'driver' => 'monolog', 'level' => env('LOG_LEVEL', 'debug'), 'handler' => \Monolog\Handler\RotatingFileHandler::class, 'formatter' => \Artim\Logger\Logger\File\JsonFormatter::class, 'handler_with' => [ 'filename' => storage_path('logs/laravel-artim.log'), ], ],
它将日志写入 storage/logs/laravel-test.log 文件
\Log::info('Some log message', ['property' => 'some value of the property']);
{ "message": "Some log message", "context": { "property": "some value of the property", "user": null, "token": "cd3856c0bc1abae60ebd8f1df9d0ebb8" }, "level": 200, "level_name": "INFO", "channel": "local", "datetime": "2023-03-18T18:58:13.262363+00:00", "extra": {} }
如果你想跟踪你的应用程序在队列中的操作,可以使用 Artim\Logger\Job 类
namespace Artim\Logger\Job; use Illuminate\Contracts\Queue\ShouldQueue; abstract class Job implements ShouldQueue { use LogToken; protected string $logToken; }