8

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

Mike
  • 2,686
  • 7
  • 44
  • 61

6 Answers6

10

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.

Anton Babenko
  • 6,586
  • 2
  • 36
  • 44
  • What if you want to level up the 'my_logger' handler? i.e., imagine you set all across your controllers different level of messages. Using several of the info, alert, err methods. How do you tell the configuration in certain moment just to write in the log those with warning or higher relevance (alert or err) and not writing the info and warning ones? Any chance to do that? – ElPiter Jun 06 '13 at 20:05
  • 3
    Not sure if I understand the question correctly, but I will try to answer. You have to change argument in the handler (now it is `100`) to another number (say, '200' to log less). Check real values in `Monolog\Monolog`. Doing so your code call info/debug/error methods as usual, but only thoso with value 200 and higher will be logged. – Anton Babenko Jun 06 '13 at 22:09
  • 1
    You understood perfectly my question and actually solved my issue. To be honest, I didn't know what that parameter was for. Ashame. Thanks a lot – ElPiter Jun 07 '13 at 07:07
6

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.

Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
2

you can user alert level. file: type: stream path: %kernel.logs_dir%/mylogfile.log level: alert

$logger = $this->get('logger'); // Log
$logger->alert('somelogcontent');
lovesmzh
  • 37
  • 1
2

This log message comes from the router_listener service. You can re-define it in services configuration file.

What I've done in my main bundle config/services.yml :

services:

    # ...

    router_listener:
        class:  %router_listener.class%
        arguments: ['@router', %request_listener.http_port%, %request_listener.https_port%]
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onEarlyKernelRequest }
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

It makes "Matched route ..." log messages not to be logged (as RouterListener doesn't have a logger service in its constructor arguments).

AlterPHP
  • 12,667
  • 5
  • 49
  • 54
  • That is actually pretty cool. I just looked up my old post while I was running into the same issue again. ;) Many Thanks! – Mike Aug 07 '12 at 09:11
0

Add in your config\packages\monolog.yaml file the "app" spécific channel

monolog:
    handlers:
        info:
            type:  rotating_file
            path:  '%kernel.logs_dir%/info_%kernel.environment%_info.log'
            level: info
            max_files: 10
            channels: app

I use symfony 4 (or 5)

Bruno
  • 31
  • 3
0

Add in you monolog configuration for example if i use handler:test

test:
    type: filter
    accepted_levels: [info]
    handler: test_info
test_info:
    type: stream // many possibilities(rotating_file..)
    level: info
    path: // the path where you save file log