How I make log by day in Symfony - symfony

community. I need make logs files daily with Symfony4; or push a current date in a yml file. Specifically in monologo.yml.
handlers:
app:
type: stream
path: "%kernel.logs_dir%/system_compact_%kernel.environment%-***date***.log"
channels: ["app"]
Thank a lot.

Use can use "rotating_file" log type:
monolog:
handlers:
main:
type: rotating_file
path: "%kernel.logs_dir%/system_compact_%kernel.environment%.log"
Look at this documentation page: https://symfony.com/doc/current/logging.html#how-to-rotate-your-log-files

Related

Symfony custom channel / logger

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

monolog sending old logs

W have web-app, built with symfony-flex. For deployment, I am using capistrano. For logging critical logs, I have configured monolog in this way:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
channels: ['!translation']
excluded_http_codes: [{ 404: ['^/security/login'] }]
handler: grouped
grouped:
type: group
members: [deduplicated]
deduplicated:
type: deduplication
handler: swift
swift:
type: swift_mailer
from_email: '%mailer_user%'
to_email: ['email1#gmail.com', 'email2#gmail.com']
subject: "📞🚨 %%level_name%% %%level%%"
level: info
formatter: monolog.formatter.html
content_type: text/html
SwiftMailer configuration:
swiftmailer:
url: '%env(MAILER_URL)%'
spool: { type: 'memory' }
And all works fine except logs after each release. I'm getting old logs which were sent before. Example:
Maybe i have missed something in configuration?
The MonologBundle configuration for the deduplication handler type has additional potential parameters - including
store: The file/path where the deduplication log should be kept, defaults to %kernel.cache_dir%/monolog_dedup_*
It is re-reading the file that is in the cache directory from before you deployed.
I also deploy my site(s) with Capistrano - but I do not share the cache directory between different deploys of my site. My config for shared files is set :linked_dirs, [fetch(:log_path)] - only shares the logs to keep updating them in the long term. The cache directory is still in ./var/cache, but it's freshly created on each deployment.

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