|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Smalex86 package. |
| 5 | + * |
| 6 | + * (c) Alexandr Smirnov <mail_er@mail.ru> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace smalex86\logger\route; |
| 13 | + |
| 14 | +use smalex86\logger\Route; |
| 15 | + |
| 16 | +/** |
| 17 | + * Description of CachedFileRoute |
| 18 | + * |
| 19 | + * @author Alexandr Smirnov <mail_er@mail.ru> |
| 20 | + */ |
| 21 | +class CachedFileRoute extends Route { |
| 22 | + |
| 23 | + /** |
| 24 | + * Log file name |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + public $logFile = 'syslog.log'; |
| 28 | + /** |
| 29 | + * Log folder |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + public $folder = __DIR__ . '/logs/'; |
| 33 | + /** |
| 34 | + * Message template |
| 35 | + * @var string |
| 36 | + */ |
| 37 | + public $template = '{date} :: {level} :: {file}=>{line} :: {message} {context}'; |
| 38 | + |
| 39 | + /** |
| 40 | + * Cache size in line count |
| 41 | + * @var int |
| 42 | + */ |
| 43 | + public $cacheSize = 50; |
| 44 | + |
| 45 | + /** |
| 46 | + * Cache string array |
| 47 | + * @var array |
| 48 | + */ |
| 49 | + protected $cache = []; |
| 50 | + |
| 51 | + /** |
| 52 | + * Constructor |
| 53 | + */ |
| 54 | + public function __construct(array $attributes = []) { |
| 55 | + parent::__construct($attributes); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Destructor for flush remaining cache to log file |
| 60 | + */ |
| 61 | + public function __destruct() { |
| 62 | + if (count($this->cache) > 0) { |
| 63 | + $this->flushCacheToFile(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Method for suitable with PsrLogger. |
| 69 | + * Logs with an arbitrary level. |
| 70 | + * |
| 71 | + * @param mixed $level |
| 72 | + * @param string $message |
| 73 | + * @param array $context |
| 74 | + * @return bool |
| 75 | + */ |
| 76 | + public function log($level, $message, array $context = []): bool |
| 77 | + { |
| 78 | + // if level set in psrloglevel string |
| 79 | + if (is_string($level)) { |
| 80 | + $level = $this->getLogLevelFromRsrLogLevel($level); |
| 81 | + } |
| 82 | + // check for requirement of writing |
| 83 | + // it is determined by log maxLevel and msg level |
| 84 | + if ($this->maxLevel < $level) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + // write message to cache or flush cache to file |
| 88 | + if (count($this->cache) >= $this->cacheSize) { |
| 89 | + return $this->flushCacheToFile(); |
| 90 | + } else { |
| 91 | + $this->cache[] = $this->getLogLine($level, $message, $context); |
| 92 | + } |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Generate new log line |
| 98 | + * @param mixed $level |
| 99 | + * @param string $message |
| 100 | + * @param array $context |
| 101 | + * @return string |
| 102 | + */ |
| 103 | + protected function getLogLine($level, $message, array $context = array()): string |
| 104 | + { |
| 105 | + // pull file and line |
| 106 | + $fileLine = $this->getFileLine(); |
| 107 | + // generate log line |
| 108 | + return trim(strtr($this->template, [ |
| 109 | + '{date}' => $this->getDate(), |
| 110 | + '{level}' => $this->getStatusWord($level), |
| 111 | + '{file}' => $fileLine['file'], |
| 112 | + '{line}' => $fileLine['line'], |
| 113 | + '{message}' => $message, |
| 114 | + '{context}' => $this->contextStringify($context), |
| 115 | + ])); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Flush cache string to log file |
| 120 | + * @return bool |
| 121 | + */ |
| 122 | + protected function flushCacheToFile(): bool |
| 123 | + { |
| 124 | + // folder existing check and creating |
| 125 | + if (!file_exists($this->folder)) { |
| 126 | + mkdir($this->folder); |
| 127 | + } |
| 128 | + // put message to log file |
| 129 | + $result = file_put_contents( |
| 130 | + realpath($this->folder) . DIRECTORY_SEPARATOR . $this->logFile, |
| 131 | + implode(PHP_EOL, $this->cache) . PHP_EOL, |
| 132 | + FILE_APPEND); |
| 133 | + // clear cache |
| 134 | + unset($this->cache); |
| 135 | + $this->cache = []; |
| 136 | + return $result; |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments