In monolog you can exclude path so that when an exception happens it doesn't get logged into prod.log
I wanted to exclude all logs from /items/* so I did the following:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: file
channels: ["!doctrine", "!event", "!security", "!request"]
excluded_404s:
- ^/items/
I wanted to exclude errors from /items/19283712 and anything that has /items/WHATEVERCRAPINHERE . The documentation shown here details on how to do it. I've tried the regex above and it doesn't seem to work. Why is this?
You can use the following syntax ^/items/*
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: file
channels: ["!doctrine", "!event", "!security", "!request"]
excluded_404s:
- ^/items/*
Related
I have 3 web domains (first.com, second.com, third.com) and I'm trying to have separate log files for each of these domains.
I'm using php7.4, Symfony4 and Monolog, I managed to have the 3 log files, but so far every attempt I made just modified every log file, no matter which domain I was on.
I tried to use the host and url options, but it kept modifying all 3 files at the same time.
Here is my monolog.yaml file with different examples of what I tried :
monolog:
handlers:
main_first:
host: "first.com"
type: fingers_crossed
action_level: error
handler: first
excluded_404s:
# regex: exclude all 404 errors from the logs
- ^/
first:
type: rotating_file
path: "%kernel.logs_dir%/first/%kernel.environment%.log"
level: debug
max_files: 30
main_second:
url: "second.com"
type: fingers_crossed
action_level: error
handler: second
excluded_404s:
# regex: exclude all 404 errors from the logs
- ^/
second:
type: rotating_file
path: "%kernel.logs_dir%/second/%kernel.environment%.log"
level: debug
max_files: 30
main_third:
type: fingers_crossed
action_level: error
handler: third
excluded_404s:
# regex: exclude all 404 errors from the logs
- ^/
third:
type: rotating_file
path: "%kernel.logs_dir%/third/%kernel.environment%.log"
level: debug
max_files: 30
Is there any way to tell Monolog to modify a specific file depending on the domain of the request ?
If not, how can I manage to do so without Monolog ?
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');
In Symfony 3 is there anyway i can write all errors to logs on production without setting the debug mode on? Errors would include http 500 errors or application errors or the php errors which are silenced due to the error flag set to false on production.
The current log config for production is
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: info
channels: [!request, !event, !translation, !kernel, !security, !php, !snc_redis]
php:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_php.log"
level: warning
channels: [php]
You could use the default production settings (taken from the monolog-bundle recipe) for this:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
excluded_404s:
# regex: exclude all 404 errors from the logs
- ^/
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
What happens with this configuration is that Monolog buffers all messages during a request, but only writes them to the log when an error appears. This way you will not "flood" your logs with noisy debug and info messages all the time. You only get full log information when there was an error during a request.
I use Symfony 2.4 with monolog and have monolog configuration:
monolog:
channels: ["voter", "worker"]
handlers:
# 'critical' means HTTP 5XX responses.
critical:
type: fingers_crossed
action_level: critical
handler: critical_group
# 'error' means HTTP 4XX responses.
error:
type: fingers_crossed
action_level: error
handler: error_stream
debug:
type: fingers_crossed
action_level: debug
handler: debug_stream
critical_group:
type: group
members: critical_stream
critical_stream:
type: stream
path: %kernel.logs_dir%/%kernel.environment%_critical.log
level: critical
error_stream:
type: stream
path: %kernel.logs_dir%/%kernel.environment%_error.log
level: error
debug_stream:
type: stream
path: %kernel.logs_dir%/%kernel.environment%_warning.log
level: debug
I want to exclude warning messages from debug messages. And warning and debug messages from error messages for cleaner logs. Is any solution for this?
Thanks for any suggestions!
You can use the filter handler.
Here an additional handler based on your config:
handlers:
cleaned_debug:
type: filter
handler: debug
accepted_levels: [DEBUG]
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.