2013-11-16 21:21:43 +01:00
|
|
|
<?php
|
|
|
|
class LogHelper
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2013-11-23 20:52:41 +01:00
|
|
|
return TextHelper::absolutePath(
|
|
|
|
\Chibi\Registry::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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LogEvent
|
|
|
|
{
|
|
|
|
public $timestamp;
|
|
|
|
public $text;
|
|
|
|
public $ip;
|
|
|
|
public $tokens;
|
|
|
|
|
|
|
|
public function __construct($text, array $tokens = [])
|
|
|
|
{
|
|
|
|
$this->timestamp = time();
|
|
|
|
$this->text = $text;
|
|
|
|
$this->ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
|
|
|
|
|
|
|
|
$context = \Chibi\Registry::getContext();
|
2013-11-22 23:27:55 +01:00
|
|
|
$tokens['anon'] = Model_User::getAnonymousName();
|
2013-11-23 13:35:23 +01:00
|
|
|
if ($context->loggedIn and isset($context->user))
|
|
|
|
$tokens['user'] = TextHelper::reprUser($context->user->name);
|
2013-11-22 23:27:55 +01:00
|
|
|
else
|
|
|
|
$tokens['user'] = $tokens['anon'];
|
2013-11-23 13:35:23 +01:00
|
|
|
$this->tokens = $tokens;
|
|
|
|
}
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2013-11-23 13:35:23 +01:00
|
|
|
public function getText()
|
|
|
|
{
|
|
|
|
return TextHelper::replaceTokens($this->text, $this->tokens);
|
|
|
|
}
|
2013-11-16 21:21:43 +01:00
|
|
|
|
2013-11-23 13:35:23 +01:00
|
|
|
public function getFullText()
|
|
|
|
{
|
|
|
|
$date = date('Y-m-d H:i:s', $this->timestamp);
|
|
|
|
$ip = $this->ip;
|
|
|
|
$text = $this->getText();
|
|
|
|
$line = sprintf('[%s] %s: %s', $date, $ip, $text);
|
|
|
|
return $line;
|
2013-11-16 21:21:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LogHelper::init();
|