Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monolog FingersCrossedHandler

Tags:

php

monolog

I am looking into using monolog in an application I am working on but I am unsure whether I would be able to implement what I require using the FingersCrosedHandler.

I would like to only log DEBUG level messages if a message with a level of ERROR or higher is added, however I would like to see INFO messages in the log.

I have tried:

$applicationLog = new Monolog\Logger('App');
$streamHandler = new Monolog\Handler\StreamHandler(LOG_FILE, Monolog\Logger::DEBUG, false);
$fingersCrossedHandler = new Monolog\Handler\FingersCrossedHandler($streamHandler, Monolog\Logger::INFO, 0 , false);
$applicationLog->pushHandler($fingersCrossedHandler);
$applicationLog->addDebug('debug');
$applicationLog->addInfo('info');

But this adds both debug and info level messages to the log.

Is this possible to implement using the FingersCrossedHandler or would I need to create my own?

like image 289
John White Avatar asked Nov 20 '12 15:11

John White


2 Answers

This was changed in version 1.11.0 of Monolog. There's now an optional 6th parameter, $passThruLevel. This parameter is the minimum level log that should always be flushed. In your case, you should set up FingersCrossed in the following manner:

use Monolog\Handler\FingersCrossedHandler;

$fingersCrossedHandler = new FingersCrossedHandler(
    $streamHandler,
    Monolog\Logger::ERROR,
    0,
    true,
    true,
    Monolog\Logger::INFO
);

This will result in INFO or higher messages always being logged, but DEBUG messages will only show up if an ERROR level or higher message has been logged.

like image 200
Logan Bailey Avatar answered Oct 21 '22 12:10

Logan Bailey


This is not possible with the FingersCrossedHandler as such. You could easily extend it though and override handle() so that it buffers only debug messages and lets the rest through always. Problem is if you do this you'll have messages out of order when an error occurs, unless you buffer everything, and at the end flush it all except debug if no error occurred.

like image 23
Seldaek Avatar answered Oct 21 '22 13:10

Seldaek