From 363a028a1c5171c803a0d8a86ffbf70e1e9c4d76 Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 11 Feb 2025 15:46:33 +0000 Subject: [PATCH] Use an enum for log levels Avoiding a random handful of disconnected constants in the global namespace --- core/cli_app.php | 10 +++---- core/logging.php | 57 +++++++++++++++++++------------------- ext/cron_uploader/main.php | 36 ++++++++++-------------- ext/log_console/main.php | 16 +++++------ ext/log_db/main.php | 39 ++++++++------------------ tests/phpstan.neon | 1 - 6 files changed, 67 insertions(+), 92 deletions(-) diff --git a/core/cli_app.php b/core/cli_app.php index 97f930a3d..7c6d90b17 100644 --- a/core/cli_app.php +++ b/core/cli_app.php @@ -41,16 +41,16 @@ public function run(?InputInterface $input = null, ?OutputInterface $output = nu send_event(new UserLoginEvent($user)); } - $log_level = SCORE_LOG_WARNING; + $log_level = LogLevel::WARNING->value; if (true === $input->hasParameterOption(['--quiet', '-q'], true)) { - $log_level = SCORE_LOG_ERROR; + $log_level = LogLevel::ERROR->value; } else { if ($input->hasParameterOption('-vvv', true) || $input->hasParameterOption('--verbose=3', true) || 3 === $input->getParameterOption('--verbose', false, true)) { - $log_level = SCORE_LOG_DEBUG; + $log_level = LogLevel::DEBUG->value; } elseif ($input->hasParameterOption('-vv', true) || $input->hasParameterOption('--verbose=2', true) || 2 === $input->getParameterOption('--verbose', false, true)) { - $log_level = SCORE_LOG_DEBUG; + $log_level = LogLevel::DEBUG->value; } elseif ($input->hasParameterOption('-v', true) || $input->hasParameterOption('--verbose=1', true) || $input->hasParameterOption('--verbose', true) || $input->getParameterOption('--verbose', false, true)) { - $log_level = SCORE_LOG_INFO; + $log_level = LogLevel::INFO->value; } } if (!defined("CLI_LOG_LEVEL")) { diff --git a/core/logging.php b/core/logging.php index 267adc733..2a883ce46 100644 --- a/core/logging.php +++ b/core/logging.php @@ -8,28 +8,27 @@ * Logging convenience * \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -define("SCORE_LOG_CRITICAL", 50); -define("SCORE_LOG_ERROR", 40); -define("SCORE_LOG_WARNING", 30); -define("SCORE_LOG_INFO", 20); -define("SCORE_LOG_DEBUG", 10); -define("SCORE_LOG_NOTSET", 0); +enum LogLevel: int +{ + case NOT_SET = 0; + case DEBUG = 10; + case INFO = 20; + case WARNING = 30; + case ERROR = 40; + case CRITICAL = 50; -const LOGGING_LEVEL_NAMES = [ - SCORE_LOG_NOTSET => "Not Set", - SCORE_LOG_DEBUG => "Debug", - SCORE_LOG_INFO => "Info", - SCORE_LOG_WARNING => "Warning", - SCORE_LOG_ERROR => "Error", - SCORE_LOG_CRITICAL => "Critical", -]; -const LOGGING_LEVEL_NAMES_TO_LEVELS = [ - LOGGING_LEVEL_NAMES[SCORE_LOG_DEBUG] => SCORE_LOG_DEBUG, - LOGGING_LEVEL_NAMES[SCORE_LOG_INFO] => SCORE_LOG_INFO, - LOGGING_LEVEL_NAMES[SCORE_LOG_WARNING] => SCORE_LOG_WARNING, - LOGGING_LEVEL_NAMES[SCORE_LOG_ERROR] => SCORE_LOG_ERROR, - LOGGING_LEVEL_NAMES[SCORE_LOG_CRITICAL] => SCORE_LOG_CRITICAL, -]; + /** + * @return array + */ + public static function names_to_levels(): array + { + $ret = []; + foreach (LogLevel::cases() as $case) { + $ret[$case->name] = $case->value; + } + return $ret; + } +} /** * A shorthand way to send a LogEvent @@ -38,13 +37,13 @@ * When taking action, a log event should be stored by the server * Quite often, both of these happen at once, hence log_*() having $flash */ -function log_msg(string $section, int $priority, string $message, ?string $flash = null): void +function log_msg(string $section, LogLevel $priority, string $message, ?string $flash = null): void { global $page; - send_event(new LogEvent($section, $priority, $message)); + send_event(new LogEvent($section, $priority->value, $message)); $threshold = defined("CLI_LOG_LEVEL") ? CLI_LOG_LEVEL : 0; - if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') && ($priority >= $threshold)) { + if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') && ($priority->value >= $threshold)) { print date("c")." $section: $message\n"; ob_flush(); } @@ -56,23 +55,23 @@ function log_msg(string $section, int $priority, string $message, ?string $flash // More shorthand ways of logging function log_debug(string $section, string $message, ?string $flash = null): void { - log_msg($section, SCORE_LOG_DEBUG, $message, $flash); + log_msg($section, LogLevel::DEBUG, $message, $flash); } function log_info(string $section, string $message, ?string $flash = null): void { - log_msg($section, SCORE_LOG_INFO, $message, $flash); + log_msg($section, LogLevel::INFO, $message, $flash); } function log_warning(string $section, string $message, ?string $flash = null): void { - log_msg($section, SCORE_LOG_WARNING, $message, $flash); + log_msg($section, LogLevel::WARNING, $message, $flash); } function log_error(string $section, string $message, ?string $flash = null): void { - log_msg($section, SCORE_LOG_ERROR, $message, $flash); + log_msg($section, LogLevel::ERROR, $message, $flash); } function log_critical(string $section, string $message, ?string $flash = null): void { - log_msg($section, SCORE_LOG_CRITICAL, $message, $flash); + log_msg($section, LogLevel::CRITICAL, $message, $flash); } diff --git a/ext/cron_uploader/main.php b/ext/cron_uploader/main.php index f268ef3e9..44b90134b 100644 --- a/ext/cron_uploader/main.php +++ b/ext/cron_uploader/main.php @@ -27,7 +27,7 @@ public function onInitUserConfig(InitUserConfigEvent $event): void ); $event->user_config->set_default_bool(CronUploaderConfig::INCLUDE_ALL_LOGS, false); $event->user_config->set_default_bool(CronUploaderConfig::STOP_ON_ERROR, false); - $event->user_config->set_default_int(CronUploaderConfig::LOG_LEVEL, SCORE_LOG_INFO); + $event->user_config->set_default_int(CronUploaderConfig::LOG_LEVEL, LogLevel::INFO->value); } public function onUserOptionsBuilding(UserOptionsBuildingEvent $event): void @@ -39,7 +39,7 @@ public function onUserOptionsBuilding(UserOptionsBuildingEvent $event): void $sb->start_table(); $sb->add_text_option(CronUploaderConfig::DIR, "Root dir", true); $sb->add_bool_option(CronUploaderConfig::STOP_ON_ERROR, "Stop On Error", true); - $sb->add_choice_option(CronUploaderConfig::LOG_LEVEL, LOGGING_LEVEL_NAMES_TO_LEVELS, "Output Log Level: ", true); + $sb->add_choice_option(CronUploaderConfig::LOG_LEVEL, LogLevel::names_to_levels(), "Output Log Level: ", true); $sb->add_bool_option(CronUploaderConfig::INCLUDE_ALL_LOGS, "Include All Logs", true); $sb->end_table(); $sb->add_label("Read the documentation for cron setup instructions."); @@ -116,7 +116,7 @@ public function onLog(LogEvent $event): void $all = $user->get_config()->get_bool(CronUploaderConfig::INCLUDE_ALL_LOGS); if ($event->priority >= $user->get_config()->get_int(CronUploaderConfig::LOG_LEVEL) && ($event->section == self::NAME || $all)) { - $output = "[" . date('Y-m-d H:i:s') . "] " . ($all ? '[' . $event->section . '] ' : '') . "[" . LOGGING_LEVEL_NAMES[$event->priority] . "] " . $event->message; + $output = "[" . date('Y-m-d H:i:s') . "] " . ($all ? '[' . $event->section . '] ' : '') . "[" . LogLevel::from($event->priority)->name . "] " . $event->message; echo $output . "\r\n"; flush_output(); @@ -325,7 +325,7 @@ public function process_upload(): bool throw new UserError("User not present. Please specify the api_key for the user to run cron upload as."); } - $this->log_message(SCORE_LOG_INFO, "Logged in as user {$user->name}"); + log_info(self::NAME, "Logged in as user {$user->name}"); if (!$user->can(Permissions::CRON_RUN)) { throw new PermissionDenied("User does not have permission to run cron upload"); @@ -357,11 +357,11 @@ public function process_upload(): bool break; } else { $remaining = $max_time - $execution_time; - $this->log_message(SCORE_LOG_DEBUG, "Max run time remaining: $remaining"); + log_debug(self::NAME, "Max run time remaining: $remaining"); } try { $result = $database->with_savepoint(function () use ($img, $output_subdir) { - $this->log_message(SCORE_LOG_INFO, "Adding file: {$img[0]} - tags: {$img[2]}"); + log_info(self::NAME, "Adding file: {$img[0]} - tags: {$img[2]}"); $result = $this->add_image($img[0], $img[1], $img[2]); $this->move_uploaded($img[0], $img[1], $output_subdir, false); return $result; @@ -373,8 +373,8 @@ public function process_upload(): bool } } catch (\Exception $e) { $failed++; - $this->log_message(SCORE_LOG_ERROR, "(" . gettype($e) . ") " . $e->getMessage()); - $this->log_message(SCORE_LOG_ERROR, $e->getTraceAsString()); + log_error(self::NAME, "(" . gettype($e) . ") " . $e->getMessage()); + log_error(self::NAME, $e->getTraceAsString()); if ($user->get_config()->get_bool(CronUploaderConfig::STOP_ON_ERROR)) { break; } else { @@ -385,13 +385,13 @@ public function process_upload(): bool // Throw exception if there's nothing in the queue if ($merged + $failed + $added === 0) { - $this->log_message(SCORE_LOG_WARNING, "Your queue is empty so nothing could be uploaded."); + log_warning(self::NAME, "Your queue is empty so nothing could be uploaded."); return false; } - $this->log_message(SCORE_LOG_INFO, "Items added: $added"); - $this->log_message(SCORE_LOG_INFO, "Items merged: $merged"); - $this->log_message(SCORE_LOG_INFO, "Items failed: $failed"); + log_info(self::NAME, "Items added: $added"); + log_info(self::NAME, "Items merged: $merged"); + log_info(self::NAME, "Items failed: $failed"); return true; @@ -437,7 +437,7 @@ private function move_uploaded(string $path, string $filename, string $output_su // move file to correct dir rename($path, $newFile); - $this->log_message(SCORE_LOG_INFO, $info . "Post \"$filename\" moved from queue to \"$newDir\"."); + log_info(self::NAME, $info . "Post \"$filename\" moved from queue to \"$newDir\"."); } /** @@ -459,7 +459,7 @@ private function add_image(string $tmpname, string $filename, array $tags): Data } else { $infomsg = "Post uploaded. ID: {$event->images[0]->id} - Filename: {$filename}"; } - $this->log_message(SCORE_LOG_INFO, $infomsg); + log_info(self::NAME, $infomsg); return $event; } @@ -487,7 +487,7 @@ private function generate_image_queue(): \Generator $base = $this->get_queue_dir(); if (!is_dir($base)) { - $this->log_message(SCORE_LOG_WARNING, "Post Queue Directory could not be found at \"$base\"."); + log_warning(self::NAME, "Post Queue Directory could not be found at \"$base\"."); return; } @@ -506,12 +506,6 @@ private function generate_image_queue(): \Generator } } - - private function log_message(int $severity, string $message): void - { - log_msg(self::NAME, $severity, $message); - } - private function get_log_file(): string { global $user; diff --git a/ext/log_console/main.php b/ext/log_console/main.php index 624d26440..0c920e3c2 100644 --- a/ext/log_console/main.php +++ b/ext/log_console/main.php @@ -11,7 +11,7 @@ public function onInitExt(InitExtEvent $event): void global $config; $config->set_default_bool(LogConsoleConfig::LOG_ACCESS, true); $config->set_default_bool(LogConsoleConfig::COLOUR, true); - $config->set_default_int(LogConsoleConfig::LEVEL, SCORE_LOG_INFO); + $config->set_default_int(LogConsoleConfig::LEVEL, LogLevel::INFO->value); } public function onSetupBuilding(SetupBuildingEvent $event): void @@ -19,7 +19,7 @@ public function onSetupBuilding(SetupBuildingEvent $event): void $sb = $event->panel->create_new_block("Logging (Console)"); $sb->add_bool_option(LogConsoleConfig::LOG_ACCESS, "Log page requests: "); $sb->add_bool_option(LogConsoleConfig::COLOUR, "
Log with colour: "); - $sb->add_choice_option(LogConsoleConfig::LEVEL, LOGGING_LEVEL_NAMES_TO_LEVELS, "
Log Level: "); + $sb->add_choice_option(LogConsoleConfig::LEVEL, LogLevel::names_to_levels(), "
Log Level: "); } public function onPageRequest(PageRequestEvent $event): void @@ -32,7 +32,7 @@ public function onPageRequest(PageRequestEvent $event): void ) { $this->log(new LogEvent( "access", - SCORE_LOG_INFO, + LogLevel::INFO->value, "{$_SERVER['REQUEST_METHOD']} {$_SERVER['REQUEST_URI']}" )); } @@ -67,19 +67,19 @@ private function log(LogEvent $event): void $levelName = "[unknown]"; $color = "\033[0;35m"; # purple for unknown levels - if ($event->priority >= SCORE_LOG_CRITICAL) { + if ($event->priority >= LogLevel::CRITICAL->value) { $levelName = "[critical]"; $color = "\033[0;31m"; # red - } elseif ($event->priority >= SCORE_LOG_ERROR) { + } elseif ($event->priority >= LogLevel::ERROR->value) { $levelName = "[error]"; $color = "\033[0;91m"; # high intensity red - } elseif ($event->priority >= SCORE_LOG_WARNING) { + } elseif ($event->priority >= LogLevel::WARNING->value) { $levelName = "[warning]"; $color = "\033[0;33m"; # yellow - } elseif ($event->priority >= SCORE_LOG_INFO) { + } elseif ($event->priority >= LogLevel::INFO->value) { $levelName = "[info]"; $color = ""; # none for info - } elseif ($event->priority >= SCORE_LOG_DEBUG) { + } elseif ($event->priority >= LogLevel::DEBUG->value) { $levelName = "[debug]"; $color = "\033[0;94m"; # high intensity blue } diff --git a/ext/log_db/main.php b/ext/log_db/main.php index 96e9cc5ab..54ffef581 100644 --- a/ext/log_db/main.php +++ b/ext/log_db/main.php @@ -118,16 +118,9 @@ public function read_input(array $inputs): HTMLElement ]) ); - $options = [ - "Debug" => SCORE_LOG_DEBUG, - "Info" => SCORE_LOG_INFO, - "Warning" => SCORE_LOG_WARNING, - "Error" => SCORE_LOG_ERROR, - "Critical" => SCORE_LOG_CRITICAL, - ]; $s = SELECT(["name" => "r_{$this->name}[]"]); $s->appendChild(OPTION(["value" => ""], '-')); - foreach ($options as $k => $v) { + foreach (LogLevel::names_to_levels() as $k => $v) { $attrs = ["value" => $v]; if ($v == @$inputs["r_{$this->name}"][1]) { $attrs["selected"] = true; @@ -155,24 +148,14 @@ public function modify_input_for_read(array|string $input): mixed public function display(array $row): HTMLElement { - $c = "#000"; - switch ($row['priority']) { - case SCORE_LOG_DEBUG: - $c = "#999"; - break; - case SCORE_LOG_INFO: - $c = "#000"; - break; - case SCORE_LOG_WARNING: - $c = "#800"; - break; - case SCORE_LOG_ERROR: - $c = "#C00"; - break; - case SCORE_LOG_CRITICAL: - $c = "#F00"; - break; - } + $c = match ($row['priority']) { + LogLevel::DEBUG->value => "#999", + LogLevel::INFO->value => "#000", + LogLevel::WARNING->value => "#800", + LogLevel::ERROR->value => "#C00", + LogLevel::CRITICAL->value => "#F00", + default => "#000", + }; return SPAN(["style" => "color: $c"], rawHTML($this->scan_entities($row[$this->name]))); } @@ -223,7 +206,7 @@ class LogDatabase extends Extension public function onInitExt(InitExtEvent $event): void { global $config; - $config->set_default_int("log_db_priority", SCORE_LOG_INFO); + $config->set_default_int(LogDatabaseConfig::LEVEL, LogLevel::INFO->value); } public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void @@ -248,7 +231,7 @@ public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void public function onSetupBuilding(SetupBuildingEvent $event): void { $sb = $event->panel->create_new_block("Logging (Database)"); - $sb->add_choice_option(LogDatabaseConfig::LEVEL, LOGGING_LEVEL_NAMES_TO_LEVELS, "Log Level: "); + $sb->add_choice_option(LogDatabaseConfig::LEVEL, LogLevel::names_to_levels(), "Log Level: "); } public function onPageRequest(PageRequestEvent $event): void diff --git a/tests/phpstan.neon b/tests/phpstan.neon index 8152c0914..43427bfe4 100644 --- a/tests/phpstan.neon +++ b/tests/phpstan.neon @@ -10,7 +10,6 @@ parameters: - TRUSTED_PROXIES - TIMEZONE - BASE_HREF - - STATSD_HOST - TRACE_FILE - UNITTEST ignoreErrors: