I'm trying to use the Monolog Loggly handler in Symfony2 but can't get it to work:
# config.yml
monolog:
handlers:
main:
type: stream
token: my-loggly-token
level: info
handler: loggly
tag: symfony-app
I've removed the monolog entries from config_dev.yml and config_prod.yml so the the setup above isn't being overridden. Inside a controller I'm trying to trigger an info event, but it's not appearing in Loggly. Where am I going wrong?
You should use type: loggly instead of stream.
# config.yml
monolog:
handlers:
main:
type: loggly
token: my-loggly-token
level: info
tag: symfony-app
Related
In the config below I am supposed to not log anything to rollbar, since I have commented out the rollbar handler from the group handler config. Still, Rollbar is receiving logs. It seems as if it is still using the rollbar handler, even though I have tried to stop log events from reaching it. How?
monolog:
handlers:
main:
type: fingers_crossed
action_level: debug
excluded_404s:
- ^/
handler: buffered
buffered:
type: buffer
handler: grouped
# stop propagation
bubble: false
rollbar:
type: service
id: Rollbar\Monolog\Handler\RollbarHandler
cloudwatch:
type: service
id: cloudwatch_handler
grouped:
type: group
members:
- local
- cloudwatch
# - rollbar
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
When running Console commands in prod mode, I need to log Doctrine related debug messages. Everything works fine in dev with the following configuration, so I assume I forgot something to set when in prod?
My system:
PHP 7.3
Symfony 4.4
Monolog
Doctrine
How do I run commands:
I run commands in prod as either
php bin/console app:scrape --env=prod
or
# set APP_ENV=prod in .env.local before
php bin/console app:scrape
Both result in no logs. I am sure, I run prod, because Symfony creates var/cache/prod every time.
Monolog configuration file: config/package/prod/monolog.yaml
This file configures Monolog in prod environment.
monolog:
handlers:
main:
type: fingers_crossed
action_level: debug
handler: nested
excluded_http_codes: [404, 405]
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
deprecation:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.deprecations.log"
deprecation_filter:
type: filter
handler: deprecation
max_level: info
channels: ["php"]
doctrine:
level: debug
type: stream
path: "%kernel.logs_dir%/doctrine/info.log"
channels: ["doctrine"]
Output of APP_ENV=prod bin/console debug:config monolog:
https://gist.github.com/k00ni/419f62941e496a376be35a0d06e44131
Maybe you could have a main handler that is grouped so that it will pass messages with both handlers (your current main and doctrine):
# config/packages/monolog.yaml
monolog:
handlers:
main:
type: group
members: ["doctrine", "default"]
doctrine:
level: debug
type: stream
path: "%kernel.logs_dir%/doctrine/info.log"
channels: ["doctrine"]
# config/package/prod/monolog.yaml
monolog:
handlers:
default: # formerly main
type: fingers_crossed
action_level: error
handler: nested
excluded_http_codes: [404, 405]
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
deprecation:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.deprecations.log"
deprecation_filter:
type: filter
handler: deprecation
max_level: info
channels: ["php"]
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 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