Question

Sorry for this Thread, but I really can't figure out, how to configure Symfony2 to do what I like it to do :-) I just don't get the 'big picture'.

I would like to create an own log-channel for some central things my bundle. This Channel should normally be written to an own file in dev, for prod it should be written to prod.log or be supressed (one of it or switchable by config).

I got the feeling, that the solution is shared over many docs and tutorials.

  1. Where and how do I define that channel?
  2. The handler is defined in the config.yml, right?
  3. Do I have to implement an own handler-class?

Sorry for this very unspecific question.

Bonus-question: 4. When logging should be possible outside of a controller (access to container), than I have to implement a service, right?

Was it helpful?

Solution

OK. My fault was a mixture of misunderstanding and being noob. After reading the Service-Doc and compare this thread to that (How to write logs from one service into separate file?) I got it working:

I like this, because it's just configuring given classes instead of implementing it's own:

config.yml:

services:
    myOwnLogger:
        class:     Symfony\Bridge\Monolog\Logger
        arguments: [pushHandler, [@myOwnLogHandler]]

    myOwnLogHandler:
        class:     Monolog\Handler\StreamHandler
        arguments: [%kernel.logs_dir%/%kernel.environment%.myOwnChannel.log, 200] #200 = INFO

Egads, that was so easy, please excuse me not understanding that 'other' thread.

OTHER TIPS

You can see Switching a channel to a different handler to understand how to configure it to log to different log files (in config_dev.yml if you want it only in development).

To create a new channel (by default, the logger service uses the app channel), you need to inject it in a service using the monolog.logger tag. Whenever you send logs to that logger instance, it will go to the handlers configured for this channel.

The @logger in your service definition will be replaced by the proper logger instance for the channel you required. If you do not add the custom tag, then @logger stays as it is and you get the default (app) channel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top