Skip to content

Instantly share code, notes, and snippets.

@mamchenkov
Last active January 5, 2022 09:31
Show Gist options
  • Save mamchenkov/04f708b8492f0f5d7a10 to your computer and use it in GitHub Desktop.
Save mamchenkov/04f708b8492f0f5d7a10 to your computer and use it in GitHub Desktop.
Example use of Monolog logger
<?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));
@Rndwiga
Copy link

Rndwiga commented Feb 20, 2018

Thanks man. Very helpful

@awadev
Copy link

awadev commented Aug 21, 2018

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