From 2b9a348ef85cb0ef58951143ae4251a653fee2a2 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 16:22:56 +0000 Subject: [PATCH] Default the bot's log level to info and read log_level from the profile The launcher's Civilizationbot tab can now set the portable profile's log_level (debug, info, notice, warning, error); LOG_LEVEL in the environment is the fallback, then info. An unknown value logs a warning and uses info, so it never stops the bot from starting. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016WTP2RCuxRvGvvpCDsWSAZ --- .github/workflows/local-profile.yml | 2 ++ bot.php | 10 ++++++++-- docs/windows-test1714.md | 5 +++++ src/Civ13/Civ13.php | 4 ++-- src/Civ13/LogLevelSetting.php | 26 ++++++++++++++++++++++++++ tests/local/log-level.php | 21 +++++++++++++++++++++ tests/windows/bootstrap.php | 4 ++-- tests/windows/default-branch.php | 2 +- 8 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 src/Civ13/LogLevelSetting.php create mode 100644 tests/local/log-level.php diff --git a/.github/workflows/local-profile.yml b/.github/workflows/local-profile.yml index 8b93767d..73d2d395 100644 --- a/.github/workflows/local-profile.yml +++ b/.github/workflows/local-profile.yml @@ -18,6 +18,7 @@ on: - 'src/Civ13/AccessFileSync.php' - 'src/Civ13/AccessListProjection.php' - 'src/Civ13/PortableProfile.php' + - 'src/Civ13/LogLevelSetting.php' - 'src/Civ13/HostedVerificationSource.php' - 'src/Civ13/HostedVerificationSnapshot.php' - 'src/Civ13/Verifier.php' @@ -65,6 +66,7 @@ jobs: extensions: sodium coverage: none - run: php tests/local/run.php + - run: php tests/local/log-level.php - run: php tests/local/admin-sync-projection.php - run: php tests/local/admin-file-sync-profile.php - run: php tests/local/admin-file-writer.php diff --git a/bot.php b/bot.php index d91b8a91..68444fb9 100644 --- a/bot.php +++ b/bot.php @@ -99,6 +99,7 @@ function loadEnv(string $filePath, bool $explicit = false): array require_once __DIR__.'/src/Civ13/PortableProfile.php'; require_once __DIR__.'/src/Civ13/PortableProcess.php'; require_once __DIR__.'/src/Civ13/DefaultBranchGuard.php'; +require_once __DIR__.'/src/Civ13/LogLevelSetting.php'; $portable = null; $portableLock = null; $portablePath = getenv('CIVILIZATIONBOT_PROFILE'); @@ -142,10 +143,15 @@ function loadEnv(string $filePath, bool $explicit = false): array if ($portable !== null && (!in_array('TOKEN', $loadedNames, true) || !getenv('TOKEN'))) { throw new \RuntimeException('Separate profile environment must explicitly supply TOKEN'); } if ($portable === null) { file_put_contents($baseDir.'/output.log', ''); } // Clear the contents of 'output.log' -$fileHandler = (new StreamHandler(($portable['state_dir'] ?? $baseDir).'/output.log', Level::Debug))->setFormatter(new LineFormatter(null, null, true, true, true)); -$stdoutHandler = (new StreamHandler('php://stdout', Level::Debug))->setFormatter(new LineFormatter(null, null, true, true, true)); +// Profile log_level (set from the launcher's Civilizationbot tab), else LOG_LEVEL, else info. Never blocks boot. +[$logLevelName, $logLevelWarning] = \Civ13\LogLevelSetting::resolve($portable['log_level'] ?? null, getenv('LOG_LEVEL')); +$logLevel = Level::fromName($logLevelName); +$fileHandler = (new StreamHandler(($portable['state_dir'] ?? $baseDir).'/output.log', $logLevel))->setFormatter(new LineFormatter(null, null, true, true, true)); +$stdoutHandler = (new StreamHandler('php://stdout', $logLevel))->setFormatter(new LineFormatter(null, null, true, true, true)); $logger = new Logger('Civ13', [$fileHandler, $stdoutHandler]); Loop::addPeriodicTimer(60 * 10, fn () => $logger->reset()); // Flush all buffers every 10 minutes +if ($logLevelWarning !== null) { $logger->warning($logLevelWarning); } +$logger->notice('Log level: '.$logLevelName); $logger->info('Loading configurations for the bot...'); set_rejection_handler(function (\Throwable $e) use ($logger) { if ($e->getMessage() !== 'Cannot resume a fiber that is not suspended') { diff --git a/docs/windows-test1714.md b/docs/windows-test1714.md index 664cbef8..6d29f342 100644 --- a/docs/windows-test1714.md +++ b/docs/windows-test1714.md @@ -53,6 +53,11 @@ start never refuses because of the source check: it prints "Source check warning (starting anyway): ..." and boots the current checkout, so a guard problem can always be fixed from the launcher with Update or Repair and update. +Logging defaults to info. The optional top-level profile key `log_level` (debug, info, +notice, warning or error; the Civ13 launcher's Civilizationbot tab sets it) or the +`LOG_LEVEL` environment value changes it at the next start. An unknown value logs a +warning and uses info; it never stops the bot from starting. + status reports whether the state lock is held (running), any pending stop or reload marker, and the stdout/stderr log paths. check-update fetches origin/main like start does and reports current, available, behind, can_update and reason; diff --git a/src/Civ13/Civ13.php b/src/Civ13/Civ13.php index 3ee97a38..37870b3e 100644 --- a/src/Civ13/Civ13.php +++ b/src/Civ13/Civ13.php @@ -670,7 +670,7 @@ private function resolveOptions(array &$options = []): array $this->welcome_message = $options['welcome_message']; if (! $options['logger']) { - $streamHandler = new StreamHandler('php://stdout', Level::Debug); + $streamHandler = new StreamHandler('php://stdout', Level::Info); $streamHandler->setFormatter(new LineFormatter(null, null, true, true)); $options['logger'] = new Logger(self::class, [$streamHandler]); } @@ -867,7 +867,7 @@ private static function initialLogger(array $options): LoggerInterface return $logger; } - return new Logger(self::class, [new StreamHandler('php://stdout', Level::Debug)]); + return new Logger(self::class, [new StreamHandler('php://stdout', Level::Info)]); } /** The command prefix `$content` starts with (bot mention or `command_symbol`), or null. */ diff --git a/src/Civ13/LogLevelSetting.php b/src/Civ13/LogLevelSetting.php new file mode 100644 index 00000000..e9e70a66 --- /dev/null +++ b/src/Civ13/LogLevelSetting.php @@ -0,0 +1,26 @@ + $profileValue, 'LOG_LEVEL' => $environmentValue] as $source => $value) { + if ($value === null || $value === false || $value === '') { continue; } + $name = is_string($value) ? strtolower(trim($value)) : ''; + if (in_array($name, self::NAMES, true)) { return [$name, null]; } + return [self::DEFAULT, 'Unknown '.$source.' '.json_encode($value).'; using '.self::DEFAULT.' (expected one of '.implode(', ', self::NAMES).')']; + } + return [self::DEFAULT, null]; + } +} diff --git a/tests/local/log-level.php b/tests/local/log-level.php new file mode 100644 index 00000000..055bd43e --- /dev/null +++ b/tests/local/log-level.php @@ -0,0 +1,21 @@ +$app,'ip'=>'127.0.0.1','port'=>1714,'name'=>'fixture','host'=>'fixture','enabled'=>false];foreach(['discussion','playercount','ooc','lobby','asay','ic','transit','adminlog','debug','garbage','runtime','attack']as$key){$server[$key]='';}