Last active
January 5, 2022 09:31
-
-
Save mamchenkov/04f708b8492f0f5d7a10 to your computer and use it in GitHub Desktop.
Example use of Monolog logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Before: composer require monolog/monolog | |
// composer autoloader | |
require_once 'vendor/autoload.php'; | |
// Shortcuts for simpler usage | |
use \Monolog\Logger; | |
use \Monolog\Formatter\LineFormatter; | |
use \Monolog\Handler\StreamHandler; | |
// Common logger | |
$log = new Logger('files'); | |
// Line formatter without empty brackets in the end | |
$formatter = new LineFormatter(null, null, false, true); | |
// Debug level handler | |
$debugHandler = new StreamHandler('debug.log', Logger::DEBUG); | |
$debugHandler->setFormatter($formatter); | |
// Error level handler | |
$errorHandler = new StreamHandler('error.log', Logger::ERROR); | |
$errorHandler->setFormatter($formatter); | |
// This will have both DEBUG and ERROR messages | |
$log->pushHandler($debugHandler); | |
// This will have only ERROR messages | |
$log->pushHandler($errorHandler); | |
// The actual logging | |
$log->debug('I am debug'); | |
$log->error('I am error', array('productId' => 123)); |
Thanks man. Very helpful
Where do I buy you a beer?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this sample code helps me, thank you.