Symfony2 monolog configuration: exclude warning messages from debug messages - php

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]

Related

Monolog: Send error logs by mail causes an extra mail for every error sent

For every error log sent by mail we get another extra email with 2 lines of smtp debug log messages.
Newly created symfony demo (symfony new --demo)
Configured monolog to send error messages by mail as in the docs
symfony/monolog-bundle 3.7.0
Symfony Versions 5.2.1 / 5.2.9 / 5.3.0-RC1,
PHP 7.4.13
# config/packages/prod/monolog.yaml
monolog:
handlers:
main:
type: fingers_crossed
action_level: critical
handler: deduplicated
deduplicated:
type: deduplication
handler: symfony_mailer
symfony_mailer:
type: symfony_mailer
from_email: 'error#example.com'
to_email: 'error#example.com'
subject: 'An Error Occurred! %%message%%'
level: debug
Monolog sends error messages as expected, but every mail is followed by a second one with this content:
[2021-05-26T10:49:47.683298+02:00] app.DEBUG: Email transport "Symfony\Component\Mailer\Transport\Smtp\SmtpTransport" stopping [] []
[2021-05-26T10:49:47.722980+02:00] app.DEBUG: Email transport "Symfony\Component\Mailer\Transport\Smtp\SmtpTransport" stopped [] []
no extra mail in dev or test environments
no extra mail when setting the framework config test: true in config/framework.yaml
Any ideas how to get rid of this extra email in production mode?
PS: There's also an open issue in the Symfony MonologBundle.
As mentioned in the linked GitHub issue by #raziel057, a workaround would be to exclude the "mailer" channel.
https://github.com/symfony/monolog-bundle/issues/405#issuecomment-1069040234
monolog:
handlers:
main:
type: fingers_crossed
action_level: critical
handler: grouped
channels: ["!mailer"]
grouped:
type: group
members: [streamed, deduplicated]
streamed:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
deduplicated:
type: deduplication
handler: symfony_mailer
symfony_mailer:
type: symfony_mailer
from_email: "%mailer_app_sender%"
to_email: "%mailer_team_receiver%"
subject: "An Error Occurred!"
level: debug

Symfony "deduplication" log hanlder not found

I am using sample config from symfony documentation page to send logs via email https://symfony.com/doc/2.8/logging/monolog_email.html
monolog:
handlers:
main:
type: fingers_crossed
# 500 errors are logged at the critical level
action_level: critical
# to also log 400 level errors (but not 404's):
# action_level: error
# excluded_404s:
# - ^/
handler: deduplicated
deduplicated:
type: deduplication
handler: swift
swift:
type: swift_mailer
from_email: 'error#example.com'
to_email: 'error#example.com'
subject: 'An Error Occurred! %%message%%'
level: debug
formatter: monolog.formatter.html
content_type: text/html
I am getting an error
Invalid handler type "deduplication" given for handler "deduplicated"
I am not sure what's wrong as i am doing exactly same as in documentation, or i have to configure something.
Related to the Github-Repository there is indeed no option "deduplication" for the type.
https://github.com/symfony/monolog-bundle/blob/2.8.1/DependencyInjection/MonologExtension.php
Just upgrade your version via composer.

Symfony production logs

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.

How to email only exceptions using monolog (symfony2)

I am using Monolog in a Symfony2 and now it's configured in this way:
services:
monolog.formatter.html:
class: Monolog\Formatter\HtmlFormatter
monolog:
handlers:
main:
type: fingers_crossed
action_level: critical
handler: swift
handler: grouped
grouped:
type: group
members: [streamed, buffered]
streamed:
type: stream
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
buffered:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: 'email#gmail.com'
to_email: 'email#gmail.com'
subject: An Error Occurred!
level: debug
content_type: text/html
formatter: monolog.formatter.html
And now it send me information about all events on my site. How can I configure it, to receive only messages about exceptions?
Additionly, most exceptions that i should catch and email came from guzzle library. Something like ( Client error response )

configure a regex path in yaml to exclude exceptions

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/*

Categories