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
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.
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.
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.
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.
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.
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"} []
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With