Skip to content

Commit

Permalink
System: Settings: Logging - maximum log file size ignored when there'…
Browse files Browse the repository at this point in the history
…s only one file in the directory. closes #7397

The rotate function needed the next file to calculate the suffix, which is problematic if someone just cleaned all logs. This patch improves the match a bit (last _ instead of assuming _ doesn't exist in the name) and uses "1" for the first rotate action.
  • Loading branch information
AdSchellevis committed Apr 20, 2024
1 parent 8167625 commit 73c3b88
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/opnsense/scripts/syslog/log_archive
Expand Up @@ -66,14 +66,19 @@ foreach ($relevant_logs as $log_subject => $items) {
if (isset($items[1]) && preg_match('/.*_(\d){8}\.log$/', $items[0])) {
/* Extract rotate sequence number from filename in format XXX_YYYYMMDD.log or XXX_YYYYMMDD.SEQ.log */
$fparts = explode('_', $items[1]);
$parts = explode('.', $fparts[1]);
$parts = explode('.', $fparts[count($fparts)-1]);
$seq = (count($parts) == 3 && ctype_digit($parts[1]) ? (int)$parts[1] : 0) + 1;
/* Suffix sequence number to last filename, rename and schedule a rotate (syslog-ng reload) */
$new_filename = sprintf('%s.%04d.log', explode('.', $items[0])[0], $seq);
array_unshift($items, array_shift($items), $new_filename);
rename($items[0], $new_filename);
$rotate = true;
} elseif (preg_match('/.*_(\d){8}\.log$/', $items[0])) {
$seq = 1;
} else {
// already rotated
continue;
}
/* Suffix sequence number to last filename, rename and schedule a rotate (syslog-ng reload) */
$new_filename = sprintf('%s.%04d.log', explode('.', $items[0])[0], $seq);
array_unshift($items, array_shift($items), $new_filename);
rename($items[0], $new_filename);
$rotate = true;
}
if (count($items) > $preserve_logs) {
foreach (array_slice($items, $preserve_logs) as $filename) {
Expand Down

0 comments on commit 73c3b88

Please sign in to comment.