Alexander  Pravdin
Alexander Pravdin

Reputation: 5579

The `console` monolog handler doesn't work

The official documentation https://symfony.com/doc/current/logging/monolog_console.html suggests the following console logger configuration:

# config/packages/dev/monolog.yaml
monolog:
    handlers:
        # ...
        console:
            type:   console
            process_psr_3_messages: false
            channels: ['!event', '!doctrine', '!console']

But it contains the !console channel exclusion. Why? How does it work? According to the configuration, this handler will NOT accept messages from the console channel. What's the purpose of this handler then?

Also, even if I try the following config in my application:

monolog:
  channels:
    - console
  handlers:
    console:
      type: console
      level: debug
      channels: ~

And try to emit logs in a console command:

        $this->logger->info('Starting command');

I'm not getting ANY messages in the console output!

How does this handler work in general, and how to make it work in my case?

Upvotes: 0

Views: 21

Answers (1)

Jonathan
Jonathan

Reputation: 2073

To your 1st question

You may be confused about the difference between Channels and Handlers.

I suggest reading this part of the documentation for further clarification:
https://symfony.com/doc/current/logging/channels_handlers.html

In short:

  • The Channel is, simply put, where the message comes from
  • The Handler is, simply put, where the message goes to

The stated example from the documentation is, indeed, a bit confusing and I cannot imagine a case, where the combination of the Handler console with the exclusion of the channel !console would make sense.

To your 2nd question

How did you initialized your logger Object in the __construct?

Let's say you have the Channel named foo_bar. To log something to that channel, you need to initialize it like this:

public function __construct(LoggerInterface $fooBarLogger) {
   // ...
}

The naming conventions are documented very well and must be followed.

Every handler that includes the Channel foo_bar would then output the log result in the given file/stream/etc.


If you don't get any output in your console with your given configuration, it might be, that your verbosity levels are suppressing it.

Upvotes: 1

Related Questions