Symfony custom channel / logger - symfony

I am trying to add a simple logger channel "brp" with the following SF6.2-DEV environment:
monolog:
channels:
- deprecation # Deprecations are logged in the dedicated "deprecation" channel when it exists
- brp
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: info
channels: ["!event","!doctrine","!console"]
Few things, I need this one channel to log to the database into a table I specify, also I am only interested in info, warning and errors captured in the "brp" channel.
I've managed to get access to that channel with a simplified config:
monolog:
channels:
- deprecation # Deprecations are logged in the dedicated "deprecation" channel when it exists
- brp
But this logs to a file, and includes all the errors levels, and so on.
TIA

You need to do someting like this, according to the official doc here :
monolog:
channels: [deprecation,brp]
handlers:
deprecation:
type: stream
channels: [deprecation]
level: error
path: '%kernel.logs_dir%/deprecated.log'
brp:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: info
channels: ["!event","!doctrine","!console"]

Related

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).

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

How to log messages to different files with Monolog in Symfony2.2?

I would like to use Monolog in symfony2 application for logging, but my question is how can I split the file every day instead of appending to the same file?
I would like my log file to be somthing like below:
"%kernel.logs_dir%/%kernel.environment%.%date%.log"
Which %date% should be replaced with real date.
I read that logrotate but I don't understand how to use it ?
Use multiple handlers
Example:
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
ex1:
type: stream
path: %kernel.logs_dir%/ex1.log
level: info
ex2:
type: stream
path: %kernel.logs_dir%/ex2.log
level: error
It's explained in the Symfony2 Cookbook http://symfony.com/doc/current/cookbook/logging/monolog.html
And specific for you (channels): http://symfony.com/doc/current/cookbook/logging/channels_handlers.html
This will create a new file for each day. You can also define a max number of files.
monolog:
handlers:
main:
type: rotating_file
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: notice
max_files: 10
It will create a date to the file name automatically, so you don't have to worry about that.
http://symfony.com/doc/current/cookbook/logging/monolog.html

Disable "notified event" message from DEBUG log

It's a painfull job to analyse my actual dev log because the huge amount of "event.DEBUG: Notified event ..." messages. Anyone knows how can I disable the dispatcher notification logs?
Thanks in advance!
You can use channels to ignore events.
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: "!event"
see details here : http://symfony.com/doc/current/cookbook/logging/channels_handlers.html#yaml-specification
The easiest way to accomplish all of this is splitting the various logging channels and levels in app/config/config_dev.yml
monolog:
handlers:
event_all:
bubble: false
action_level: DEBUG
type: stream
path: %kernel.logs_dir%/%kernel.environment%_event_all.log
channels: event
event_errors:
action_level: ERROR
type: stream
path: %kernel.logs_dir%/%kernel.environment%_event_errors.log
channels: event
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: DEBUG
Best guide for how to separate different channels and error levels is here:
http://symfony.com/doc/current/cookbook/logging/monolog.html
Also, see here for my personal recommendations for production log separation:
Symfony2 - Doctrine log

Resources