0

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.

bohr
  • 1,206
  • 1
  • 9
  • 11
  • Monolog has a UidProcessor, which can be attached to the loggers in the Symfony config - https://symfony.com/doc/current/logging/processors.html – Alister Bulman Oct 27 '17 at 15:21
  • Yep, but it's taken from the session, which I have not in sf command exec context. Plus, I wanted to be able to control generation of that Id and I wanted it to be set the soonest possible in the SF request exec plan. – bohr Oct 27 '17 at 15:25
  • You can write your own Processor to control what is put in it,but you only need it to be generated right before the first log event is written – Alister Bulman Oct 27 '17 at 15:40
  • I do have that already. This is where I fetch the uuid from the header of the current request in the request stack. I.e: //Unique Request Id if ($request && $request->headers->has('X-MYAPI-REQUEST-ID')) { $record['extra']['request_id'] = $request->headers->get('X-MYAPI-REQUEST-ID'); } – bohr Oct 27 '17 at 15:52

1 Answers1

1

OK, I ended up setting the current request directly in the Monolog Processor which is container aware, if there isnt any request Id in the request header. Thats not really safe, but I cant see other ways.

bohr
  • 1,206
  • 1
  • 9
  • 11