Skip to content

Instantly share code, notes, and snippets.

@lisaross
Forked from mamchenkov/monolog.php
Created September 6, 2018 19:50
Show Gist options
  • Save lisaross/d36f1132ad9b17cef05a212ebd4bdc47 to your computer and use it in GitHub Desktop.
Save lisaross/d36f1132ad9b17cef05a212ebd4bdc47 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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment