From 8d713b9437b8425874cf8b05746fda72a856ff06 Mon Sep 17 00:00:00 2001 From: mychidarko Date: Sat, 17 Sep 2022 23:42:31 +0000 Subject: [PATCH] feat: add support for multiple logging styles --- src/LogWriter.php | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/LogWriter.php b/src/LogWriter.php index ca9f9bc..a07354e 100755 --- a/src/LogWriter.php +++ b/src/LogWriter.php @@ -36,22 +36,41 @@ public function __construct(string $file, bool $createFile = false) /** * Write message - * + * * @param mixed $message - * @param int|null $level - * @return int + * @param int $level + * @return int|bool */ - public function write($message, int $level = null): int + public function write($message, $level = null) { + $style = class_exists('Leaf\Config') ? \Leaf\Config::get('log.style') ?? 'leaf' : 'leaf'; + if ($level !== null) { $level = Log::getLevel($level) . " - "; } + if ($style === 'leaf') { + $this->writeAsLeaf($message, $level); + } else if ($style === 'linux') { + $this->writeAsLinux($message, $level); + } + + return 1; + } + + protected function writeAsLeaf($message, $level) + { FS::prepend( $this->logFile, (string) "[" . Date::now() . "]\n" . $level . "$message\n" ); + } - return 1; + protected function writeAsLinux($message, $level) + { + FS::append( + $this->logFile, + (string) "[" . Date::now() . "] " . $level . "$message\n" + ); } }