From e8d0d56b8ecb68649caeb00a9952d22a21ed1085 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Thu, 4 Aug 2022 23:55:02 +0800 Subject: [PATCH] update: enhance log output --- src/Logger.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Logger.php b/src/Logger.php index 171d147..1129ca4 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -17,6 +17,13 @@ class logging { public const ERROR = 3; public const CRITICAL = 4; public static int $logLevel; + private static array $logName = array( + logging::DEBUG => 'DEBUG', + logging::INFO => 'INFO', + logging::WARNING => 'WARNING', + logging::ERROR => 'ERROR', + logging::CRITICAL => 'CRITICAL', + ); public static function debug($message): void { logging::output($message, logging::DEBUG); // debug level @@ -38,11 +45,13 @@ class logging { logging::output($message, logging::CRITICAL); // critical level } - private static function output($message, $type): void { + private static function output($message, $logType): void { global $logColor; - if ($type < logging::$logLevel) { + if ($logType < logging::$logLevel) { return; // skip output } - echo "\033[" . $logColor[$type] . $message . "\033[0m" . PHP_EOL; + $timeStr = '[' . date('Y-m-d H:i:s', time()) . ']'; + $message = $timeStr . ' [' . logging::$logName[$logType] . '] ' . $message; + echo "\033[" . $logColor[$logType] . $message . "\033[0m" . PHP_EOL; } }