Skip to content

Commit 3a4f23b

Browse files
committed
Log rotation, use Minz_Log, new log constants
ADMIN_LOG, API_LOG, PSHB_LOG
1 parent 452886e commit 3a4f23b

File tree

8 files changed

+66
-87
lines changed

8 files changed

+66
-87
lines changed

app/Controllers/feedController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public static function actualizeFeed($feed_id, $feed_url, $force, $simplePiePush
263263
if ((!$simplePiePush) && (!$feed_id) && $pubSubHubbubEnabled && ($feed->lastUpdate() > $pshbMinAge)) {
264264
//$text = 'Skip pull of feed using PubSubHubbub: ' . $url;
265265
//Minz_Log::debug($text);
266-
//file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
266+
//Minz_Log::debug($text, PSHB_LOG);
267267
continue; //When PubSubHubbub is used, do not pull refresh so often
268268
}
269269

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

372372
if ($pubSubHubbubEnabled && !$simplePiePush) { //We use push, but have discovered an article by pull!
373373
$text = 'An article was discovered by pull although we use PubSubHubbub!: Feed ' . $url . ' GUID ' . $entry->guid();
374-
file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
374+
Minz_Log::warning($text, PSHB_LOG);
375375
Minz_Log::warning($text);
376376
$pubSubHubbubEnabled = false;
377377
$feed->pubSubHubbubError(true);

app/Models/Feed.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,7 @@ function pubSubHubbubError($error = true) {
403403
if (!isset($hubJson['error']) || $hubJson['error'] !== (bool)$error) {
404404
$hubJson['error'] = (bool)$error;
405405
file_put_contents($hubFilename, json_encode($hubJson));
406-
file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t"
407-
. 'Set error to ' . ($error ? 1 : 0) . ' for ' . $url . "\n", FILE_APPEND);
406+
Minz_Log::warning('Set error to ' . ($error ? 1 : 0) . ' for ' . $url, PSHB_LOG);
408407
}
409408
return false;
410409
}
@@ -419,15 +418,15 @@ function pubSubHubbubPrepare() {
419418
if (!$hubJson || empty($hubJson['key']) || !ctype_xdigit($hubJson['key'])) {
420419
$text = 'Invalid JSON for PubSubHubbub: ' . $this->url;
421420
Minz_Log::warning($text);
422-
file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
421+
Minz_Log::warning($text, PSHB_LOG);
423422
return false;
424423
}
425424
if ((!empty($hubJson['lease_end'])) && ($hubJson['lease_end'] < (time() + (3600 * 23)))) { //TODO: Make a better policy
426425
$text = 'PubSubHubbub lease ends at '
427426
. date('c', empty($hubJson['lease_end']) ? time() : $hubJson['lease_end'])
428427
. ' and needs renewal: ' . $this->url;
429428
Minz_Log::warning($text);
430-
file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
429+
Minz_Log::warning($text, PSHB_LOG);
431430
$key = $hubJson['key']; //To renew our lease
432431
} elseif (((!empty($hubJson['error'])) || empty($hubJson['lease_end'])) &&
433432
(empty($hubJson['lease_start']) || $hubJson['lease_start'] < time() - (3600 * 23))) { //Do not renew too often
@@ -445,7 +444,7 @@ function pubSubHubbubPrepare() {
445444
file_put_contents(PSHB_PATH . '/keys/' . $key . '.txt', base64url_encode($this->selfUrl));
446445
$text = 'PubSubHubbub prepared for ' . $this->url;
447446
Minz_Log::debug($text);
448-
file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
447+
Minz_Log::debug($text, PSHB_LOG);
449448
}
450449
$currentUser = Minz_Session::param('currentUser');
451450
if (FreshRSS_user_Controller::checkUsername($currentUser) && !file_exists($path . '/' . $currentUser . '.txt')) {
@@ -499,9 +498,8 @@ function pubSubHubbubSubscribe($state) {
499498
$response = curl_exec($ch);
500499
$info = curl_getinfo($ch);
501500

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

506504
if (substr($info['http_code'], 0, 1) == '2') {
507505
return true;

app/Models/LogDAO.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public static function lines() {
2222
public static function truncate() {
2323
file_put_contents(join_path(DATA_PATH, 'users', Minz_Session::param('currentUser', '_'), 'log.txt'), '');
2424
if (FreshRSS_Auth::hasAccess('admin')) {
25-
file_put_contents(join_path(DATA_PATH, 'users', '_', 'log.txt'), '');
26-
file_put_contents(join_path(DATA_PATH, 'users', '_', 'log_api.txt'), '');
27-
file_put_contents(join_path(DATA_PATH, 'users', '_', 'log_pshb.txt'), '');
25+
file_put_contents(ADMIN_LOG, '');
26+
file_put_contents(API_LOG, '');
27+
file_put_contents(PSHB_LOG, '');
2828
}
2929
}
3030
}

app/actualize_script.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
$_GET['force'] = true;
2121
$_SERVER['HTTP_HOST'] = '';
2222

23-
24-
$log_file = join_path(USERS_PATH, '_', 'log.txt');
25-
26-
2723
$app = new FreshRSS();
2824

2925
$system_conf = Minz_Configuration::get('system');
@@ -45,13 +41,13 @@
4541
foreach ($users as $user) {
4642
if (($user !== $system_conf->default_user) &&
4743
(FreshRSS_UserDAO::mtime($user) < $min_last_activity)) {
48-
Minz_Log::notice('FreshRSS skip inactive user ' . $user, $log_file);
44+
Minz_Log::notice('FreshRSS skip inactive user ' . $user, ADMIN_LOG);
4945
if (defined('STDOUT')) {
5046
fwrite(STDOUT, 'FreshRSS skip inactive user ' . $user . "\n"); //Unbuffered
5147
}
5248
continue;
5349
}
54-
Minz_Log::notice('FreshRSS actualize ' . $user, $log_file);
50+
Minz_Log::notice('FreshRSS actualize ' . $user, ADMIN_LOG);
5551
if (defined('STDOUT')) {
5652
fwrite(STDOUT, 'Actualize ' . $user . "...\n"); //Unbuffered
5753
}
@@ -66,16 +62,15 @@
6662

6763

6864
if (!invalidateHttpCache()) {
69-
Minz_Log::notice('FreshRSS write access problem in ' . join_path(USERS_PATH, $user, 'log.txt'),
70-
$log_file);
65+
Minz_Log::warning('FreshRSS write access problem in ' . join_path(USERS_PATH, $user, 'log.txt'), ADMIN_LOG);
7166
if (defined('STDERR')) {
7267
fwrite(STDERR, 'Write access problem in ' . join_path(USERS_PATH, $user, 'log.txt') . "\n");
7368
}
7469
}
7570
}
7671

7772

78-
Minz_Log::notice('FreshRSS actualize done.', $log_file);
73+
Minz_Log::notice('FreshRSS actualize done.', ADMIN_LOG);
7974
if (defined('STDOUT')) {
8075
fwrite(STDOUT, 'Done.' . "\n");
8176
$end_date = date_create('now');

constants.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// PHP text output compression http://php.net/ob_gzhandler (better to do it at Web server level)
99
define('PHP_COMPRESSION', false);
1010

11-
// maximum log file size, before it will be purged (defaults to 512000 = 500kB)
12-
define('MAX_LOG_SIZE', 512000);
11+
// Maximum log file size in Bytes, before it will be divided by two
12+
define('MAX_LOG_SIZE', 1048576);
1313

1414
// Constantes de chemins
1515
define('FRESHRSS_PATH', dirname(__FILE__));
@@ -22,7 +22,10 @@
2222
define('DATA_PATH', FRESHRSS_PATH . '/data');
2323
define('UPDATE_FILENAME', DATA_PATH . '/update.php');
2424
define('USERS_PATH', DATA_PATH . '/users');
25+
define('ADMIN_LOG', USERS_PATH, '/_/log.txt');
26+
define('API_LOG', USERS_PATH, '/_/log_api.txt');
2527
define('CACHE_PATH', DATA_PATH . '/cache');
28+
define('PSHB_LOG', USERS_PATH . '/_/log_pshb.txt';
2629
define('PSHB_PATH', DATA_PATH . '/PubSubHubbub');
2730

2831
define('LIB_PATH', FRESHRSS_PATH . '/lib');

lib/Minz/Log.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function record ($information, $level, $file_name = null) {
7171
. ' [' . $level_label . ']'
7272
. ' --- ' . $information . "\n";
7373

74-
self::checkLogfileSize($file_name);
74+
self::ensureMaxLogSize($file_name);
7575

7676
if (file_put_contents($file_name, $log, FILE_APPEND | LOCK_EX) === false) {
7777
throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
@@ -88,12 +88,24 @@ public static function record ($information, $level, $file_name = null) {
8888
* @param $file_name
8989
* @throws Minz_PermissionDeniedException
9090
*/
91-
protected static function checkLogfileSize($file_name) {
92-
$maxSize = defined('MAX_LOG_SIZE') ? MAX_LOG_SIZE : 512000;
93-
if (@filesize($file_name) > $maxSize) {
94-
if (file_put_contents($file_name, '') === false) {
91+
protected static function ensureMaxLogSize($file_name) {
92+
$maxSize = defined('MAX_LOG_SIZE') ? MAX_LOG_SIZE : 1048576;
93+
if ($maxSize > 0 && @filesize($file_name) > $maxSize) {
94+
$fp = fopen($file_name, 'c+');
95+
if ($fp && flock($fp, LOCK_EX)) {
96+
fseek($fp, -intval($maxSize / 2), SEEK_END);
97+
$content = fread($fp, $maxSize);
98+
rewind($fp);
99+
ftruncate($fp, 0);
100+
fwrite($fp, $content ? $content : '');
101+
fflush($fp);
102+
flock($fp, LOCK_UN);
103+
} else {
95104
throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
96105
}
106+
if ($fp) {
107+
fclose($fp);
108+
}
97109
}
98110
}
99111

0 commit comments

Comments
 (0)