From dc0abf8c7c91d3a73eb8c5964e8d46ac3018148f Mon Sep 17 00:00:00 2001 From: Greg Sarjeant Date: Thu, 31 Jul 2025 13:19:35 +0000 Subject: [PATCH] Add debug logging to feeds. Add route info to all debug logs. (#39) Reviewed-on: https://gitea.subcultureofone.org/greg/tkr/pulls/39 Co-authored-by: Greg Sarjeant Co-committed-by: Greg Sarjeant --- public/index.php | 3 +++ src/Controller/FeedController/FeedController.php | 2 ++ src/Feed/AtomGenerator.php | 3 +++ src/Feed/RssGenerator.php | 3 +++ src/Framework/Log/Log.php | 8 +++++++- 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/public/index.php b/public/index.php index 733cadb..54b63af 100644 --- a/public/index.php +++ b/public/index.php @@ -67,6 +67,9 @@ if (strpos($path, $config->basePath) === 0) { // strip the trailing slash from the resulting route $path = trim($path, '/'); + +// Set route context for logging +Log::setRouteContext("$method $path"); Log::debug("Path requested: {$path}"); // if this is a POST and we aren't in setup, diff --git a/src/Controller/FeedController/FeedController.php b/src/Controller/FeedController/FeedController.php index dbaa787..223e6f9 100644 --- a/src/Controller/FeedController/FeedController.php +++ b/src/Controller/FeedController/FeedController.php @@ -13,6 +13,7 @@ class FeedController extends Controller { public function rss(){ $generator = new RssGenerator($this->config, $this->ticks); + Log::debug("Generating RSS feed with " . count($this->ticks) . " ticks"); header('Content-Type: ' . $generator->getContentType()); echo $generator->generate(); @@ -20,6 +21,7 @@ class FeedController extends Controller { public function atom(){ $generator = new AtomGenerator($this->config, $this->ticks); + Log::debug("Generating Atom feed with " . count($this->ticks) . " ticks"); header('Content-Type: ' . $generator->getContentType()); echo $generator->generate(); diff --git a/src/Feed/AtomGenerator.php b/src/Feed/AtomGenerator.php index 8411ad2..a00e908 100644 --- a/src/Feed/AtomGenerator.php +++ b/src/Feed/AtomGenerator.php @@ -3,6 +3,8 @@ class AtomGenerator extends FeedGenerator { public function generate(): string { $xml = '' . "\n"; $xml .= $this->buildFeed(); + + Log::debug("Generated Atom feed: " . strlen($xml) . " bytes"); return $xml; } @@ -11,6 +13,7 @@ class AtomGenerator extends FeedGenerator { } private function buildFeed(): string { + Log::debug("Building Atom feed for " . $this->config->siteTitle); $feedTitle = Util::escape_xml($this->config->siteTitle . " Atom Feed"); $siteUrl = Util::escape_xml(Util::buildUrl($this->config->baseUrl, $this->config->basePath)); $feedUrl = Util::escape_xml(Util::buildUrl($this->config->baseUrl, $this->config->basePath, 'feed/atom')); diff --git a/src/Feed/RssGenerator.php b/src/Feed/RssGenerator.php index 3a7e677..45739ab 100644 --- a/src/Feed/RssGenerator.php +++ b/src/Feed/RssGenerator.php @@ -5,6 +5,8 @@ class RssGenerator extends FeedGenerator { $xml .= '' . "\n"; $xml .= $this->buildChannel(); $xml .= '' . "\n"; + + Log::debug("Generated RSS feed: " . strlen($xml) . " bytes"); return $xml; } @@ -13,6 +15,7 @@ class RssGenerator extends FeedGenerator { } private function buildChannel(): string { + Log::debug("Building RSS channel for " . $this->config->siteTitle); ob_start(); ?> diff --git a/src/Framework/Log/Log.php b/src/Framework/Log/Log.php index 25158c4..7cc414e 100644 --- a/src/Framework/Log/Log.php +++ b/src/Framework/Log/Log.php @@ -10,6 +10,7 @@ class Log { private static $logFile; private static $maxLines = 1000; private static $maxFiles = 5; + private static $routeContext = ''; public static function init() { self::$logFile = STORAGE_DIR . '/logs/tkr.log'; @@ -22,6 +23,10 @@ class Log { } } + public static function setRouteContext(string $route): void { + self::$routeContext = $route ? "[$route]" : ''; + } + public static function debug($message) { self::write('DEBUG', $message); } @@ -52,7 +57,8 @@ class Log { } $timestamp = date('Y-m-d H:i:s'); - $logEntry = "[{$timestamp}] {$level}: " . Util::getClientIp() . " - {$message}\n"; + $context = self::$routeContext ? ' ' . self::$routeContext : ''; + $logEntry = "[{$timestamp}] {$level}: " . Util::getClientIp() . "{$context} - {$message}\n"; // Rotate if we're at the max file size (1000 lines) if (file_exists(self::$logFile)) {