How to define channels and Levels in monolog logging in symfony2

15,756

Solution 1

With version 2.4 and up (beware, the release cycle of the MonologBundle is not syncronized with symfony anymore) of the MonologBundle, you can now define new channels very simple via configuration, without defining services.

monolog:
    channels: ["my_channel"]

Now simply get the automatically created logger for the new channel in your controller:

$logger = $this->get('monolog.logger.my_channel');
$logger->info('somelogcontent');

The message level is defined trough the use of the appropriate method. Take a look into the LoggerInterface to see all logging methods (which are indeed implemented by monolog). Here some levels to be mentioned:

$logger->info('Info message for interesting things');
$logger->warning('Some application warnings, but the application works');
$logger->error('Error which can influence the application flow/output');

I know old question, but this new feature from the MonologBundle ~2.4 should be mentioned.

Solution 2

According to the Monolog documentation, channels are tied to loggers, basically the logger's name is your channel. That means you can't specify another channel in a given logger but you can create new loggers with different channels.

Levels are just numbers and constants, so you can create your own. With the right formatter, you'll get the string corresponding to your custom level.

Share:
15,756
Mirage
Author by

Mirage

Updated on June 12, 2022

Comments

  • Mirage
    Mirage almost 2 years

    Is there any way that i can define my custom levels in monolog in symfony2.

    I i do this

    $logger->err('An error occurred');

    Then in the database i have this added.

    The channel is app and level is 500

    Is there any way to do this

    $logger->log("message",(channel),(level)
    $logger->log("Object with is 212 deleted",'DELETE',NORMAL);
    

    So that i can have separate things in database for reporting and viewing