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.
Related
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 having trouble setting up Monolog to use Swiftmailer and the Html formatter. I have multiple monolog handlers and swiftmail mailers which might be the issue. Here's my config.yml
Monolog section:
services:
monolog.formatter.html:
class: Monolog\Formatter\HtmlFormatter
monolog:
channels: ["orders", "support"]
handlers:
# Called
orders:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_orders.log"
level: info
channels: orders
# Called
support:
type: fingers_crossed
action_level: critical
handler: support_grouped
channels: support
# Not called, referenced in a handler [support]
support_grouped:
type: group
members: [support_log, environment_log, support_buffered]
# Not called, referenced in a group handler [grouped_support]
support_buffered:
type: buffer
handler: support_buffered_grouped
# Not called, referenced in a handler [support_buffered]
support_buffered_grouped:
type: group
members: [hipchat, webmaster_email, support_email]
# Not called, referenced in a group handler [support_buffered_grouped]
support_email:
type: swift_mailer
mailer: swiftmailer.mailer.errors
from_email: %webmaster_email_address%
to_email: %support_email_address%
subject: '[Costimator] Support: An Error Occurred!'
level: debug
content_type: text/html
formatter: monolog.formatter.html
# Called
webmaster:
type: fingers_crossed
action_level: critical
handler: webmaster_grouped
# Not called, referenced in a handler [webmaster]
webmaster_grouped:
type: group
members: [environment_log, webmaster_buffered]
# Not called, referenced in a handler [webmaster_grouped]
webmaster_buffered:
type: buffer
handler: webmaster_buffered_grouped
# Not called, referenced in a handler [webmaster_buffered_grouped]
webmaster_buffered_grouped:
type: group
members: [hipchat, webmaster_email]
# Not called, referenced in a group handler [webmaster_buffered_grouped, support_buffered_grouped]
webmaster_email:
type: swift_mailer
mailer: swiftmailer.mailer.errors
content_type: text/html
from_email: %webmaster_email_address%
to_email: %webmaster_email_address%
subject: '[Costimator] An Error Occurred!'
level: debug
formatter: monolog.formatter.html
# Not called, referenced in a group handler [support_grouped, webmaster_grouped]
environment_log:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
# Not called, referenced in a group handler [support_grouped, webmaster_grouped]
support_log:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.support_log"
level: debug
# Not called, referenced in a group handler [webmaster_buffered_grouped, support_buffered_grouped]
hipchat:
type: hipchat
notify: true
token: xxx
room: xxx
level: critical
Swiftmailer section:
# Swiftmailer Configuration
swiftmailer:
default_mailer: %mailer_default_config%
mailers:
default:
delivery_address: "%mailer_delivery_address%"
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool:
type: "%mailer_spool_type%"
path: "%kernel.root_dir%/spool"
errors:
delivery_address: "%mailer_errors_delivery_address%"
transport: "%mailer_errors_transport%"
host: "%mailer_errors_host%"
username: "%mailer_errors_user%"
password: "%mailer_errors_password%"
spool:
type: "%mailer_errors_spool_type%"
path: "%kernel.root_dir%/spool"
Errors are being sent as text/html but without the table formatting. Any ideas?
// Ping #seldaek
Edit Formatters seem to be set on the handlers:
The configuration above actually works correctly. Not sure what was going on.
Leaving this here for anyone else who wants to implement HTML formatting.
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
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