Skip to content

Instantly share code, notes, and snippets.

@andybeak
Created January 27, 2015 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andybeak/32557d6431c776737d20 to your computer and use it in GitHub Desktop.
Save andybeak/32557d6431c776737d20 to your computer and use it in GitHub Desktop.
Simple implementation of Monolog
<?php
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
class Log
{
public static $log;
public static function initLog()
{
$fileName = themosis_path('storage') . 'logs' . DS . 'app.log';
if(!file_exists($fileName))
{
$handle = fopen($fileName, 'w') or die('Cannot open file: ' . $fileName);
fclose($handle);
}
self::$log = new Logger('main');
// configure handler
$handler = new StreamHandler($fileName, Logger::DEBUG);
// format output
$output = "[%datetime%] %channel%.%level_name%: %message%\r\n";
$formatter = new LineFormatter($output, null, true);
$handler->setFormatter($formatter);
// set output to file
self::$log->pushHandler($handler);
}
public static function __callStatic($name, $arguments)
{
if(!is_object(self::$log) || get_class(self::$log) !== 'Monolog\Logger')
{
self::initLog();
}
;
if(is_array($arguments[0]))
{
$arguments[0] = print_r($arguments[0],true);
}
self::$log->$name($arguments[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment