Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2/Monolog: Log Level - only show app.INFO?

I am using Symfony2 and monolog to write in specific logs in a defined logfile (mylogfile.log):

#config_dev.yml
monolog:
    handlers:
        main:
           type:  stream
           path:  %kernel.logs_dir%/%kernel.environment%.log
           level: debug
       file:
           type:   stream
           path:   %kernel.logs_dir%/mylogfile.log
           level:  info

I am accessing the logfile in my controller via:

    $logger = $this->get('logger'); // Log
    $logger->info('somelogcontent');

Now my issue is that my log file contains the whole info level, meaning it gives me all app.INFO logs (which is what I want) and request.INFO (which I don't really need):

[2012-04-04 11:13:17] request.INFO: Matched route ... blablabla
[2012-04-04 11:13:17] app.INFO: somelogcontent
...

Is there any way not to log the Request.INFO?

Mike

like image 534
Mike Avatar asked Apr 04 '12 09:04

Mike


2 Answers

You have to make a new logger service, which should be used it in your classes. Like this, config.yml:

services:
  my_logger:
    class: Monolog\Logger
    arguments: [my_info]
    calls:
        - [pushHandler, [@my_log_handler]]

  my_log_handler:
    class: Monolog\Handler\StreamHandler
    arguments: [%kernel.root_dir%/logs/my_info.log, 100]

Usage (in Controller, for example):

$this->get('my_logger')->info('info message');

More detailed information in symfony cookbook.

like image 86
Anton Babenko Avatar answered Nov 16 '22 07:11

Anton Babenko


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"]
    handlers:
       file:
           type:   stream
           path:   %kernel.logs_dir%/mylogfile.log
           level:  info
           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');

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

like image 30
Emii Khaos Avatar answered Nov 16 '22 08:11

Emii Khaos