From f254e7bb1e9085a523812003043f4617cf0431e8 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 5 May 2014 09:58:18 +0200 Subject: [PATCH] Logger path accepts simple templates --- data/config.ini | 2 +- src/Api/Jobs/GetLogJob.php | 2 +- src/Api/Jobs/ListLogsJob.php | 2 +- src/Logger.php | 15 +++++++++++++-- tests/MiscTest.php | 30 ++++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/data/config.ini b/data/config.ini index e4740d27..7b786abb 100644 --- a/data/config.ini +++ b/data/config.ini @@ -8,7 +8,7 @@ dbUser = "test" dbPass = "test" filesPath = "./data/files/" thumbsPath = "./data/thumbs/" -logsPath = "./data/logs/" +logsPath = "./data/logs/{yyyy-mm}.log" mediaPath = "./public_html/media/" title = "szurubooru" salt = "1A2/$_4xVa" diff --git a/src/Api/Jobs/GetLogJob.php b/src/Api/Jobs/GetLogJob.php index a82aa7ff..e033b4a7 100644 --- a/src/Api/Jobs/GetLogJob.php +++ b/src/Api/Jobs/GetLogJob.php @@ -11,7 +11,7 @@ class GetLogJob extends AbstractPageJob //parse input $page = max(1, intval($page)); $name = str_replace(['/', '\\'], '', $name); //paranoia mode - $path = TextHelper::absolutePath(getConfig()->main->logsPath . DS . $name); + $path = TextHelper::absolutePath(dirname(getConfig()->main->logsPath) . DS . $name); if (!file_exists($path)) throw new SimpleNotFoundException('Specified log doesn\'t exist'); diff --git a/src/Api/Jobs/ListLogsJob.php b/src/Api/Jobs/ListLogsJob.php index 48e0434f..8d812346 100644 --- a/src/Api/Jobs/ListLogsJob.php +++ b/src/Api/Jobs/ListLogsJob.php @@ -6,7 +6,7 @@ class ListLogsJob extends AbstractJob $path = TextHelper::absolutePath(getConfig()->main->logsPath); $logs = []; - foreach (glob($path . DS . '*.log') as $log) + foreach (glob(dirname($path) . DS . '*.log') as $log) $logs []= basename($log); usort($logs, function($a, $b) diff --git a/src/Logger.php b/src/Logger.php index 9b46d629..2e8f83bf 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -5,11 +5,18 @@ class Logger static $config; static $autoFlush; static $buffer; + static $path; public static function init() { self::$autoFlush = true; self::$buffer = []; + self::$path = self::getLogPath(); + $dir = dirname(self::$path); + if (!is_dir($dir)) + mkdir($dir, 0777, true); + #if (!is_writable(self::$path)) + # throw new SimpleException('Cannot write logs to "' . self::$path . '". Check access rights.'); } public static function bufferChanges() @@ -19,7 +26,7 @@ class Logger public static function flush() { - $fh = fopen(self::getLogPath(), 'ab'); + $fh = fopen(self::$path, 'ab'); if (!$fh) throw new SimpleException('Cannot write to log files'); if (flock($fh, LOCK_EX)) @@ -36,7 +43,11 @@ class Logger public static function getLogPath() { - return TextHelper::absolutePath(getConfig()->main->logsPath . DS . date('Y-m') . '.log'); + return TextHelper::absolutePath( + TextHelper::replaceTokens(getConfig()->main->logsPath, [ + 'yyyy' => date('Y'), + 'mm' => date('m'), + 'dd' => date('d')])); } public static function log($text, array $tokens = []) diff --git a/tests/MiscTest.php b/tests/MiscTest.php index 47795911..cd6b791d 100644 --- a/tests/MiscTest.php +++ b/tests/MiscTest.php @@ -16,4 +16,34 @@ class MiscTest extends AbstractTest $this->assert->areEqual($text, TextHelper::decrypt(TextHelper::encrypt($text))); } } + + public function testLogging() + { + $logPath = __DIR__ . '/logs/{yyyy}-{mm}-{dd}.log'; + $realLogPath = __DIR__ . '/logs/' . date('Y-m-d') . '.log'; + + try + { + getConfig()->main->logsPath = $logPath; + $this->assert->doesNotThrow(function() + { + Logger::init(); + }); + + $this->assert->isFalse(file_exists($realLogPath)); + $this->assert->doesNotThrow(function() + { + Logger::log('Simple text'); + }); + $this->assert->isTrue(file_exists($realLogPath)); + + $x = file_get_contents($realLogPath); + $this->assert->isTrue(strpos($x, 'Simple text') !== false); + } + finally + { + if (file_exists($realLogPath)) + unlink($realLogPath); + } + } }