szurubooru/src/Logger.php

62 lines
1.1 KiB
PHP
Raw Normal View History

2013-11-16 21:21:43 +01:00
<?php
2014-05-04 19:23:09 +02:00
class Logger
2013-11-16 21:21:43 +01:00
{
static $context;
static $config;
static $autoFlush;
2013-11-23 13:35:23 +01:00
static $buffer;
2013-11-16 21:21:43 +01:00
public static function init()
{
self::$autoFlush = true;
2013-11-23 13:35:23 +01:00
self::$buffer = [];
2013-11-16 21:21:43 +01:00
}
public static function bufferChanges()
{
self::$autoFlush = false;
}
public static function flush()
{
$fh = fopen(self::getLogPath(), 'ab');
if (!$fh)
throw new SimpleException('Cannot write to log files');
if (flock($fh, LOCK_EX))
{
2013-11-23 13:35:23 +01:00
foreach (self::$buffer as $logEvent)
fwrite($fh, $logEvent->getFullText() . PHP_EOL);
2013-11-16 21:21:43 +01:00
fflush($fh);
flock($fh, LOCK_UN);
fclose($fh);
}
2013-11-23 13:35:23 +01:00
self::$buffer = [];
2013-11-16 21:21:43 +01:00
self::$autoFlush = true;
}
public static function getLogPath()
{
2014-04-29 21:35:29 +02:00
return TextHelper::absolutePath(getConfig()->main->logsPath . DS . date('Y-m') . '.log');
2013-11-16 21:21:43 +01:00
}
public static function log($text, array $tokens = [])
{
2013-11-23 13:35:23 +01:00
self::$buffer []= new LogEvent($text, $tokens);
if (self::$autoFlush)
self::flush();
}
//methods for manipulating buffered logs
public static function getBuffer()
{
return self::$buffer;
}
public static function setBuffer(array $buffer)
{
self::$buffer = $buffer;
}
}
2014-05-04 19:23:09 +02:00
Logger::init();