dotburo/laravel-molog

Laravel 工具,用于记录特定模型的日志信息和指标

1.6.0 2023-06-13 14:39 UTC

README

GitHub Workflow Status Latest Version on Packagist Total Downloads

Laravel Molog 允许您记录与特定模型相关的日志信息和指标。类似于 Spatie 的 Activity Log,但更通用,并且可以将指标(度量)关联到消息或任何其他 Laravel 模型。度量工厂类还提供内置计时器、增加和百分比方法。

在最简单的形式中

$user = auth()->user();

$this->message('Mail sent!')->concerning($user)->save();

一个稍微复杂一点的例子——一个自定义模型的带有指标的日志消息

$model = new Model();

$this->gauges()->startTimer();

$this->message()->notice('Import started...')->concerning($model)->save();

// processing...

$this->gauges()
    ->concerning($this->messages()->last())
    ->gauge('Files accepted', 16)
    ->gauge('Files refused', 2)
    ->stopTimer()
    ->save();

经典的异常日志记录

$msg = $this->message(new Exception('Oops'))->setContext('example');

echo $msg;              // 2023-04-10 17:34:22.348 [debug] [example] Oops
echo $msg->subject;     // Oops
echo $msg->body;        // Stack trace ...

功能

  • 将日志消息和指标(度量)附加到模型
  • 遵循 PSR-3:Logger 接口
  • 异常日志记录
  • 开始和停止计时器指标
  • 度量的增加和减少方法
  • 将消息和指标输出为字符串
  • 通用 HTTP 控制器

用法

使用 composer 从 packagist.org 安装

composer require dotburo/laravel-molog

发布配置文件和迁移,并迁移您的应用程序

php artisan vendor:publish --provider="Dotburo\Molog\MologServiceProvider"

php artisan migrate

在需要记录的地方使用 Logging 特性。有关更多信息,请参阅文档中的 用法示例

class YourClass {

    use \Dotburo\Molog\Traits\Logging;
    
    protected function handle()
    {
        // This will store three messages
        $this->messages()
            ->message('Import process initiated', \Psr\Log\LogLevel::INFO)
            ->notice('Import process ongoing')
            ->warn('Import process aborted')
            ->save();
        
       // This will store one message with the subject 'aborted' and level critical
       $this->message()
            ->setContext('Import process')
            ->notice('ongoing')
            ->warn('aborted')
            ->setLevel(\Dotburo\Molog\MologConstants::CRITICAL)
            ->save();
        
        // Associate all subsequent metrics with the last message
        $this->gauges()->concerning($this->messages()->last());
        
        // Associate this metric of type INT with the first message
        $this->gauge('density', 5)->concerning($this->messages()->first())->save();
        
        // Add three metrics associated with the last message
        $this->gauges()
            ->gauge('density', 5.3567)  // updates the previous 'density' metric to the FLOAT value
            ->gauge('pressure', 2.35, 'bar', \Dotburo\Molog\MologConstants::GAUGE_INT_TYPE) // forcibly cast to FLOAT
            ->gauge('quality', 3)
            ->save();
    }
}

测试

composer test

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

请查看我们关于如何报告安全漏洞的 安全策略

致谢

许可

GNU 通用公共许可证 (GPL)。有关更多信息,请参阅 许可文件