Situation:
In my Symfony2.8 API, I have a custom RecordProcessor for Monolog, in which I added some convenient informations:
my_api.logger.formatter:
class: Monolog\Formatter\LineFormatter
arguments:
- "[%%datetime%%] [%%extra.request_id%%] [%%extra.ip%%] [%%extra.user%%] %%channel%%.%%level_name%%: %%message%% %%context%%\n"
As you can see, I add to every request (common practice I guess) a unique execution Uuid (request_id) to the headers of the request in front controllers (web/app.php). So here is what my logs lines look like:
[2017-10-27 13:11:24] [7e1666b7] [79.XX.XX.XX] [myuser@gmail.com] app.INFO: OutGoing GET ...
[2017-10-27 13:11:24] [15ded457] [11.XX.XX.XX] [myotheruser@gmail.com] security.DEBUG: ...
[2017-10-27 13:11:24] [15ded457] [11.XX.XX.XX] [myotheruser@gmail.com] security.DEBUG: ...
[2017-10-27 13:11:24] [15ded457] [11.XX.XX.XX] [myotheruser@gmail.com] app.INFO: Incoming POST ...
[2017-10-27 13:11:24] [15ded457] [44.XX.XX.XX] [myotheruser@gmail.com] app.INFO: Asynchronous : SENDING message ....
I use rabbitmq with rabbitmq-bundle and phpamqplib for async heavy logic processing. This works with rabbitmq consumers launched thanks to symfony commands (and rabbitmq-supervisord-bundle). These rabbitmq consumers log stuff to the common log file of the app (/var/log/env.log).
Problem:
I would like to add a unique_id to the log lines of the consummer processes (from symfony commands, as the processes are async by nature, and some async processes launch other async processes, log file is unreadable...). How to do so ? I've found a lot of doc on monolog handlers, but it seems that its not what I'm looking for. I also tried creating a fake request with my uniqueId header, pushed onto the stack_request (as in classic http trait), but this seems ugly and too faky.
Ideally, I'd like to do this, in my abstract GenericMyAppConsumer.php:
//Constructor of any consumers (sf command)
//...
$this->container->get('logger')->setData('extra.request_id', $this->uniqueId);
So it will be used for all the current thread logging.
I hope I'm clear. Thanks.