Mix Multi and Threshold handlers
trasher opened this issue · 3 comments
Hello,
Using Mutli or Threshold handler separately works fine; but I can't achieve to get them working together. I'm using files to log, and I'd like to get two separate files:
- one for notices, warnings, errors, etc
- another for info and debug messages (because those ones may be very verbose).
Giving the the following example:
$errors = "Errors:\n";
$debug = "Debug:\n";
\Analog\Analog::handler(
\Analog\Handler\Multi::init(
array (
\Analog\Analog::NOTICE => \Analog\Handler\Threshold::init(
\Analog\Handler\Variable::init($errors),
\Analog\Analog::NOTICE
),
Analog::DEBUG => \Analog\Handler\Threshold::init(
\Analog\Handler\Variable::init($debug),
\Analog\Analog::DEBUG
)
)
)
);
\Analog\Analog::log(
'An error',
\Analog\Analog::ERROR
);
\Analog\Analog::log(
'A debug message',
\Analog\Analog::DEBUG
);
var_dump($errors);
var_dump($debug);
The output is:
string 'Errors:
' (length=8)
string 'Debug:
127.0.0.1 - 2016-11-16 21:32:13 - 3 - An error
127.0.0.1 - 2016-11-16 21:32:13 - 7 - A debug message
' (length=108)
But what I was expecting was "An error" to be written in $errors
, and "A debug message" to $debug
.
Did I miss something?
Thank you very much!
I think the Multi handler on its own will do what you're looking for. For example:
$errors = "Errors:\n";
$debug = "Debug:\n";
\Analog\Analog::handler (\Analog\Handler\Multi::init (array (
\Analog\Analog::NOTICE => \Analog\Handler\Variable::init ($errors),
\Analog\Analog::DEBUG => \Analog\Handler\Variable::init ($debug)
)));
\Analog\Analog::log ('First error');
\Analog\Analog::log ('Emergency!', \Analog\Analog::URGENT);
\Analog\Analog::log ('A warning...', \Analog\Analog::WARNING);
\Analog\Analog::log ('Some info', \Analog\Analog::INFO);
\Analog\Analog::log ('Debugging output', \Analog\Analog::DEBUG);
echo $errors;
echo "-----\n";
echo $debug;
This splits anything NOTICE or worse into the $errors
list, and anything DEBUG or INFO into $debug
. Here's my output:
Errors:
localhost - 2016-11-17 15:18:53 - 3 - First error
localhost - 2016-11-17 15:18:53 - 0 - Emergency!
localhost - 2016-11-17 15:18:53 - 4 - A warning...
-----
Debug:
localhost - 2016-11-17 15:18:53 - 6 - Some info
localhost - 2016-11-17 15:18:53 - 7 - Debugging output
Here's a sample using Multi and Threshold together:
\Analog\Analog::handler (\Analog\Handler\Multi::init (array (
\Analog\Analog::NOTICE => \Analog\Handler\Threshold::init (
\Analog\Handler\Variable::init ($errors),
\Analog\Analog::ERROR
),
\Analog\Analog::DEBUG => \Analog\Handler\Variable::init ($debug)
)));
I set the first Multi group to NOTICE, but I set the threshold to ERROR, to show how it skips notices below the threshold. Now the output looks like this:
Errors:
localhost - 2016-11-17 15:21:38 - 3 - First error
localhost - 2016-11-17 15:21:38 - 0 - Emergency!
-----
Debug:
localhost - 2016-11-17 15:21:38 - 6 - Some info
localhost - 2016-11-17 15:21:38 - 7 - Debugging output
This skips the WARNING notice, since it's below the ERROR threshold, while the DEBUG and INFO messages still make it into the separate $debug
output.
Hope that helps!
Hello @jbroadway ,
Indeed that makes sense, don't know why I was trying to make something quite complex when a very simplier code already makes the job!
Thank you very much for your help!!
Sounds like there is one limitation anyways. With your second example, we can indeed prevent warning and notice to be logged at all; but all info and debug messages will still be logged.
So, as far as I understand, I cannot mix a threshold with another one. But this is fine for me, I have a debug mode on my app I can rely on to use the Ignore handler.
Again, thank you for your help!