andybeak
1/27/2015 - 10:57 AM

Simple implementation of Monolog

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]);
    }

}