I want to setup Symfony2 to send me an email for critical errors, but just log error level errors. Will the following settings do that?
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: grouped
grouped:
type: group
members: [filelog, mail]
# log all errors to file
filelog:
type: fingers_crossed
action_level: error
handler: nested_stream
nested_stream:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
# send me an email when we have a critical error
mail:
type: fingers_crossed
action_level: critical
handler: buffered
buffered:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: %mailer_sender%
to_email: %error_email%
subject: "[FeedStream Error]"
level: debug
I saw: http://symfony.com/doc/current/cookbook/logging/monolog_email.html But it doesn't handle error at all, which is a case where I still want logs (but no email). I was pretty sure my config would work, but I don't know enough about the monolog settings. Please let me know if this is correct or if there is a better way.
The following is my production monolog config. This is confirmed working sending critical errors, whilst logging 'error' level and above to file. I've also split out the different channels to separate files. The other channels seem to produce errors far less than 'request', so it makes sense to split them out in production for me. Realise that's not your question, but hope it helps someone else; this can pared back to fit most requirements.
monolog:
handlers:
main:
level: error
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_remaining.log"
channels: ["!doctrine", "!request", "!security"]
request:
type: fingers_crossed
handler: requests
excluded_404s:
- ^/phpmyadmin
requests:
type: group
members: [request_critical, request_error]
request_critical:
level: critical
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_request_critical.log"
channels: [request]
request_error:
level: error
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_request.log"
channels: [request]
doctrine:
level: error
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_doctrine.log"
channels: [doctrine]
security:
level: error
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_security.log"
channels: [security]
mail:
type: fingers_crossed
action_level: critical
handler: buffered
buffered:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: aj.cerqueti#example.com
to_email: aj.cerqueti#example.com
subject: A critical error occurred
Related
Is it possible with Monolog in Symfony5 to write errors coming from PHP exceptions to a specific file when the current request has a specific pattern ?
What I would like to achieve would be something looking like this:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: grouped
grouped:
type: group
members: [all_except_backend, backend]
all_except_backend:
url: "!^/backend"
type: rotating_file
max_files: 10
path: %kernel.logs_dir%/%kernel.environment%.all.log
level: error
backend:
url: "^/backend"
type: rotating_file
max_files: 10
path: %kernel.logs_dir%/%kernel.environment%.backend.log
level: error
The Symfony documentation on "How to Configure Monolog to Email Errors" is describing how to combine two different logging targets (last example, scroll to bottom of doc).
# config/packages/prod/monolog.yaml
monolog:
handlers:
main:
type: fingers_crossed
action_level: critical
handler: grouped
grouped:
type: group
members: [streamed, deduplicated]
streamed:
type: stream
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
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
But in this example, the monolog.handlers.main.action_level defines the level of errors. How can I use different levels –in this case– for the stream and for the mailing logger?
In detail, I want to log all 400 and 500 errors (action_level error) to the stream, but I only want 500 errors (action_level critical) to be sent by mail.
i'm struggling with a monolog configuration for quite some time and hope you can help me.
I want my application to log
1) evrything from notice upward
2) but in case of an error all available logs (or if possible only for info upward all logs).
This is how i though it might work: collecting logs from fingers_crossed AND a stream, deduplicate them and send them to syslog
handlers:
filter_for_errors:
type: fingers_crossed
action_level: error
handler: unique
standard:
type: stream
level: notice
handler: unique
formatter: monolog.formatter.session_request
unique:
type: deduplication
handler: log2Syslog
log2Syslog:
type: syslog
facility: local5
ident: shop
formatter: monolog.formatter.session_request
level: info
Does one of you see my error or has deeper knowledge about what handlers can be combined and which not?
At the moment i see only error log lines.
You can try using a group handler.
# app/config/config_prod.yml
monolog:
handlers:
main:
type: fingers_crossed
action_level: critical
handler: grouped
grouped:
type: group
members: [streamed, deduplicated]
streamed:
type: stream
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
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
More info here: http://symfony.com/doc/current/logging/monolog_email.html
https://github.com/Seldaek/monolog/blob/master/doc/02-handlers-formatters-processors.md
GroupHandler: This handler groups other handlers. Every record
received is sent to all the handlers it is configured with.
I have a problem with the exclusion of sending mails for common 404 requests. I have setup the following in my config_prod.yml using Symfony 3.1.3 and Monolog 1.21.0, but it keeps sending me the emails when pages are requested. Do I miss something?
[EDIT]
In fact it's a known problem: https://github.com/symfony/monolog-bundle/issues/166
monolog:
use_microseconds: false
handlers:
main:
type: fingers_crossed
action_level: critical
handler: grouped
excluded_404s:
- ^/admin.php
- ^/administrator
- ^/blog
- ^/joomla
- ^/license
- ^/phpmyadmin
- ^/rss
- ^/sitemap
- ^/wordpress
- ^/wp-content
- ^/wp-login.php
- ^/xml
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: 'XXX#YYY.YY'
to_email: 'XXX#YYY.ZZ'
subject: An error has occured! [Monolog > config_prod.yml]
level: debug
it's been a while since I tried this, but it might be this setting. Can you try it and let us know the results:
swift:
type: swift_mailer
from_email: 'XXX#YYY.YY'
to_email: 'XXX#YYY.ZZ'
subject: An error has occured! [Monolog > config_prod.yml]
level: error
So change the "level" from "debug" to "error".
Not certain that will work, but try it.
I'm using Monolog with Symfony2 and have configured a logging environment where everything is logged to file, and above a certain threshold are e-mailed to me. My config is below.
However, I have not been able to adjust the e-mail subject so that it changes based on the actual level of the log. The difference in response time, for say, a warning and a critical, would probably be different. Is there a way to do this?
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
mail:
type: fingers_crossed
action_level: %logger_level%
handler: buffered
buffered:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: %logger_from_email%
to_email: %logger_to_email%
subject: Log # I want the subject to include the log level somehow
level: debug
You can define two different handlers (or handler chains): one is activated by low level problems and the other by more serious issues. They have different subjects for the email they send. Something like:
monolog:
handlers:
mail_critical:
type: fingers_crossed
action_level: critical
handler: buffered_critical
bubble: false
buffered_critical:
type: buffer
handler: swift_critical
swift_critical:
type: swift_mailer
from_email: %logger_from_email%
to_email: %logger_to_email%
subject: CALL 911, YOUR SITE IS BURNING
level: critical
mail_debug:
type: fingers_crossed
action_level: debug
handler: buffered_debug
buffered_debug:
type: buffer
handler: swift_debug
swift_debug:
type: swift_mailer
from_email: %logger_from_email%
to_email: %logger_to_email%
subject: Just a normal error
level: debug
You can include %%level%% in the subject. It will add int.
Or you can add %%level_name%% it will add string of leg level name.
swift:
type: swift_mailer
from_email: '%env(ADMIN_EMAIL)%'
to_email: '%env(ADMIN_EMAIL)%'
subject: '[%kernel.environment%] [LEVEL CODE:%%level%%][LEVEL NAME:%%level_name%%] Some subject text! %%message%%'
level: error
formatter: monolog.formatter.html
content_type: text/html
More can be found here https://github.com/Seldaek/monolog/blob/master/doc/message-structure.md