承接 yuriitatur/laravel-logger-boost 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yuriitatur/laravel-logger-boost

最新稳定版本:v1.0.0

Composer 安装命令:

composer require yuriitatur/laravel-logger-boost

包简介

A simple package for boosting your logging experience in Laravel

README 文档

README

Quality Gate Status Coverage

Laravel logger boost

Add useful processors to your app.

Installation

composer require yuriitatur/laravel-logger-boost

Testing

composer test

Usage

Disclaimer

This package is designed to work with a Laravel framework, but it can be used with/without any framework as the set of useful Monolog processors.

Laravel

Add BoostLogger to your channel config in config/logging.php

'channels' => [
    'single' => [
        'driver' => 'single',
        'tap' => [\YuriiTatur\MonologHandler\BoostLogger::class],
        'path' => storage_path('logs/laravel.log'),
        'level' => env('LOG_LEVEL', 'debug'),
    ],
],

Or you can add even more context with AddMoreContext

'channels' => [
    'single' => [
        'driver' => 'single',
        'tap' => [
            \YuriiTatur\MonologHandler\BoostLogger::class,
            \YuriiTatur\MonologHandler\AddMoreContext::class
        ],
        'path' => storage_path('logs/laravel.log'),
        'level' => env('LOG_LEVEL', 'debug'),
    ],
],

This will add multiple extra fields like hostname, is_cli, route or user. Thus, returning all that data in each record is overhead. So make use of a stack driver with bubble option

 'channels' => [

        'stack' => [
            'driver' => 'stack',
            'channels' => [ // this is our main channels
                'single-error', // single-error channel will not bubble message to single
                'single',
            ],
            'ignore_exceptions' => false,
        ],

        'single' => [ // all debug+ messages goes with only BoostLogger
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'formatter' => JsonFormatter::class,
            'tap' => [
                BoostLogger::class,
                BufferRecords::class . ':10', // optionally enable record buffering, with specified buffer size
            ],
        ],

        'single-error' => [ // if something happens, use AddMoreContext and bubble => false
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'error',
            'bubble' => false, // if this is true, the message will be duplicated
            'formatter' => JsonFormatter::class,
            'tap' => [
                BoostLogger::class,
                AddMoreContext::class,
            ],
        ],
}

Eloquent

You can also add log key to your eloquent models, to keep track of all that happened to them with one key. First, add log_key column to your table;

# in your migration
Schema::table('my_models', function (Blueprint $table) {
    $table->string('log_key')->nullable()->unique();
});

The reason, why it's not in a separate thing called AddLogKeyColumn class/method is because, requirements to this column may vary. Create your own how you like it. Next, add LogKeyAwareTrait trait in your model. You can use overtakeLogs and applyLogKey methods now.

# in your model
class Product extends Model 
{
    use LogKeyAwareTrait;
}

# in your service
public function createProduct($params) 
{
    $product = new Product($params);
    $product->applyLogKey(); # creating event is a good place to put this 
    $product->save();
}

public function updateProduct($id, $params) 
{
    $product = Product::findOrFail($id);
    $product->overtakeLogs(); # this will take log key from the model and apply to your logger 
    $product->update($params);
}

Queue Jobs

This package automatically registers new LoggerAwareWorkerDecorator queue worker. That now resets all processors between each job, this is done specifically to reload log keys, so each job can be traced using its own id.

License

This code is under MIT license, read more in the LICENSE file.

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-27