Created
January 27, 2015 10:57
-
-
Save andybeak/32557d6431c776737d20 to your computer and use it in GitHub Desktop.
Simple implementation of Monolog
This file contains hidden or 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 | |
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