Skip to content

Commit

Permalink
Use an enum for log levels
Browse files Browse the repository at this point in the history
Avoiding a random handful of disconnected constants in the global namespace
  • Loading branch information
shish committed Feb 11, 2025
1 parent d5a8176 commit 363a028
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 92 deletions.
10 changes: 5 additions & 5 deletions core/cli_app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
57 changes: 28 additions & 29 deletions core/logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, int>
*/
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
Expand All @@ -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();
}
Expand All @@ -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);
}


Expand Down
36 changes: 15 additions & 21 deletions ext/cron_uploader/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("<a href='$documentation_link'>Read the documentation</a> for cron setup instructions.");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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\".");
}

/**
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions ext/log_console/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ 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
{
$sb = $event->panel->create_new_block("Logging (Console)");
$sb->add_bool_option(LogConsoleConfig::LOG_ACCESS, "Log page requests: ");
$sb->add_bool_option(LogConsoleConfig::COLOUR, "<br>Log with colour: ");
$sb->add_choice_option(LogConsoleConfig::LEVEL, LOGGING_LEVEL_NAMES_TO_LEVELS, "<br>Log Level: ");
$sb->add_choice_option(LogConsoleConfig::LEVEL, LogLevel::names_to_levels(), "<br>Log Level: ");
}

public function onPageRequest(PageRequestEvent $event): void
Expand All @@ -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']}"
));
}
Expand Down Expand Up @@ -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
}
Expand Down
39 changes: 11 additions & 28 deletions ext/log_db/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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])));
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ parameters:
- TRUSTED_PROXIES
- TIMEZONE
- BASE_HREF
- STATSD_HOST
- TRACE_FILE
- UNITTEST
ignoreErrors:
Expand Down

0 comments on commit 363a028

Please sign in to comment.