Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
80.00% covered (warning)
80.00%
4 / 5
CRAP
94.12% covered (success)
94.12%
32 / 34
AbstractLogger
0.00% covered (danger)
0.00%
0 / 1
83.33% covered (warning)
83.33%
5 / 6
17.06
94.12% covered (success)
94.12%
32 / 34
 _getTimeString()
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 log($message, $eventType = ScreenLogger::NORMAL)
100.00% covered (success)
100.00%
1 / 1
1  
 
 debug($bool = true)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 _getString($message, $timeString, $eventType)
0.00% covered (danger)
0.00%
0 / 1
2.50
50.00% covered (warning)
50.00%
2 / 4
 logDebug($message)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 logError(\Exception $e)
100.00% covered (success)
100.00%
1 / 1
10
100.00% covered (success)
100.00%
22 / 22
<?php
/**
 *
 */
namespace aae\log {
    /**
     * @author Axel Ancona Esselmann
     * @package aae\log
     */
    abstract class AbstractLogger {
        const NORMAL = 0, DEBUG = 1, FATAL = 2;
        private $_debug = false;
        protected $_newLine = "\n";
        protected function _getTimeString() {
            date_default_timezone_set('America/Los_Angeles');
            return date('m/d/Y h:i:s a', time());
        }
        abstract public function log($message, $eventType = ScreenLogger::NORMAL);
        public function debug($bool = true) {
            $this->_debug = (bool)$bool;
        }
        protected function _getString($message, $timeString, $eventType) {
            if (!is_string($message)) {
                $message = serialize($message);
            }
            return "Type: {$eventType}, Time: {$timeString}, Message: '{$message}'\n";
        }
        public function logDebug($message) {
            if ($this->_debug) {
                $this->log($message, AbstractLogger::DEBUG);
            }
        }
        public function logError(\Exception $e) {
            $errorMessage = get_class($e)." {$e->getCode()} with message: '{$e->getMessage()}'";
            if ($this->_debug) {
                $errorMessage .= $this->_newLine.$this->_newLine."Exception stack:".$this->_newLine;
                $trace = $e->getTrace();
                for ($i=0; $i < count($trace); $i++) { 
                    if (isset($trace[$i]["file"])) {
                        $file = " in '".$trace[$i]["file"]."' on line ".$trace[$i]["line"];
                    } else {
                        $file = "";
                    }
                    $class = "";
                    $type = "";
                    if (isset($trace[$i]["class"])) {
                        $class = $trace[$i]["class"];
                        $type = $trace[$i]["type"];
                    }
                    if ($class != "PHPUnit_Framework_TestCase" && $class != "PHPUnit_Framework_TestSuite" && $class != "PHPUnit_Framework_TestResult" && $class != "PHPUnit_TextUI_TestRunner" && $class != "PHPUnit_TextUI_Command") {
                        $errorMessage .= $this->_newLine.$class.$type.$trace[$i]["function"].$file;
                    }
                }
            }
            $this->log($errorMessage, AbstractLogger::FATAL);
        }
    }
}