From ea2fbc499a7c9a3255541f7abb9aa8d254d181fe Mon Sep 17 00:00:00 2001 From: "o.pinke" Date: Mon, 11 Mar 2024 15:29:03 +0100 Subject: [PATCH] init new log writer stuff psr-4 compatible --- conlite/classes/Log/Log.php | 141 ++++++++++++++++++++++++++ conlite/classes/Log/LogWriter.php | 66 ++++++++++++ conlite/classes/Log/LogWriterFile.php | 8 ++ conlite/classes/Log/ModuleLog.php | 8 ++ 4 files changed, 223 insertions(+) create mode 100644 conlite/classes/Log/Log.php create mode 100644 conlite/classes/Log/LogWriter.php create mode 100644 conlite/classes/Log/LogWriterFile.php create mode 100644 conlite/classes/Log/ModuleLog.php diff --git a/conlite/classes/Log/Log.php b/conlite/classes/Log/Log.php new file mode 100644 index 0000000..80c4e8a --- /dev/null +++ b/conlite/classes/Log/Log.php @@ -0,0 +1,141 @@ + \cRegistry::getConfigValue('path', 'logs') . 'conlite.log']; + $writer = LogWriter::factory('File', $options); + } + + $this->setWriter($writer); + $this->setShortcutHandler('%date', [$this, 'shDate']); + $this->setShortcutHandler('%level', [$this, 'shLevel']); + $this->setShortcutHandler('%message', [$this, 'shMessage']); + + $this->getWriter()->setOption('log_format', '[%date] [%level] %message', false); + + $reflection = new ReflectionClass($this); + $this->priorities = $this->defaultPriorities = array_flip($reflection->getConstants()); + } + + public function getWriter() { + return $this->writer; + } + + public function setWriter(LogWriter $writer): void + { + $this->writer = $writer; + } + + /** + * @throws InvalidArgumentException + */ + public function setShortcutHandler($shortcut, $handler) { + if ($shortcut == '') { + throw new InvalidArgumentException('The shortcut name must not be empty.'); + } + + if (cString::getPartOfString($shortcut, 0, 1) == '%') { + $shortcut = cString::getPartOfString($shortcut, 1); + } + + if (!is_callable($handler)) { + throw new InvalidArgumentException('The specified shortcut handler does not exist.'); + } + + if (array_key_exists($shortcut, $this->shortcutHandlers)) { + throw new InvalidArgumentException('The shortcut ' . $shortcut . ' is already in use!'); + } + + $this->shortcutHandlers[$shortcut] = $handler; + + return true; + } + +} \ No newline at end of file diff --git a/conlite/classes/Log/LogWriter.php b/conlite/classes/Log/LogWriter.php new file mode 100644 index 0000000..f40eb43 --- /dev/null +++ b/conlite/classes/Log/LogWriter.php @@ -0,0 +1,66 @@ +setOptions($options); + + $this->setOption('default_priority', Log::INFO, false); + $this->setOption('line_ending', PHP_EOL, false); + } + + /** + * @throws invalidArgumentException + */ + public static function factory($writerName, array $writerOptions): LogWriter + { + $logWriterClassName = 'LogWriter' . ucfirst($writerName); + if(!class_exists($logWriterClassName)) { + throw new InvalidArgumentException('Unknown ConLite LogWriter class: ' . $writerName); + } + + $writer = new $logWriterClassName($writerOptions); + if(!($writer instanceof LogWriter)) { + throw new InvalidArgumentException('Provided class is not an instance of ConLite LogWriter'); + } + + return $writer; + } + + public function getOptions(): array + { + return $this->options; + } + + public function getOption($option) { + return $this->options[$option]; + } + + public function setOptions(array $options): void + { + $this->options = $options; + } + + public function setOption($option, $value, $force = false) { + if (!$force && isset($this->options[$option])) { + return; + } + + $this->options[$option] = $value; + } + public function removeOption($option) { + unset($this->options[$option]); + } + + abstract function write($message, $priority); +} \ No newline at end of file diff --git a/conlite/classes/Log/LogWriterFile.php b/conlite/classes/Log/LogWriterFile.php new file mode 100644 index 0000000..97ebdc2 --- /dev/null +++ b/conlite/classes/Log/LogWriterFile.php @@ -0,0 +1,8 @@ +