Logger path accepts simple templates

This commit is contained in:
Marcin Kurczewski 2014-05-05 09:58:18 +02:00
parent c64d97fae6
commit f254e7bb1e
5 changed files with 46 additions and 5 deletions

View file

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

View file

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

View file

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

View file

@ -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 = [])

View file

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