Monolog RotatingFileHandler save to a specific file according to log type
Asked Answered
S

3

6

I'm trying to do something with monolog that I'm not sure it's possible. So I was thinking of a practical way to organize the log files.

First I thought to have 3 different files, INFO, WARNING and ERROR, but it would be difficult to search a specific date inside de file. So I decided to organize like this:

Logs

  • |_ Info |_Year |_12-05-2016.log
  • |_Warning |_Year |_12-05-2016.log
  • |_Error |_Year |_12-05-2016.log

Here's what I decided to do

$infoStreamHandler = new \Monolog\Handler\RotatingFileHandler($settings['path_info'].'/info.log', Monolog\Logger::INFO);
$warningStreamHandler = new \Monolog\Handler\RotatingFileHandler($settings['path_warn'].'/warning.log', Monolog\Logger::WARNING);
$errorStreamHandler = new \Monolog\Handler\RotatingFileHandler($settings['path_error'].'/error.log', Monolog\Logger::ERROR);

$logger ->pushHandler($infoStreamHandler);
$logger->pushHandler($warningStreamHandler);
$logger->pushHandler($errorStreamHandler);

But this does not work as I expected. I tried first with StreamHandler and it worked(but it was only creating one file for all dates), but as soon as I switched to RotatingFileHandler it saved the same warning in all 3 files, instead of saving it in the warning log only.

Any thoughts ?

Thank you in advance.

Snuggle answered 12/5, 2016 at 9:36 Comment(0)
S
2

So I just found what was the problem. I was missing a param, the $maxFiles. $infoStreamHandler = new \Monolog\Handler\RotatingFileHandler($settings['path_info'].'/info.log', 0, Monolog\Logger::INFO);

And it works perfectly now!

Snuggle answered 16/5, 2016 at 9:10 Comment(2)
Do you know how I can save only info logs in info file? Because currently it contains all logs with higher level incllluding warnings and errors.Teak
The log level is representing how much detail you want to record or how technical is your log message. You want to know about critical emergencies and (a bit less) errors. Warnings are a little less interesting, because their are not showstoppers. Infos/Notices are more detail, but you are still interested, what was your system doing before an error happened. Debug messages are really fine detail information, you don't want this o production, only if debugging a problem locally. finally trace is hardcore debugging info, you need to be in trouble to need this fine info.Edwardedwardian
C
5

If you want to divide your logs to ./path/to/directory/2017/07/21-yournamelog.log this is what you need to do:

$logger = new Logger('chanel-name');
$handler = new RotatingFileHandler('./path/to/directory/yournamelog.log', 0, Logger::INFO, true, 0664);
# '/' in date format is treated like '/' in directory path
# so Y/m/d-filename will create path: eg. 2017/07/21-filename.log
$handler->setFilenameFormat('{date}-{filename}', 'Y/m/d');
$logger->pushHandler($handler);

$array = ["x" => "y"];
$logger->addInfo('new message', $array);

It will create log file in path: ./path/to/directory/2017/07/21-yournamelog.log with content:

[2017-07-21 14:33:49] chanel-name.INFO: new message {"x":"y"} []
Concupiscent answered 21/7, 2017 at 13:13 Comment(0)
S
2

So I just found what was the problem. I was missing a param, the $maxFiles. $infoStreamHandler = new \Monolog\Handler\RotatingFileHandler($settings['path_info'].'/info.log', 0, Monolog\Logger::INFO);

And it works perfectly now!

Snuggle answered 16/5, 2016 at 9:10 Comment(2)
Do you know how I can save only info logs in info file? Because currently it contains all logs with higher level incllluding warnings and errors.Teak
The log level is representing how much detail you want to record or how technical is your log message. You want to know about critical emergencies and (a bit less) errors. Warnings are a little less interesting, because their are not showstoppers. Infos/Notices are more detail, but you are still interested, what was your system doing before an error happened. Debug messages are really fine detail information, you don't want this o production, only if debugging a problem locally. finally trace is hardcore debugging info, you need to be in trouble to need this fine info.Edwardedwardian
G
0

In newer versions of symfony(4.4) you can set the date format and filename format on rotating handler.

This is an example from monolog yaml file:

my_channel:
        type: rotating_file
        path:     "%kernel.logs_dir%/%kernel.environment%.my_channel.log"
        level: debug
        date_format: 'Y/m/d'
        filename_format: '{date}-{filename}'
        channels: [my_channel]

It will save it in a path "Y/m/d-my_channel.log"

Garlen answered 4/10, 2022 at 12:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.