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

问题描述:

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.

我正在开发一个多租户应用程序,我需要记录比传递给我的更多数据 日志立面。 我的意思是,每次我这样做...... p>

  Log :: info('发生的事情'); 
  code>  pre> 
  
 

我明白了: p>

[2017-02-15 18:12:55] local.INFO:发生了什么事 p>

但我想得到这个: p>

[2017-02-15 18:12:55] [my ec2 instance id ] [client_id] local.INFO:发生了什么事情 p> blockquote>

正如您所见,我正在记录EC2实例ID和我的应用程序的客户端ID。 我当然正在简化这个,因为我需要在那里记录更多的东西。 当我使用和聚合这些日志时,拥有这些额外的字段可以非常方便地找出出错的地方和位置。 p>

在Zend Framework中,我通常将记录器子类化并添加这些额外的 在我的子类中的字段,但我不知道如何使用Laravel来做到这一点。 我无法找到记录器实例化的位置,以便我可以插入自定义记录器(如果这甚至是Laravel中的方法)。 p>

所以,我不是在问 如何获取EC2实例ID和其他东西,我只是想知道Laravel记录器“热线”的正确方法是能够插入它。 p> div>

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...