@@ -34,322 +34,322 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class Logger extends AbstractLogger |
| 36 | 36 | {
|
| 37 | - /** |
|
| 38 | - * KLogger options |
|
| 39 | - * Anything options not considered 'core' to the logging library should be |
|
| 40 | - * settable view the third parameter in the constructor |
|
| 41 | - * |
|
| 42 | - * Core options include the log file path and the log threshold |
|
| 43 | - * |
|
| 44 | - * @var array |
|
| 45 | - */ |
|
| 46 | - protected $options = array ( |
|
| 47 | - 'extension' => 'txt', |
|
| 48 | - 'dateFormat' => 'Y-m-d G:i:s.u', |
|
| 49 | - 'filename' => false, |
|
| 50 | - 'flushFrequency' => false, |
|
| 51 | - 'prefix' => 'log_', |
|
| 52 | - 'logFormat' => false, |
|
| 53 | - 'appendContext' => true, |
|
| 54 | - ); |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Path to the log file |
|
| 58 | - * @var string |
|
| 59 | - */ |
|
| 60 | - private $logFilePath; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Current minimum logging threshold |
|
| 64 | - * @var integer |
|
| 65 | - */ |
|
| 66 | - protected $logLevelThreshold = LogLevel::DEBUG; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * The number of lines logged in this instance's lifetime |
|
| 70 | - * @var int |
|
| 71 | - */ |
|
| 72 | - private $logLineCount = 0; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Log Levels |
|
| 76 | - * @var array |
|
| 77 | - */ |
|
| 78 | - protected $logLevels = array( |
|
| 79 | - LogLevel::EMERGENCY => 0, |
|
| 80 | - LogLevel::ALERT => 1, |
|
| 81 | - LogLevel::CRITICAL => 2, |
|
| 82 | - LogLevel::ERROR => 3, |
|
| 83 | - LogLevel::WARNING => 4, |
|
| 84 | - LogLevel::NOTICE => 5, |
|
| 85 | - LogLevel::INFO => 6, |
|
| 86 | - LogLevel::DEBUG => 7 |
|
| 87 | - ); |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * This holds the file handle for this instance's log file |
|
| 91 | - * @var resource |
|
| 92 | - */ |
|
| 93 | - private $fileHandle; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * This holds the last line logged to the logger |
|
| 97 | - * Used for unit tests |
|
| 98 | - * @var string |
|
| 99 | - */ |
|
| 100 | - private $lastLine = ''; |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Octal notation for default permissions of the log file |
|
| 104 | - * @var integer |
|
| 105 | - */ |
|
| 106 | - private $defaultPermissions = 0777; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Class constructor |
|
| 110 | - * |
|
| 111 | - * @param string $logDirectory File path to the logging directory |
|
| 112 | - * @param string $logLevelThreshold The LogLevel Threshold |
|
| 113 | - * @param array $options |
|
| 114 | - * |
|
| 115 | - * @internal param string $logFilePrefix The prefix for the log file name |
|
| 116 | - * @internal param string $logFileExt The extension for the log file |
|
| 117 | - */ |
|
| 118 | - public function __construct($logDirectory, $logLevelThreshold = LogLevel::DEBUG, array $options = array()) |
|
| 119 | - {
|
|
| 120 | - $this->logLevelThreshold = $logLevelThreshold; |
|
| 121 | - $this->options = array_merge($this->options, $options); |
|
| 122 | - |
|
| 123 | - $logDirectory = rtrim($logDirectory, DIRECTORY_SEPARATOR); |
|
| 124 | - if ( ! file_exists($logDirectory)) {
|
|
| 125 | - mkdir($logDirectory, $this->defaultPermissions, true); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - if(strpos($logDirectory, 'php://') === 0) {
|
|
| 129 | - $this->setLogToStdOut($logDirectory); |
|
| 130 | - $this->setFileHandle('w+');
|
|
| 131 | - } else {
|
|
| 132 | - $this->setLogFilePath($logDirectory); |
|
| 133 | - if(file_exists($this->logFilePath) && !is_writable($this->logFilePath)) {
|
|
| 134 | - throw new RuntimeException('The file could not be written to. Check that appropriate permissions have been set.');
|
|
| 135 | - } |
|
| 136 | - $this->setFileHandle('a');
|
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if ( ! $this->fileHandle) {
|
|
| 140 | - throw new RuntimeException('The file could not be opened. Check permissions.');
|
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @param string $stdOutPath |
|
| 146 | - */ |
|
| 147 | - public function setLogToStdOut($stdOutPath) {
|
|
| 148 | - $this->logFilePath = $stdOutPath; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @param string $logDirectory |
|
| 153 | - */ |
|
| 154 | - public function setLogFilePath($logDirectory) {
|
|
| 155 | - if ($this->options['filename']) {
|
|
| 156 | - if (strpos($this->options['filename'], '.log') !== false || strpos($this->options['filename'], '.txt') !== false) {
|
|
| 157 | - $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename']; |
|
| 158 | - } |
|
| 159 | - else {
|
|
| 160 | - $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename'].'.'.$this->options['extension']; |
|
| 161 | - } |
|
| 162 | - } else {
|
|
| 163 | - $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['prefix'].date('Y-m-d').'.'.$this->options['extension'];
|
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @param $writeMode |
|
| 169 | - * |
|
| 170 | - * @internal param resource $fileHandle |
|
| 171 | - */ |
|
| 172 | - public function setFileHandle($writeMode) {
|
|
| 173 | - $this->fileHandle = fopen($this->logFilePath, $writeMode); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Class destructor |
|
| 179 | - */ |
|
| 180 | - public function __destruct() |
|
| 181 | - {
|
|
| 182 | - if ($this->fileHandle) {
|
|
| 183 | - fclose($this->fileHandle); |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Sets the date format used by all instances of KLogger |
|
| 189 | - * |
|
| 190 | - * @param string $dateFormat Valid format string for date() |
|
| 191 | - */ |
|
| 192 | - public function setDateFormat($dateFormat) |
|
| 193 | - {
|
|
| 194 | - $this->options['dateFormat'] = $dateFormat; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Sets the Log Level Threshold |
|
| 199 | - * |
|
| 200 | - * @param string $logLevelThreshold The log level threshold |
|
| 201 | - */ |
|
| 202 | - public function setLogLevelThreshold($logLevelThreshold) |
|
| 203 | - {
|
|
| 204 | - $this->logLevelThreshold = $logLevelThreshold; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Logs with an arbitrary level. |
|
| 209 | - * |
|
| 210 | - * @param mixed $level |
|
| 211 | - * @param string $message |
|
| 212 | - * @param array $context |
|
| 213 | - * @return null |
|
| 214 | - */ |
|
| 215 | - public function log($level, $message, array $context = array()) |
|
| 216 | - {
|
|
| 217 | - if ($this->logLevels[$this->logLevelThreshold] < $this->logLevels[$level]) {
|
|
| 218 | - return; |
|
| 219 | - } |
|
| 220 | - $message = $this->formatMessage($level, $message, $context); |
|
| 221 | - $this->write($message); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Writes a line to the log without prepending a status or timestamp |
|
| 226 | - * |
|
| 227 | - * @param string $message Line to write to the log |
|
| 228 | - * @return void |
|
| 229 | - */ |
|
| 230 | - public function write($message) |
|
| 231 | - {
|
|
| 232 | - if (null !== $this->fileHandle) {
|
|
| 233 | - if (fwrite($this->fileHandle, $message) === false) {
|
|
| 234 | - throw new RuntimeException('The file could not be written to. Check that appropriate permissions have been set.');
|
|
| 235 | - } else {
|
|
| 236 | - $this->lastLine = trim($message); |
|
| 237 | - $this->logLineCount++; |
|
| 238 | - |
|
| 239 | - if ($this->options['flushFrequency'] && $this->logLineCount % $this->options['flushFrequency'] === 0) {
|
|
| 240 | - fflush($this->fileHandle); |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Get the file path that the log is currently writing to |
|
| 248 | - * |
|
| 249 | - * @return string |
|
| 250 | - */ |
|
| 251 | - public function getLogFilePath() |
|
| 252 | - {
|
|
| 253 | - return $this->logFilePath; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Get the last line logged to the log file |
|
| 258 | - * |
|
| 259 | - * @return string |
|
| 260 | - */ |
|
| 261 | - public function getLastLogLine() |
|
| 262 | - {
|
|
| 263 | - return $this->lastLine; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Formats the message for logging. |
|
| 268 | - * |
|
| 269 | - * @param string $level The Log Level of the message |
|
| 270 | - * @param string $message The message to log |
|
| 271 | - * @param array $context The context |
|
| 272 | - * @return string |
|
| 273 | - */ |
|
| 274 | - protected function formatMessage($level, $message, $context) |
|
| 275 | - {
|
|
| 276 | - if ($this->options['logFormat']) {
|
|
| 277 | - $parts = array( |
|
| 278 | - 'date' => $this->getTimestamp(), |
|
| 279 | - 'level' => strtoupper($level), |
|
| 280 | - 'level-padding' => str_repeat(' ', 9 - strlen($level)),
|
|
| 281 | - 'priority' => $this->logLevels[$level], |
|
| 282 | - 'message' => $message, |
|
| 283 | - 'context' => json_encode($context), |
|
| 284 | - ); |
|
| 285 | - $message = $this->options['logFormat']; |
|
| 286 | - foreach ($parts as $part => $value) {
|
|
| 287 | - $message = str_replace('{'.$part.'}', $value, $message);
|
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - } else {
|
|
| 291 | - $message = "[{$this->getTimestamp()}] [{$level}] {$message}";
|
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - if ($this->options['appendContext'] && ! empty($context)) {
|
|
| 295 | - $message .= PHP_EOL.$this->indent($this->contextToString($context)); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - return $message.PHP_EOL; |
|
| 299 | - |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Gets the correctly formatted Date/Time for the log entry. |
|
| 304 | - * |
|
| 305 | - * PHP DateTime is dump, and you have to resort to trickery to get microseconds |
|
| 306 | - * to work correctly, so here it is. |
|
| 307 | - * |
|
| 308 | - * @return string |
|
| 309 | - */ |
|
| 310 | - private function getTimestamp() |
|
| 311 | - {
|
|
| 312 | - $originalTime = microtime(true); |
|
| 313 | - $micro = sprintf("%06d", ($originalTime - floor($originalTime)) * 1000000);
|
|
| 314 | - $date = new DateTime(date('Y-m-d H:i:s.'.$micro, (int)$originalTime));
|
|
| 315 | - |
|
| 316 | - return $date->format($this->options['dateFormat']); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Takes the given context and coverts it to a string. |
|
| 321 | - * |
|
| 322 | - * @param array $context The Context |
|
| 323 | - * @return string |
|
| 324 | - */ |
|
| 325 | - protected function contextToString($context) |
|
| 326 | - {
|
|
| 327 | - $export = ''; |
|
| 328 | - foreach ($context as $key => $value) {
|
|
| 329 | - $export .= "{$key}: ";
|
|
| 330 | - $export .= preg_replace(array( |
|
| 331 | - '/=>\s+([a-zA-Z])/im', |
|
| 332 | - '/array\(\s+\)/im', |
|
| 333 | - '/^ |\G /m' |
|
| 334 | - ), array( |
|
| 335 | - '=> $1', |
|
| 336 | - 'array()', |
|
| 337 | - ' ' |
|
| 338 | - ), str_replace('array (', 'array(', var_export($value, true)));
|
|
| 339 | - $export .= PHP_EOL; |
|
| 340 | - } |
|
| 341 | - return str_replace(array('\\\\', '\\\''), array('\\', '\''), rtrim($export));
|
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Indents the given string with the given indent. |
|
| 346 | - * |
|
| 347 | - * @param string $string The string to indent |
|
| 348 | - * @param string $indent What to use as the indent. |
|
| 349 | - * @return string |
|
| 350 | - */ |
|
| 351 | - protected function indent($string, $indent = ' ') |
|
| 352 | - {
|
|
| 353 | - return $indent.str_replace("\n", "\n".$indent, $string);
|
|
| 354 | - } |
|
| 37 | + /** |
|
| 38 | + * KLogger options |
|
| 39 | + * Anything options not considered 'core' to the logging library should be |
|
| 40 | + * settable view the third parameter in the constructor |
|
| 41 | + * |
|
| 42 | + * Core options include the log file path and the log threshold |
|
| 43 | + * |
|
| 44 | + * @var array |
|
| 45 | + */ |
|
| 46 | + protected $options = array ( |
|
| 47 | + 'extension' => 'txt', |
|
| 48 | + 'dateFormat' => 'Y-m-d G:i:s.u', |
|
| 49 | + 'filename' => false, |
|
| 50 | + 'flushFrequency' => false, |
|
| 51 | + 'prefix' => 'log_', |
|
| 52 | + 'logFormat' => false, |
|
| 53 | + 'appendContext' => true, |
|
| 54 | + ); |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Path to the log file |
|
| 58 | + * @var string |
|
| 59 | + */ |
|
| 60 | + private $logFilePath; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Current minimum logging threshold |
|
| 64 | + * @var integer |
|
| 65 | + */ |
|
| 66 | + protected $logLevelThreshold = LogLevel::DEBUG; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * The number of lines logged in this instance's lifetime |
|
| 70 | + * @var int |
|
| 71 | + */ |
|
| 72 | + private $logLineCount = 0; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Log Levels |
|
| 76 | + * @var array |
|
| 77 | + */ |
|
| 78 | + protected $logLevels = array( |
|
| 79 | + LogLevel::EMERGENCY => 0, |
|
| 80 | + LogLevel::ALERT => 1, |
|
| 81 | + LogLevel::CRITICAL => 2, |
|
| 82 | + LogLevel::ERROR => 3, |
|
| 83 | + LogLevel::WARNING => 4, |
|
| 84 | + LogLevel::NOTICE => 5, |
|
| 85 | + LogLevel::INFO => 6, |
|
| 86 | + LogLevel::DEBUG => 7 |
|
| 87 | + ); |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * This holds the file handle for this instance's log file |
|
| 91 | + * @var resource |
|
| 92 | + */ |
|
| 93 | + private $fileHandle; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * This holds the last line logged to the logger |
|
| 97 | + * Used for unit tests |
|
| 98 | + * @var string |
|
| 99 | + */ |
|
| 100 | + private $lastLine = ''; |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Octal notation for default permissions of the log file |
|
| 104 | + * @var integer |
|
| 105 | + */ |
|
| 106 | + private $defaultPermissions = 0777; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Class constructor |
|
| 110 | + * |
|
| 111 | + * @param string $logDirectory File path to the logging directory |
|
| 112 | + * @param string $logLevelThreshold The LogLevel Threshold |
|
| 113 | + * @param array $options |
|
| 114 | + * |
|
| 115 | + * @internal param string $logFilePrefix The prefix for the log file name |
|
| 116 | + * @internal param string $logFileExt The extension for the log file |
|
| 117 | + */ |
|
| 118 | + public function __construct($logDirectory, $logLevelThreshold = LogLevel::DEBUG, array $options = array()) |
|
| 119 | + {
|
|
| 120 | + $this->logLevelThreshold = $logLevelThreshold; |
|
| 121 | + $this->options = array_merge($this->options, $options); |
|
| 122 | + |
|
| 123 | + $logDirectory = rtrim($logDirectory, DIRECTORY_SEPARATOR); |
|
| 124 | + if ( ! file_exists($logDirectory)) {
|
|
| 125 | + mkdir($logDirectory, $this->defaultPermissions, true); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + if(strpos($logDirectory, 'php://') === 0) {
|
|
| 129 | + $this->setLogToStdOut($logDirectory); |
|
| 130 | + $this->setFileHandle('w+');
|
|
| 131 | + } else {
|
|
| 132 | + $this->setLogFilePath($logDirectory); |
|
| 133 | + if(file_exists($this->logFilePath) && !is_writable($this->logFilePath)) {
|
|
| 134 | + throw new RuntimeException('The file could not be written to. Check that appropriate permissions have been set.');
|
|
| 135 | + } |
|
| 136 | + $this->setFileHandle('a');
|
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if ( ! $this->fileHandle) {
|
|
| 140 | + throw new RuntimeException('The file could not be opened. Check permissions.');
|
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @param string $stdOutPath |
|
| 146 | + */ |
|
| 147 | + public function setLogToStdOut($stdOutPath) {
|
|
| 148 | + $this->logFilePath = $stdOutPath; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @param string $logDirectory |
|
| 153 | + */ |
|
| 154 | + public function setLogFilePath($logDirectory) {
|
|
| 155 | + if ($this->options['filename']) {
|
|
| 156 | + if (strpos($this->options['filename'], '.log') !== false || strpos($this->options['filename'], '.txt') !== false) {
|
|
| 157 | + $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename']; |
|
| 158 | + } |
|
| 159 | + else {
|
|
| 160 | + $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['filename'].'.'.$this->options['extension']; |
|
| 161 | + } |
|
| 162 | + } else {
|
|
| 163 | + $this->logFilePath = $logDirectory.DIRECTORY_SEPARATOR.$this->options['prefix'].date('Y-m-d').'.'.$this->options['extension'];
|
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @param $writeMode |
|
| 169 | + * |
|
| 170 | + * @internal param resource $fileHandle |
|
| 171 | + */ |
|
| 172 | + public function setFileHandle($writeMode) {
|
|
| 173 | + $this->fileHandle = fopen($this->logFilePath, $writeMode); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Class destructor |
|
| 179 | + */ |
|
| 180 | + public function __destruct() |
|
| 181 | + {
|
|
| 182 | + if ($this->fileHandle) {
|
|
| 183 | + fclose($this->fileHandle); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Sets the date format used by all instances of KLogger |
|
| 189 | + * |
|
| 190 | + * @param string $dateFormat Valid format string for date() |
|
| 191 | + */ |
|
| 192 | + public function setDateFormat($dateFormat) |
|
| 193 | + {
|
|
| 194 | + $this->options['dateFormat'] = $dateFormat; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Sets the Log Level Threshold |
|
| 199 | + * |
|
| 200 | + * @param string $logLevelThreshold The log level threshold |
|
| 201 | + */ |
|
| 202 | + public function setLogLevelThreshold($logLevelThreshold) |
|
| 203 | + {
|
|
| 204 | + $this->logLevelThreshold = $logLevelThreshold; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Logs with an arbitrary level. |
|
| 209 | + * |
|
| 210 | + * @param mixed $level |
|
| 211 | + * @param string $message |
|
| 212 | + * @param array $context |
|
| 213 | + * @return null |
|
| 214 | + */ |
|
| 215 | + public function log($level, $message, array $context = array()) |
|
| 216 | + {
|
|
| 217 | + if ($this->logLevels[$this->logLevelThreshold] < $this->logLevels[$level]) {
|
|
| 218 | + return; |
|
| 219 | + } |
|
| 220 | + $message = $this->formatMessage($level, $message, $context); |
|
| 221 | + $this->write($message); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Writes a line to the log without prepending a status or timestamp |
|
| 226 | + * |
|
| 227 | + * @param string $message Line to write to the log |
|
| 228 | + * @return void |
|
| 229 | + */ |
|
| 230 | + public function write($message) |
|
| 231 | + {
|
|
| 232 | + if (null !== $this->fileHandle) {
|
|
| 233 | + if (fwrite($this->fileHandle, $message) === false) {
|
|
| 234 | + throw new RuntimeException('The file could not be written to. Check that appropriate permissions have been set.');
|
|
| 235 | + } else {
|
|
| 236 | + $this->lastLine = trim($message); |
|
| 237 | + $this->logLineCount++; |
|
| 238 | + |
|
| 239 | + if ($this->options['flushFrequency'] && $this->logLineCount % $this->options['flushFrequency'] === 0) {
|
|
| 240 | + fflush($this->fileHandle); |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Get the file path that the log is currently writing to |
|
| 248 | + * |
|
| 249 | + * @return string |
|
| 250 | + */ |
|
| 251 | + public function getLogFilePath() |
|
| 252 | + {
|
|
| 253 | + return $this->logFilePath; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Get the last line logged to the log file |
|
| 258 | + * |
|
| 259 | + * @return string |
|
| 260 | + */ |
|
| 261 | + public function getLastLogLine() |
|
| 262 | + {
|
|
| 263 | + return $this->lastLine; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Formats the message for logging. |
|
| 268 | + * |
|
| 269 | + * @param string $level The Log Level of the message |
|
| 270 | + * @param string $message The message to log |
|
| 271 | + * @param array $context The context |
|
| 272 | + * @return string |
|
| 273 | + */ |
|
| 274 | + protected function formatMessage($level, $message, $context) |
|
| 275 | + {
|
|
| 276 | + if ($this->options['logFormat']) {
|
|
| 277 | + $parts = array( |
|
| 278 | + 'date' => $this->getTimestamp(), |
|
| 279 | + 'level' => strtoupper($level), |
|
| 280 | + 'level-padding' => str_repeat(' ', 9 - strlen($level)),
|
|
| 281 | + 'priority' => $this->logLevels[$level], |
|
| 282 | + 'message' => $message, |
|
| 283 | + 'context' => json_encode($context), |
|
| 284 | + ); |
|
| 285 | + $message = $this->options['logFormat']; |
|
| 286 | + foreach ($parts as $part => $value) {
|
|
| 287 | + $message = str_replace('{'.$part.'}', $value, $message);
|
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + } else {
|
|
| 291 | + $message = "[{$this->getTimestamp()}] [{$level}] {$message}";
|
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + if ($this->options['appendContext'] && ! empty($context)) {
|
|
| 295 | + $message .= PHP_EOL.$this->indent($this->contextToString($context)); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + return $message.PHP_EOL; |
|
| 299 | + |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Gets the correctly formatted Date/Time for the log entry. |
|
| 304 | + * |
|
| 305 | + * PHP DateTime is dump, and you have to resort to trickery to get microseconds |
|
| 306 | + * to work correctly, so here it is. |
|
| 307 | + * |
|
| 308 | + * @return string |
|
| 309 | + */ |
|
| 310 | + private function getTimestamp() |
|
| 311 | + {
|
|
| 312 | + $originalTime = microtime(true); |
|
| 313 | + $micro = sprintf("%06d", ($originalTime - floor($originalTime)) * 1000000);
|
|
| 314 | + $date = new DateTime(date('Y-m-d H:i:s.'.$micro, (int)$originalTime));
|
|
| 315 | + |
|
| 316 | + return $date->format($this->options['dateFormat']); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Takes the given context and coverts it to a string. |
|
| 321 | + * |
|
| 322 | + * @param array $context The Context |
|
| 323 | + * @return string |
|
| 324 | + */ |
|
| 325 | + protected function contextToString($context) |
|
| 326 | + {
|
|
| 327 | + $export = ''; |
|
| 328 | + foreach ($context as $key => $value) {
|
|
| 329 | + $export .= "{$key}: ";
|
|
| 330 | + $export .= preg_replace(array( |
|
| 331 | + '/=>\s+([a-zA-Z])/im', |
|
| 332 | + '/array\(\s+\)/im', |
|
| 333 | + '/^ |\G /m' |
|
| 334 | + ), array( |
|
| 335 | + '=> $1', |
|
| 336 | + 'array()', |
|
| 337 | + ' ' |
|
| 338 | + ), str_replace('array (', 'array(', var_export($value, true)));
|
|
| 339 | + $export .= PHP_EOL; |
|
| 340 | + } |
|
| 341 | + return str_replace(array('\\\\', '\\\''), array('\\', '\''), rtrim($export));
|
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Indents the given string with the given indent. |
|
| 346 | + * |
|
| 347 | + * @param string $string The string to indent |
|
| 348 | + * @param string $indent What to use as the indent. |
|
| 349 | + * @return string |
|
| 350 | + */ |
|
| 351 | + protected function indent($string, $indent = ' ') |
|
| 352 | + {
|
|
| 353 | + return $indent.str_replace("\n", "\n".$indent, $string);
|
|
| 354 | + } |
|
| 355 | 355 | } |