From a737937a6d164c735b29ba819a6d417225e9f50c Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Thu, 4 Aug 2022 23:42:50 +0800 Subject: [PATCH] feat: logging module --- src/Logger.php | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/demo.php | 9 +++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/Logger.php create mode 100644 src/demo.php diff --git a/src/Logger.php b/src/Logger.php new file mode 100644 index 0000000..171d147 --- /dev/null +++ b/src/Logger.php @@ -0,0 +1,48 @@ + '37m', // white + logging::INFO => '36m', // light blue + logging::WARNING => '33m', // yellow + logging::ERROR => '31m', // red + logging::CRITICAL => '35m', // purple +); + +class logging { + public const DEBUG = 0; + public const INFO = 1; + public const WARNING = 2; + public const ERROR = 3; + public const CRITICAL = 4; + public static int $logLevel; + + public static function debug($message): void { + logging::output($message, logging::DEBUG); // debug level + } + + public static function info($message): void { + logging::output($message, logging::INFO); // info level + } + + public static function warning($message): void { + logging::output($message, logging::WARNING); // warning level + } + + public static function error($message): void { + logging::output($message, logging::ERROR); // error level + } + + public static function critical($message): void { + logging::output($message, logging::CRITICAL); // critical level + } + + private static function output($message, $type): void { + global $logColor; + if ($type < logging::$logLevel) { + return; // skip output + } + echo "\033[" . $logColor[$type] . $message . "\033[0m" . PHP_EOL; + } +} diff --git a/src/demo.php b/src/demo.php new file mode 100644 index 0000000..1dc35b3 --- /dev/null +++ b/src/demo.php @@ -0,0 +1,9 @@ +