Symfony2/Monolog: Log Level - only show app.INFO?
Question
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
Solution
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.
OTHER TIPS
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.
you can user alert level. file: type: stream path: %kernel.logs_dir%/mylogfile.log level: alert
$logger = $this->get('logger'); // Log
$logger->alert('somelogcontent');
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).
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)
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