bartlett / monolog-callbackfilterhandler
Monolog handler wrapper that filters records based on a list of callback functions
Installs: 3 302
Dependents: 3
Suggesters: 4
Security: 0
Stars: 5
Watchers: 1
Forks: 1
Open Issues: 1
Requires
- php: >=5.3.0
- monolog/monolog: ~1.10
This package is auto-updated.
Last update: 2021-03-31 16:37:06 UTC
README
Installation
This handler what is not yet part of standard Monolog distribution, is available on Packagist bartlett/monolog-callbackfilterhandler and as such installable via Composer.
$ php composer.phar require bartlett/monolog-callbackfilterhandler
If you do not use Composer, you can grab the code from GitHub, and use any PSR-0 compatible autoloader to load Monolog classes.
Requirements
-
This handler works with PHP 5.3 and Monolog 1.10 (or above)
Features
-
This handler obey first to basic Monolog rules as
handler level
andbubble
. -
Then, in second time, logs are filtered by rules defined in one or more callback functions.
Main difference with FilterHandler included in standard Monolog distribution since version 1.8.0
-
FilterHandler
can just filter records and only allow those of a given list of levels through to the wrapped handler. -
CallbackFilterHandler
may filter records to the wrapped handler, on each standard record elements including extra data and logging context.
Configuring
Here is a basic setup to log all events to a file and most important to another one (or notify by mail).
<?php use Bartlett\Monolog\Handler\CallbackFilterHandler; use Monolog\Logger; use Monolog\Handler\RotatingFileHandler; use Monolog\Handler\NativeMailerHandler; use Monolog\Handler\StreamHandler; // Create the logger $logger = new Logger('my_logger'); // Create filter rules $filters = array( function ($record) { if (!array_key_exists('exception', $record['context'])) { return false; } return (preg_match('/fake error/', $record['message']) === 1); } ); // Create some handlers $stream = new RotatingFileHandler(__DIR__ . DIRECTORY_SEPARATOR . 'my_logger.log'); $stream->setFilenameFormat('{filename}-{date}', 'Ymd'); //$mailer = new NativeMailerHandler('user@example.org', 'dear user', 'receiver@example.org'); $mailer = new StreamHandler(__DIR__ . DIRECTORY_SEPARATOR . 'notifications.log', Logger::ERROR); // add handlers to the logger $logger->pushHandler($stream); $logger->pushHandler(new CallbackFilterHandler($mailer, $filters)); // You can now use your logger $logger->addInfo('My logger is now ready'); $logger->addError('A fake error has occured. Will be logged to file BUT NOT notified by mail.'); try { throw new \RuntimeException(); } catch (\Exception $e) { $logger->addCritical( 'A fake error has occured. Will be logged to file AND notified by mail.', array('exception' => (string) $e) ); }
Authors
-
Laurent Laville
-
Christophe Coevoet (suggested the code base on discussion of Monolog PR#411)
License
This handler is licensed under the BSD-3-clauses License - see the LICENSE file for details