Write symfony command log output to console AND FILE - symfony

The setup is thus:
I have a command, which calls a function on service X which uses logger Y.
Currently, when run from the console, the log messages are output to the console (using the -v parameters). I would also like to output those messages to a file, regardless of the -v level.
Edit: Adding monolog config, as requested (though there's nothing interesting - it's the default).
monolog:
channels: ['amz_update']
handlers:
main:
type: stream
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: emergency
channels: ['!event']
console:
type: console
process_psr_3_messages: false
channels: ['!event', '!doctrine', '!console']
# To follow logs in real time, execute the following command:
# `bin/console server:log -vv`
server_log:
type: server_log
process_psr_3_messages: false
host: 127.0.0.1:9911

Related

Symfony 3 Console exception logging only displayed on console

In our Symfony 3 app all logging from console commands (e.g. when there's an exception) are nicely formatted and displayed to the screen... which when dealing with cron jobs in production mode is completely useless.
All exception logging from HTTP requests are routed through log files on the server and reports sent to us via Swifmail (as per https://symfony.com/doc/3.4/logging/monolog_email.html)
I need to get logs from console commands treated the same as logs from HTTP requests but the Symfony documentation seems to ignore this scenario?
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
level: debug
formatter: monolog.formatter.html
content_type: text/html
console:
type: console
process_psr_3_messages: false
Any ideas?????
-- Edit --
It turns out that Symfony logs console exceptions at Monolog's error(400) level but we have monolog.handlers.main.action_level set to critical(500).
So, it seems, that I can either patch Symfony\Component\Console\EventListener\ErrorListener to call $this->logger->addCritical instead of $this->logger->error or take the config level down from critical to error and put up with getting error reports for all the HTTP 4?? errors too.

How to set Monolog log level per channel in Symfony

I'm using Symfony with Monolog to log data to loggly.com. My symfony app uses following configuration:
loggly:
type: loggly
token: ...
level: INFO
bubble: true
channels: ["app", "request"]
As you can see, I'm logging the channels app and request. The minimum log level is INFO for both channels.
Now i would like to distinguish the log level per channel like this:
Channel "app": INFO (and above)
Channel "request": ERROR (and above)
Is there a way to adjust my configuration or do I have to solve this programatically?
Thanks in advance
ninsky
loggly_app:
type: loggly
token: ...
level: INFO
bubble: true
channels: ["app"]
loggly_request:
type: loggly
token: ...
level: ERROR
bubble: true
channels: ["request"]
an additional option would be to have environment specific configs (e.g. when app-info logging wouldn't be required in production).

How to email console errors in symfony

I have configured my app to send emails on exceptions this way (prod/monolog.yaml):
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: grouped
excluded_404s:
# regex: exclude all 404 errors from the logs
- ^/
grouped:
type: group
members: [nested, deduplicated]
deduplicated:
type: deduplication
handler: swift
swift:
type: swift_mailer
from_email: '%admin_notifier_from%'
to_email: '%admin_notifier_to%'
subject: 'Error'
level: debug
formatter: monolog.formatter.html
content_type: text/html
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine"]
And it works. When I throw an exception in some controller, open url for that controller I get an email with an exception. So emailing works on http level. However it does not on console level. I am using SF v4.2. I found that in recent symfony version console exception logging was added so no need to add error console listener for it. It should work on my version. Actually I see exception messages in log files. But no email. So I decided to add an error listener and log critical message (assuming it should trigger main monolog handler). So I did it. And again, I see my critical message in log file but still no email. So what wrong am I doing? I want to get exception emails on console exceptions.
I triend to replace "deduplicated" handler with "swift" one but it did not help.

Cannot add a new channel to monolog for prod environment

I wanted to add a new block in my monolog config in order to have logs of a specific bundle in a separate log file. Say that the channel is called purchase
config_dev.php / config_prod.php
purchase:
type: rotating_file
max_files: 10
path: %kernel.logs_dir%/purchase_%kernel.environment%.log
level: debug
channels: purchase
In dev mode, every thing works great and the puchase logs are written in purchase_dev.log. However, although the log configuration of prod mode is the same as dev mode, I'm getting this error
Fatal error: Uncaught exception 'Symfony\Component\DependencyInjection\Exception\InvalidArgumentException' with message 'The service definition "monolog.logger.purchase" does not exist.' in /home/users/me/projects/ecoback/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/ContainerBuilder.php:798
monolog:
channels: ["purchase"]
handlers:
purchase:
type: rotating_file
max_files: 10
path: %kernel.logs_dir%/purchase_%kernel.environment%.log
level: debug
channels: ["purchase"]

symfony2 monolog command output to console in dev

I'm trying to set up a console command in Symfony2 so that it logs to the console in my dev environment, but to a logfile in prod.
so my config_prod.yml has this:
monolog:
handlers:
payment:
type: stream
path: %kernel.logs_dir%/payment.log
channels: payment
while my config_dev.yml uses this:
monolog:
handlers:
console:
type: console
channels: payment
and the service is defined in services.yml like this:
payment_manager:
class: My\Bundle\Service\PaymentManager
arguments: [#doctrine.orm.entity_manager, #logger]
tags:
- { name: monolog.logger, channel: payment }
To my surprise, this does squat nothing. Output goes to app/logs/dev.log instead of the console. Why?
It turns out that "console" doesn't actually mean console at all, it means "browser's javascript console".
If you came here because you have the same question, here's how to do it:
output:
type: stream
path: php://stdout
level: info

Resources