1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
<?php
namespace oroboros\core\libraries\logger;
final class FileLogger extends \oroboros\core\abstracts\libraries\logger\AbstractLogger {
const FLAG_TRUNCATE = '::truncate_logfile::';
const LOGMODE = 'file';
private $_pointer;
private $_flags;
private $_params;
private $_write_mode;
private $_file_path;
public function __construct(array $params = array(), array $flags = array()) {
$this->_init($params, $flags);
parent::__construct($params, $flags);
}
public function refreshOptions(array $params = array(), array $flags = array()) {
if (isset($this->_pointer)) {
fclose($this->_pointer);
}
$this->_pointer = NULL;
$this->_flags = NULL;
$this->_params = NULL;
$this->_init($params, $flags);
}
public function log($level, $message, array $context = array()) {
$errorlevel = '[' . strtoupper($level) . ']';
$timestamp = '[' . date_create()->format('Y-m-d H:i:s') . ']';
$context['LEVEL'] = $errorlevel;
$context['TIMESTAMP'] = $timestamp;
$trace = debug_backtrace();
array_shift($trace);
if (!empty($trace) && (isset($trace[0]['class']) && isset($trace[0]['function']) && isset($trace[0]['type'])) && (($trace[0]['class'] == 'oroboros\Oroboros') && ($trace[0]['function'] == 'log') && ($trace[0]['type'] == '::'))) {
array_shift($trace);
}
$last = array_shift($trace);
$context['ORIGIN'] = NULL;
if (isset($last)) {
$details = array(
'class' => ((isset($last['class'])) ? $last['class'] : NULL),
'line' => ((isset($last['line'])) ? $last['line'] : NULL),
'function' => ((isset($last['function'])) ? $last['function'] : NULL),
'type' => ((isset($last['type'])) ? $last['type'] : NULL),
'args' => ((isset($last['args'])) ? $last['args'] : NULL),
'trace' => $trace
);
if (isset($details['class'])) {
$context['ORIGIN'] = '[::Class: ' . $details['class'] . $details['type'] . $details['function'] . '(); ::]';
} elseif (!isset($details['class']) && isset($details['file']) && isset($details['function'])) {
$context['ORIGIN'] = '[::Function: ' . $details['function'] . '(); - (Line: ' . $details['line'] . ' of File:' . $details['file'] . ')::]';
} elseif (isset($details['function']) && !isset($details['file'])) {
$context['ORIGIN'] = '[::Closure::]';
} else {
$context['ORIGIN'] = '[::Unknown Origin::]';
}
}
$message = $this->interpolate('[]TIMESTAMP[/][]LEVEL[/][]ORIGIN[/] ' . $message . PHP_EOL, $context);
$this->_write($message);
}
public function __destruct() {
if (isset($this->_pointer) && is_resource($this->_pointer)) {
fclose($this->_pointer);
}
$this->_pointer = NULL;
parent::__destruct();
}
private function _init(array $params = array(), array $flags = array()) {
if (isset($this->_pointer) && is_resource($this->_pointer)) {
fclose($this->_pointer);
}
$this->_pointer = NULL;
$setheader = FALSE;
$this->_params = $params;
$this->_flags = $flags;
if (!array_key_exists('log', $params) || !$params['log']) {
throw new \oroboros\core\utilities\exception\Exception("A log file must be supplied!", self::ERROR_CORE);
}
if (in_array("debugMode", $params) && $params['debugMode']) {
}
if (count(explode(DIRECTORY_SEPARATOR, $params['log'])) === 1) {
$params['log'] = OROBOROS_DATA . 'logs' . DIRECTORY_SEPARATOR . $params['log'];
}
$filepath = (string) $params['log'];
$fileIndex = explode(DIRECTORY_SEPARATOR, $filepath);
$file = array_pop($fileIndex);
$directory = realpath(implode(DIRECTORY_SEPARATOR, $fileIndex));
if (!is_writable($filepath)) {
throw new \oroboros\core\utilities\exception\Exception("Supplied logfile: [" . $filepath . "] is not writeable!", self::ERROR_CORE);
} elseif ((!file_exists($filepath))
|| (in_array(self::FLAG_TRUNCATE, $flags))) {
$this->_file_path = $filepath;
if (file_exists($this->_file_path)) {
unlink($this->_file_path);
}
touch($this->_file_path);
$this->_write_mode = 'a+';
$this->_createLogHeading($params, $flags);
} else {
$this->_file_path = $filepath;
$this->_write_mode = 'a+';
}
}
private function _createLogHeading(array $options = array(), array $flags = array()) {
$heading = ((isset($options['logHeader']) && $options['logHeader']) ? '[:: ' . $options['logHeader'] . ' ::]' . PHP_EOL : NULL);
$heading .= ((isset($options['logTimestamp']) && $options['logTimestamp']) ? '[:: Created at: ' . date_create()->format('Y-m-d H:i:s') . ' ::]' . PHP_EOL : NULL);
$heading .= (((isset($options['logHeader'])
&& $options['logHeader']) && (isset($options['logTimestamp']) && $options['logTimestamp']))
? '[:: ================ Begin Log ================ ::]' . PHP_EOL . PHP_EOL
: NULL);
if (isset($heading)) {
$this->_write($heading);
}
}
private function _write($message) {
$pointer = fopen($this->_file_path, $this->_write_mode);
fwrite($pointer, rtrim($message) . PHP_EOL);
fclose($pointer);
$pointer = NULL;
}
}