doukekui0914 2017-02-16 15: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 15: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...

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部