doukekui0914 2017-02-16 23:19
浏览 32
已采纳

记录上下文数据(未明确指定)

I'm working on a multi-tenant app where I need to log a lot more data than what I pass to the log facade. What I mean is, every time I do this...

Log::info('something happened');

I get this:

[2017-02-15 18:12:55] local.INFO: something happened

But I want to get this:

[2017-02-15 18:12:55] [my ec2 instance id] [client_id] local.INFO: something happened

As you can see I'm logging the EC2 instance ID and my app's client ID. I'm of course simplifying this as I need to log a lot more stuff in there. When I consume and aggregate these logs, having these extra fields make them incredibly handy to figure what things went wrong and where.

In the Zend Framework, I usually subclass the logger and add these extra fields in my subclass but I'm not sure how I can do that with Laravel. I can't find where the logger is instantiated so that I can plug my custom logger in (if that is even the way to go in Laravel).

So, I'm not asking how to get the EC2 instance ID and the other stuff, I'm only asking what the proper way to "hot wire" the Laravel logger is to be able to plug this in.

  • 写回答

1条回答 默认 最新

  • dpa89292 2017-02-16 23:57
    关注

    Just an idea... the logger in Laravel is really a Monolog instance... You could push a handler on it and do whatever processing you want for each entry... like so...

    <?php
    
    $logger->pushProcessor(function ($record) {
        $record['extra']['dummy'] = 'Hello world!';
    
        return $record;
    });
    

    As per the Laravel doc you can hook up into the monolog config at boot...

    Custom Monolog Configuration

    If you would like to have complete control over how Monolog is configured for your application, you may use the application's configureMonologUsing method. You should place a call to this method in your bootstrap/app.php file right before the $app variable is returned by the file:

    $app->configureMonologUsing(function ($monolog) {
        $monolog->pushHandler(...);
    });
    
    return $app;
    

    So instead just push a processor on the $monolog instance passed to the hook...

    Just an idea, I have not tried this in Laravel but used Monolog before...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果