Skip to content

Commit

Permalink
Merge pull request FreshRSS#1712 from kevinpapst/logfilesize
Browse files Browse the repository at this point in the history
Prevent logfile from growing unlimited
  • Loading branch information
Alkarex authored Dec 11, 2017
2 parents 8119a15 + b776f59 commit f13926c
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 78 deletions.
4 changes: 2 additions & 2 deletions app/Controllers/feedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public static function actualizeFeed($feed_id, $feed_url, $force, $simplePiePush
if ((!$simplePiePush) && (!$feed_id) && $pubSubHubbubEnabled && ($feed->lastUpdate() > $pshbMinAge)) {
//$text = 'Skip pull of feed using PubSubHubbub: ' . $url;
//Minz_Log::debug($text);
//file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
//Minz_Log::debug($text, PSHB_LOG);
continue; //When PubSubHubbub is used, do not pull refresh so often
}

Expand Down Expand Up @@ -371,7 +371,7 @@ public static function actualizeFeed($feed_id, $feed_url, $force, $simplePiePush

if ($pubSubHubbubEnabled && !$simplePiePush) { //We use push, but have discovered an article by pull!
$text = 'An article was discovered by pull although we use PubSubHubbub!: Feed ' . $url . ' GUID ' . $entry->guid();
file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
Minz_Log::warning($text, PSHB_LOG);
Minz_Log::warning($text);
$pubSubHubbubEnabled = false;
$feed->pubSubHubbubError(true);
Expand Down
14 changes: 6 additions & 8 deletions app/Models/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,7 @@ function pubSubHubbubError($error = true) {
if (!isset($hubJson['error']) || $hubJson['error'] !== (bool)$error) {
$hubJson['error'] = (bool)$error;
file_put_contents($hubFilename, json_encode($hubJson));
file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t"
. 'Set error to ' . ($error ? 1 : 0) . ' for ' . $url . "\n", FILE_APPEND);
Minz_Log::warning('Set error to ' . ($error ? 1 : 0) . ' for ' . $url, PSHB_LOG);
}
return false;
}
Expand All @@ -419,15 +418,15 @@ function pubSubHubbubPrepare() {
if (!$hubJson || empty($hubJson['key']) || !ctype_xdigit($hubJson['key'])) {
$text = 'Invalid JSON for PubSubHubbub: ' . $this->url;
Minz_Log::warning($text);
file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
Minz_Log::warning($text, PSHB_LOG);
return false;
}
if ((!empty($hubJson['lease_end'])) && ($hubJson['lease_end'] < (time() + (3600 * 23)))) { //TODO: Make a better policy
$text = 'PubSubHubbub lease ends at '
. date('c', empty($hubJson['lease_end']) ? time() : $hubJson['lease_end'])
. ' and needs renewal: ' . $this->url;
Minz_Log::warning($text);
file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
Minz_Log::warning($text, PSHB_LOG);
$key = $hubJson['key']; //To renew our lease
} elseif (((!empty($hubJson['error'])) || empty($hubJson['lease_end'])) &&
(empty($hubJson['lease_start']) || $hubJson['lease_start'] < time() - (3600 * 23))) { //Do not renew too often
Expand All @@ -445,7 +444,7 @@ function pubSubHubbubPrepare() {
file_put_contents(PSHB_PATH . '/keys/' . $key . '.txt', base64url_encode($this->selfUrl));
$text = 'PubSubHubbub prepared for ' . $this->url;
Minz_Log::debug($text);
file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
Minz_Log::debug($text, PSHB_LOG);
}
$currentUser = Minz_Session::param('currentUser');
if (FreshRSS_user_Controller::checkUsername($currentUser) && !file_exists($path . '/' . $currentUser . '.txt')) {
Expand Down Expand Up @@ -499,9 +498,8 @@ function pubSubHubbubSubscribe($state) {
$response = curl_exec($ch);
$info = curl_getinfo($ch);

file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" .
'PubSubHubbub ' . ($state ? 'subscribe' : 'unsubscribe') . ' to ' . $url .
' with callback ' . $callbackUrl . ': ' . $info['http_code'] . ' ' . $response . "\n", FILE_APPEND);
Minz_Log::warning('PubSubHubbub ' . ($state ? 'subscribe' : 'unsubscribe') . ' to ' . $url .
' with callback ' . $callbackUrl . ': ' . $info['http_code'] . ' ' . $response, PSHB_LOG);

if (substr($info['http_code'], 0, 1) == '2') {
return true;
Expand Down
6 changes: 3 additions & 3 deletions app/Models/LogDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public static function lines() {
public static function truncate() {
file_put_contents(join_path(DATA_PATH, 'users', Minz_Session::param('currentUser', '_'), 'log.txt'), '');
if (FreshRSS_Auth::hasAccess('admin')) {
file_put_contents(join_path(DATA_PATH, 'users', '_', 'log.txt'), '');
file_put_contents(join_path(DATA_PATH, 'users', '_', 'log_api.txt'), '');
file_put_contents(join_path(DATA_PATH, 'users', '_', 'log_pshb.txt'), '');
file_put_contents(ADMIN_LOG, '');
file_put_contents(API_LOG, '');
file_put_contents(PSHB_LOG, '');
}
}
}
11 changes: 4 additions & 7 deletions app/actualize_script.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
$_GET['force'] = true;
$_SERVER['HTTP_HOST'] = '';

$log_file = join_path(USERS_PATH, '_', 'log.txt');

$app = new FreshRSS();

$system_conf = Minz_Configuration::get('system');
Expand All @@ -42,13 +40,13 @@
foreach ($users as $user) {
if (($user !== $system_conf->default_user) &&
(FreshRSS_UserDAO::mtime($user) < $min_last_activity)) {
Minz_Log::notice('FreshRSS skip inactive user ' . $user, $log_file);
Minz_Log::notice('FreshRSS skip inactive user ' . $user, ADMIN_LOG);
if (defined('STDOUT')) {
fwrite(STDOUT, 'FreshRSS skip inactive user ' . $user . "\n"); //Unbuffered
}
continue;
}
Minz_Log::notice('FreshRSS actualize ' . $user, $log_file);
Minz_Log::notice('FreshRSS actualize ' . $user, ADMIN_LOG);
if (defined('STDOUT')) {
fwrite(STDOUT, 'Actualize ' . $user . "...\n"); //Unbuffered
}
Expand All @@ -63,15 +61,14 @@


if (!invalidateHttpCache()) {
Minz_Log::notice('FreshRSS write access problem in ' . join_path(USERS_PATH, $user, 'log.txt'),
$log_file);
Minz_Log::warning('FreshRSS write access problem in ' . join_path(USERS_PATH, $user, 'log.txt'), ADMIN_LOG);
if (defined('STDERR')) {
fwrite(STDERR, 'Write access problem in ' . join_path(USERS_PATH, $user, 'log.txt') . "\n");
}
}
}

Minz_Log::notice('FreshRSS actualize done.', $log_file);
Minz_Log::notice('FreshRSS actualize done.', ADMIN_LOG);
if (defined('STDOUT')) {
fwrite(STDOUT, 'Done.' . "\n");
$end_date = date_create('now');
Expand Down
6 changes: 6 additions & 0 deletions constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// PHP text output compression http://php.net/ob_gzhandler (better to do it at Web server level)
define('PHP_COMPRESSION', false);

// Maximum log file size in Bytes, before it will be divided by two
define('MAX_LOG_SIZE', 1048576);

// Constantes de chemins
define('FRESHRSS_PATH', dirname(__FILE__));

Expand All @@ -19,7 +22,10 @@
define('DATA_PATH', FRESHRSS_PATH . '/data');
define('UPDATE_FILENAME', DATA_PATH . '/update.php');
define('USERS_PATH', DATA_PATH . '/users');
define('ADMIN_LOG', USERS_PATH . '/_/log.txt');
define('API_LOG', USERS_PATH . '/_/log_api.txt');
define('CACHE_PATH', DATA_PATH . '/cache');
define('PSHB_LOG', USERS_PATH . '/_/log_pshb.txt');
define('PSHB_PATH', DATA_PATH . '/PubSubHubbub');

define('LIB_PATH', FRESHRSS_PATH . '/lib');
Expand Down
33 changes: 33 additions & 0 deletions lib/Minz/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Minz_Log {
* @param $information message d'erreur / information à enregistrer
* @param $level niveau d'erreur
* @param $file_name fichier de log
* @throws Minz_PermissionDeniedException
*/
public static function record ($information, $level, $file_name = null) {
try {
Expand Down Expand Up @@ -70,12 +71,44 @@ public static function record ($information, $level, $file_name = null) {
. ' [' . $level_label . ']'
. ' --- ' . $information . "\n";

self::ensureMaxLogSize($file_name);

if (file_put_contents($file_name, $log, FILE_APPEND | LOCK_EX) === false) {
throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
}
}
}

/**
* Make sure we do not waste a huge amount of disk space with old log messages.
*
* This method can be called multiple times for one script execution, but its result will not change unless
* you call clearstatcache() in between. We won't due do that for performance reasons.
*
* @param $file_name
* @throws Minz_PermissionDeniedException
*/
protected static function ensureMaxLogSize($file_name) {
$maxSize = defined('MAX_LOG_SIZE') ? MAX_LOG_SIZE : 1048576;
if ($maxSize > 0 && @filesize($file_name) > $maxSize) {
$fp = fopen($file_name, 'c+');
if ($fp && flock($fp, LOCK_EX)) {
fseek($fp, -intval($maxSize / 2), SEEK_END);
$content = fread($fp, $maxSize);
rewind($fp);
ftruncate($fp, 0);
fwrite($fp, $content ? $content : '');
fflush($fp);
flock($fp, LOCK_UN);
} else {
throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
}
if ($fp) {
fclose($fp);
}
}
}

/**
* Automatise le log des variables globales $_GET et $_POST
* Fait appel à la fonction record(...)
Expand Down
Loading

0 comments on commit f13926c

Please sign in to comment.