under app/config/config_dev.yml I have:
monolog:
handlers:
main:
type: stream
path: /var/log/dev.log
level: debug
and under app/config/config_prod.yml I have:
monolog:
handlers:
syslog:
type: stream
path: /var/log/prod.log
level: error
In my controller I have
$logger = $this->get('logger');
$logger->debug("foo");
I would expect to see the debug entry while in dev and only error and above entry when in prod.
But so far it's outputting everything. How can I adjust it so that different levels exists for different environments?
Related
I am new to Symfony 5.3 and Monolog. The docu explains "the Symfony Framework organizes log messages into channels. By default, there are several channels, including doctrine, event, security, request and more."
Is there any way to find out what channels exactly are configured?
I tried to run php bin/console debug:config monolog but this only shows the channels I configured (e.g. by adding channels: ['myChannelA', 'channelB'] to the monolog.yaml)
I made no changes (beside adding custom channels, see above) to the default monolog config which was created on install:
// config/packages/dev/monolog.yaml
monolog:
channels: ['myChannelA','channelB']
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!event"]
# uncomment to get logging in your browser
# you may have to allow bigger header sizes in your Web server configuration
#firephp:
# type: firephp
# level: info
#chromephp:
# type: chromephp
# level: info
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine", "!console"]
// config/packages/prod/monolog.yaml
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
excluded_http_codes: [404, 405]
buffer_size: 50 # How many messages should be saved? Prevent memory leaks
nested:
type: stream
path: php://stderr
level: debug
formatter: monolog.formatter.json
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine"]
You can try the following command:
php bin/console debug:autowiring | grep monolog
I wrote a very simple test command which has LoggerInterface injected in its constructor.
How am I suppose to change the monolog.yaml configuration to save this logger output to both log file and to output it to console?
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
formatter: monolog.line.formatter
handler: terminal
excluded_http_codes: [404, 405]
buffer_size: 50 # How many messages should be saved? Prevent memory leaks
terminal:
type: stream
path: "php://stderr"
level: debug
console:
type: console
process_psr_3_messages: false
channels: [ "!event", "!doctrine" ]
The commands will always stderr by default (if you specify the -vvv option)
If you need to write the logs in a file only on error (with the stack error) you can use the finger_crossed handler :
handlers:
main:
# fingers_crossed allow to log only if action_level defined is reach
type: fingers_crossed
# minimum level to activate the handler
# available level (emergency|alert|critical|error|warning|notice|info|debug){1}
action_level: error
# wrapped handler's name
handler: nested
nested:
# stream allow to write log in file
type: stream
# path to the log file
path: "%kernel.logs_dir%/%project_name%_%kernel.environment%.log"
# available level (emergency|alert|critical|error|warning|notice|info|debug){1}
level: debug
If you want to filter a bit the logs shown in the stderr you can use the default config for the console :
console:
type: console
process_psr_3_messages: false
channels: ['!event', '!doctrine', '!console']
I'll allow you to have nicer console logs (and avoid too many "useless" logs such as event or doctrine which are very verbose)
You can use the "group" handler https://github.com/symfony/monolog-bundle/blob/master/DependencyInjection/Configuration.php#L137
Check an implementation here https://symfony.com/doc/current/logging/monolog_email.html
You can do this like your terminal handler does. copy the terminal handler and set the path to the file. Both handlers will be executed.
monolog:
handlers:
main:
...
terminal:
...
terminal_file:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%-terminal.log"
level: debug
You can also do this to log different channels to different files.
Sometimes i use this, to log doctrine debug messages to a different file, by adding the doctrine channel.
monolog:
handlers:
...
doctrine_debug:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%-doctrine.log"
level: debug
channels: ["doctrine"]
I am working on a web app built using Symfony 2.6 and there are different configuration inside app/config folder. How do I know which one is being used.
Inside app/config I see config.yml config_dev.yml config_prod.yml and the monolog entry I see in config_dev.yml and also in monolog_prod.yml. It is as below.
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
Now I want to use monolog to input some logs in a controller and put those logs separate form other logs. How can this be done?.
You know which one is used on your environment.
If you are in dev mode you are using config_dev.yml merged with config.yml
You can check it into your virtual host if the file is on app.php you are in prod mode, if the file points to app_dev.php you are in dev mode usually
To log into other files you can create a channel like this:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
your_handler:
level: debug
type: stream
path: '%kernel.logs_dir%/custom.log'
channels: ['your_channel']
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
And into your controller you can call it in this way:
$logger = $this->get('monolog.logger.your_handler');
$logger->debug('your custom message into your custom log file');
Welcome,
My task is to change the convention of loggers in the application. Currently, loggers are configured as dedicated services. I would like to define the loggers in the Symfony convention and ensure backward compatibility by defining DI directing the old names of logger services to the new ones in the monolog.logger.channel convention. I have the loggers in Symfony do things for the first time or can anyone give me an example of how to do it? Thank you in advance and I greet you below with my current configuration:
app/config/config_dev.yml:
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ['!my_channel']
my_handler:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.my_handler.log"
level: debug
channels: ['my_channel']
console:
type: console
bubble: false
app/config/config_prod.yml:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
channels: ['!my_channel']
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
my_channel:
type: fingers_crossed
action_level: error
handler: my_channel_stream
my_channel_stream:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.my_channel.log"
level: debug
channels: ['my_channel']
src/bundle/Resources/config/logger.yml:
services:
logger:
class: Symfony\Bridge\Monolog\Logger
arguments: [logger]
calls:
- [pushHandler, ['#monolog.handler.console']]
- [pushHandler, ['#logger_handler']]
logger_handler:
class: Monolog\Handler\StreamHandler
arguments: ["%kernel.logs_dir%/%kernel.environment%.cof.log", 200]
src/bundle/Resources/config/config.yml:
monolog:
channels: ['my_channel']
I wanto to override some configurations from config_dev.yml in my config_test.yml. So, imagine the following part in the config_dev.yml:
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
firephp:
type: firephp
level: info
In my test environment, I want no logger at all. So I tried
monolog: ~
with no effect. I also tried:
monolog:
handlers:
main: ~
firephp: ~
again without any effect. Then I tested
monolog:
handlers:
main:
type: ~
path: ~
level: ~
firephp:
type: ~
level: ~
and I get a ErrorException Couldn't find constant Monolog\Logger::. If anybody could point out a way to override the monolog settings I would very much appreciate it. Thanks!
It's better to define handlers as empty array:
monolog:
handlers: []
UPD1: There are special type of loggers: test and null, you can use them:
monolog:
handlers:
test:
type: test
level: debug
If you're using the Symfony2 Standard Edition
Your config_dev.yml looks something like this for monolog out of the box:
# config_dev.yml
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
nested:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
As you can see this defines the handlers main and nested where nested is only used because it's referenced by main.
config_dev.yml is imported from config_test.yml so
if you want to override the configuration for your test environment you need to override the mainhandler in config_test.yml:
# config_text.yml
monolog:
handlers:
main:
type: test
This will stop monolog from creating a log file.
Have you tried:
monolog:
handlers: ~
It should work (I think).
Look here
Without handlers, monolog is not load.