Skip to content

Commit

Permalink
feat: add support for multiple logging styles
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Sep 17, 2022
1 parent 7cf0be0 commit 8d713b9
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/LogWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
}

0 comments on commit 8d713b9

Please sign in to comment.