I want to write my application logs in another file than the one Symfony2 writes its own logs and system logs. I understood that I needed to create a service of my own like this :
services:
actionslogger:
class: Symfony\Bridge\Monolog\Logger
arguments: [app]
calls:
- [pushHandler, [@actionslogger_handler]]
actionslogger_handler:
class: Monolog\Handler\StreamHandler
arguments: [%kernel.logs_dir%/actions_%kernel.environment%.log, 200]
That works fine when I use $logger = $this->get(‘actionslogger’); in my application, so that’s ok.
But I also want to use a Formatter and a Processor to manage the way my logs are written. To do that, I use this configuration :
services:
actionslogger.formatter.session_request:
class: Monolog\Formatter\LineFormatter
arguments:
- "[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%%\n"
actionslogger.processor.session_request:
class: My\Bundle\LogProcessor
arguments: [ @session ]
tags:
- { name: actionslogger.processor, method: processRecord }
I can use this Formatter and Processor with Symfony2 default logger with this config:
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
formatter: actionslogger.formatter.session_request
But if I can use the Formatter with my own logger, I can’t use the Processor. Here’s my config:
services:
actionslogger.formatter.session_request:
class: Monolog\Formatter\LineFormatter
arguments:
- "[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%%\n"
actionslogger.processor.session_request:
class: My\Bundle\LogProcessor
arguments: [ @session ]
tags:
- { name: actionslogger.processor, channel: app, method: processRecord, handler: @actionslogger_handler }
actionslogger:
class: Symfony\Bridge\Monolog\Logger
arguments: [app]
calls:
- [pushHandler, [@actionslogger_handler]]
actionslogger_handler:
class: Monolog\Handler\StreamHandler
arguments: [%kernel.logs_dir%/actions_%kernel.environment%.log, 200]
calls:
#- [pushProcessor, [???]]
- [setFormatter, [@actionslogger.formatter.session_request]]
The tags channel and handler in the Processor’s config seems useless.
What can I do to make the Processor work with my logger?
What should I pass to the pushProcessor method in the commented line (if that could be a valid option)?
Thanks for the help.
Note: using Symfony 2.0.0
Answer to myself:
Well, I couldn’t find how to set this, so I ended up with this not-so-heavy solution :
I create a new logger, that is quite simple, and affect it my brand new Processor in the constructor, so the files are in My/Bundle and like this:
(That could even come in handy if I want to modify the addRecord method of Logger later)
Then I create a new service like this:
And here I go. I can now use my new logging service, with my Formatter /and/ my Processor, and do what I want with them (here: adding session id and log context information in an easy-to-use format).
Anyway, if someone has the answer for my 1st question (about how to link the processor on a personal logging service, directly in the config), please post here.