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 <greg@subcultureofone.org>
Co-committed-by: Greg Sarjeant <greg@subcultureofone.org>
This commit is contained in:
Greg Sarjeant 2025-07-31 13:19:35 +00:00 committed by greg
parent 8b5a249450
commit dc0abf8c7c
5 changed files with 18 additions and 1 deletions

View File

@ -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,

View File

@ -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();

View File

@ -3,6 +3,8 @@ class AtomGenerator extends FeedGenerator {
public function generate(): string {
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\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'));

View File

@ -5,6 +5,8 @@ class RssGenerator extends FeedGenerator {
$xml .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">' . "\n";
$xml .= $this->buildChannel();
$xml .= '</rss>' . "\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();
?>
<channel>

View File

@ -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)) {