From c6221eb4fe29ddf8a09f7fdb4277758716269bd6 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Fri, 15 Nov 2024 17:36:55 +0200 Subject: Improve tests. --- src/Format.php | 8 +++++++- src/Loggr.php | 12 +++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/Format.php b/src/Format.php index 4517e0a..fa1c215 100644 --- a/src/Format.php +++ b/src/Format.php @@ -43,7 +43,7 @@ enum Format /** @var string $trace_line */ $trace_line = $message->trace['line']; - return json_encode([ + $json = json_encode([ 'date' => $dateTime->format('Y-m-d H:i:s'), 'level' => strtoupper($message->level->value), 'message' => $message->content, @@ -53,6 +53,12 @@ enum Format 'line' => $trace_line, ], ]); + + if (!$json) { + return ''; + } + + return $json; } /** diff --git a/src/Loggr.php b/src/Loggr.php index 78b3504..82d5681 100644 --- a/src/Loggr.php +++ b/src/Loggr.php @@ -159,8 +159,18 @@ class Loggr implements LoggerInterface $this->write(Level::Debug, $message, $context); } - public function log(mixed $level, \Stringable|string $message, array $context = []): void + /** + * Logs a message with the specified level and context. + * + * @param mixed $level The logging level (e.g., error, info, debug). + * @param \Stringable|string $message The message to log. + * @param mixed $context Optional context information to include in the log. + * @return void + */ + public function log(mixed $level, \Stringable|string $message, mixed $context = null): void { + if (!is_string($level) && !is_int($level)) return; + $this->trace = debug_backtrace()[0]; $this->write(Level::from($level), $message, $context); } -- cgit v1.2.3