Compressed logs from uploads
This commit is contained in:
parent
18f7fff21f
commit
fdd49d783a
2 changed files with 71 additions and 31 deletions
|
@ -199,6 +199,7 @@ class PostController
|
||||||
R::transaction(function()
|
R::transaction(function()
|
||||||
{
|
{
|
||||||
$post = Model_Post::create();
|
$post = Model_Post::create();
|
||||||
|
LogHelper::bufferChanges();
|
||||||
|
|
||||||
//basic stuff
|
//basic stuff
|
||||||
$anonymous = InputHelper::get('anonymous');
|
$anonymous = InputHelper::get('anonymous');
|
||||||
|
@ -208,24 +209,29 @@ class PostController
|
||||||
//store the post to get the ID in the logs
|
//store the post to get the ID in the logs
|
||||||
Model_Post::save($post);
|
Model_Post::save($post);
|
||||||
|
|
||||||
//log
|
//do the edits
|
||||||
LogHelper::bufferChanges();
|
|
||||||
$fmt = ($anonymous and !$this->config->misc->logAnonymousUploads)
|
|
||||||
? '{anon}'
|
|
||||||
: '{user}';
|
|
||||||
$fmt .= ' added {post}';
|
|
||||||
LogHelper::log($fmt, ['post' => TextHelper::reprPost($post)]);
|
|
||||||
|
|
||||||
//after logging basic info, do the editing stuff
|
|
||||||
$this->doEdit($post, true);
|
$this->doEdit($post, true);
|
||||||
|
|
||||||
//this basically means that user didn't specify file nor url
|
//this basically means that user didn't specify file nor url
|
||||||
if (empty($post->type))
|
if (empty($post->type))
|
||||||
throw new SimpleException('No post type detected; upload faled');
|
throw new SimpleException('No post type detected; upload faled');
|
||||||
|
|
||||||
LogHelper::flush();
|
//clean edit log
|
||||||
|
LogHelper::setBuffer([]);
|
||||||
|
|
||||||
|
//log
|
||||||
|
$fmt = ($anonymous and !$this->config->misc->logAnonymousUploads)
|
||||||
|
? '{anon}'
|
||||||
|
: '{user}';
|
||||||
|
$fmt .= ' added {post} (tags: {tags}, safety: {safety}, source: {source})';
|
||||||
|
LogHelper::log($fmt, [
|
||||||
|
'post' => TextHelper::reprPost($post),
|
||||||
|
'tags' => join(', ', array_map(['TextHelper', 'reprTag'], $post->sharedTag)),
|
||||||
|
'safety' => PostSafety::toString($post->safety),
|
||||||
|
'source' => $post->source]);
|
||||||
|
|
||||||
//finish
|
//finish
|
||||||
|
LogHelper::flush();
|
||||||
Model_Post::save($post);
|
Model_Post::save($post);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -5,16 +5,14 @@ class LogHelper
|
||||||
static $context;
|
static $context;
|
||||||
static $config;
|
static $config;
|
||||||
static $autoFlush;
|
static $autoFlush;
|
||||||
static $content;
|
static $buffer;
|
||||||
|
|
||||||
public static function init()
|
public static function init()
|
||||||
{
|
{
|
||||||
self::$config = \Chibi\Registry::getConfig();
|
self::$path = \Chibi\Registry::getConfig()->main->logsPath . date('Y-m') . '.log';
|
||||||
self::$context = \Chibi\Registry::getContext();
|
|
||||||
self::$path = self::$config->main->logsPath . date('Y-m') . '.log';
|
|
||||||
self::$autoFlush = true;
|
self::$autoFlush = true;
|
||||||
|
|
||||||
self::$content = '';
|
self::$buffer = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function bufferChanges()
|
public static function bufferChanges()
|
||||||
|
@ -29,12 +27,13 @@ class LogHelper
|
||||||
throw new SimpleException('Cannot write to log files');
|
throw new SimpleException('Cannot write to log files');
|
||||||
if (flock($fh, LOCK_EX))
|
if (flock($fh, LOCK_EX))
|
||||||
{
|
{
|
||||||
fwrite($fh, self::$content);
|
foreach (self::$buffer as $logEvent)
|
||||||
|
fwrite($fh, $logEvent->getFullText() . PHP_EOL);
|
||||||
fflush($fh);
|
fflush($fh);
|
||||||
flock($fh, LOCK_UN);
|
flock($fh, LOCK_UN);
|
||||||
fclose($fh);
|
fclose($fh);
|
||||||
}
|
}
|
||||||
self::$content = '';
|
self::$buffer = [];
|
||||||
self::$autoFlush = true;
|
self::$autoFlush = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,23 +44,58 @@ class LogHelper
|
||||||
|
|
||||||
public static function log($text, array $tokens = [])
|
public static function log($text, array $tokens = [])
|
||||||
{
|
{
|
||||||
$tokens['anon'] = Model_User::getAnonymousName();
|
self::$buffer []= new LogEvent($text, $tokens);
|
||||||
if (self::$context->loggedIn and isset(self::$context->user))
|
|
||||||
$tokens['user'] = TextHelper::reprUser(self::$context->user->name);
|
|
||||||
else
|
|
||||||
$tokens['user'] = $tokens['anon'];
|
|
||||||
|
|
||||||
$text = TextHelper::replaceTokens($text, $tokens);
|
|
||||||
|
|
||||||
$timestamp = date('Y-m-d H:i:s');
|
|
||||||
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
|
|
||||||
$line = sprintf('[%s] %s: %s' . PHP_EOL, $timestamp, $ip, $text);
|
|
||||||
|
|
||||||
self::$content .= $line;
|
|
||||||
|
|
||||||
if (self::$autoFlush)
|
if (self::$autoFlush)
|
||||||
self::flush();
|
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();
|
||||||
|
$tokens['anon'] = Model_User::getAnonymousName();
|
||||||
|
if ($context->loggedIn and isset($context->user))
|
||||||
|
$tokens['user'] = TextHelper::reprUser($context->user->name);
|
||||||
|
else
|
||||||
|
$tokens['user'] = $tokens['anon'];
|
||||||
|
$this->tokens = $tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getText()
|
||||||
|
{
|
||||||
|
return TextHelper::replaceTokens($this->text, $this->tokens);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LogHelper::init();
|
LogHelper::init();
|
||||||
|
|
Loading…
Reference in a new issue