No logging in production environment when running Console commands - symfony

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"]

Related

Combination of monolog handlers seems not to work

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.

Symfony 3.1 monolog excluded 404

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.

The service definition "monolog.logger.event" does not exist

I have created different channels for my monolog, while working with my app (dev and test environment) everything is fine, creates and writes into all logs but when execute my tests (unit test) I am getting the following error: "The service definition "monolog.logger.event" does not exist"
I've dump my "ContainerBuilder $container" in "vendor/symfony/monolog-bundle/DependencyInjection/Compiler/LoggerChannelPass.php" and for some reason the monolog.logger.event does not exists, all the rest of my channels do exists: doctrine, request, security, etc...
I am pasting my monolog config corresponding to the channels:
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ['!event', '!snc_redis', '!doctrine', '!request', '!security']
event:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_event.log"
level: debug
channels: ['event']
snc_redis:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_redis.log"
level: debug
channels: ['snc_redis']
doctrine:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_doctrine.log"
level: debug
channels: ['doctrine']
request:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_request.log"
level: debug
channels: ['request']
security:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_security.log"
level: debug
channels: ['security']
Any clue how can I fix this
It is happens because web/app_test.php and console have different debug flag, for example:
#web/app_test.php
$kernel = new AppKernel('test', false);
#bin(app)/console
$kernel = new AppKernel('test', true);
In this situation web application cannot find the right cache container file.

Using monolog loggly handler in Symfony

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

Symfony2 Monolog Settings for email and file logging

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

Resources