Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monolog RotatingFileHandler save to a specific file according to log type

Tags:

php

slim

monolog

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.

like image 623
Carla Sousa Avatar asked May 12 '16 09:05

Carla Sousa


People also ask

How do monolog loggers work?

With Monolog, you create a Logger instance that has a channel-name and stack of Handlers. Handlers are responsible for saving or sending the message to the desired location which can be a file, terminal, database, or even an email. The sent message travels through the stack of Handlers.

What are monolog processors and formatters?

Monolog also has Processors that can be used to process the message and Formatters that are used to transform the log message to the desired format. INFO (200) : interesting events. Examples: User logs in, SQL logs WARNING (300): exceptional occurrences that are not errors.

What is the Monolog library?

Monolog is an open-source PHP library that aims to make logging easier and more efficient. To see the library source code, visit Monolog on GitHub. Finding bugs in the code may be very frustrating and time-consuming, especially, when the application grows in scale.

How do I format a monolog log message to JSON?

Monolog has multiple built-in formatters that are used to transform your log message to the desired format. A full list of the built-in formatters can be found on the official GitHub documentation. In this example, we will use a JSON formatter that will transform the message to the JSON format.


2 Answers

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"} []
like image 81
R Picheta Avatar answered Oct 18 '22 13:10

R Picheta


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!

like image 23
Carla Sousa Avatar answered Oct 18 '22 11:10

Carla Sousa