Support automatically removing old log files after a number of days

This commit is contained in:
HummyPkg 2019-01-17 12:13:06 +00:00
parent 06188094bf
commit da865f3e85
4 changed files with 24 additions and 3 deletions

View File

@ -30,6 +30,7 @@ set pkgdev [$settings pkgdev]
set rtschedule [$settings rtschedule] set rtschedule [$settings rtschedule]
set logsize [$settings logsize] set logsize [$settings logsize]
set logkeep [$settings logkeep] set logkeep [$settings logkeep]
set logage [$settings logage]
set cryptokey [system encryptionkey] set cryptokey [system encryptionkey]
if {![catch {set ck_fd [open "/mod/boot/cryptokey"]}]} { if {![catch {set ck_fd [open "/mod/boot/cryptokey"]}]} {
@ -45,4 +46,5 @@ handle_int_update pkgdev $pkgdev "Development Package Display"
handle_int_update rtschedule $rtschedule "Real-time scheduling" handle_int_update rtschedule $rtschedule "Real-time scheduling"
handle_int_update logsize $logsize "Log rotation size" handle_int_update logsize $logsize "Log rotation size"
handle_int_update logkeep $logkeep "Logs to keep" handle_int_update logkeep $logkeep "Logs to keep"
handle_int_update logage $logage "Rotated log max age"

View File

@ -41,6 +41,9 @@ puts "
setting_number logkeep "How many rotated logs to keep" $logkeep \ setting_number logkeep "How many rotated logs to keep" $logkeep \
1 100 1 100
setting_number logage "Delete old logs after X days" $logage \
0 365
if {[system modversion 1] >= 214} { if {[system modversion 1] >= 214} {
setting_toggle "Disable telnet menu?" "xtelnet" \ setting_toggle "Disable telnet menu?" "xtelnet" \
[file exists /mod/boot/xtelnet] 0 1 [file exists /mod/boot/xtelnet] 0 1

View File

@ -6,18 +6,19 @@ require settings.class
set s [settings] set s [settings]
set logsize [$s logsize] set logsize [$s logsize]
set logkeep [$s logkeep] set logkeep [$s logkeep]
set logage [$s logage]
puts "Rotating at $logsize, keeping $logkeep" puts "Rotating at $logsize, keeping $logkeep, max age $logage"
set logdir "/mod/tmp" set logdir "/mod/tmp"
set baselogs {} set baselogs {}
puts "\n-- Rotate\n" puts "\n-- Rotate\n"
set tstamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
foreach log [glob -nocomplain -tail -directory $logdir *.log] { foreach log [glob -nocomplain -tail -directory $logdir *.log] {
puts "\[$log\]" puts "\[$log\]"
set tstamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
# Check for old-style rotated log. # Check for old-style rotated log.
if {[string match {*_old.log} $log]} { if {[string match {*_old.log} $log]} {
@ -30,6 +31,15 @@ foreach log [glob -nocomplain -tail -directory $logdir *.log] {
# Check for already rotated log. # Check for already rotated log.
if {[regexp -- {\.[0-9]{14}\.log$} $log]} { if {[regexp -- {\.[0-9]{14}\.log$} $log]} {
puts " already rotated." puts " already rotated."
if {$logage > 0} {
set days $(
([clock seconds] - [file mtime $logdir/$log])
/ 86400)
if {$days > $logage} {
file tdelete "$logdir/$log"
puts " Deleting old log $log (age=$days)"
}
}
continue continue
} }
@ -61,7 +71,7 @@ foreach log $baselogs {
if {$num <= $logkeep} continue if {$num <= $logkeep} continue
lmap i [lrange [lsort -command logsort $old] 0 end-$logkeep] { lmap i [lrange [lsort -command logsort $old] 0 end-$logkeep] {
puts " Deleting old log $i" puts " Deleting old log $i"
file tdelete "$logdir/$i" file tdelete "$logdir/$i"
} }
} }

View File

@ -49,6 +49,7 @@ class settings {
audiomp3 0 audiomp3 0
logsize 1048576 logsize 1048576
logkeep 2 logkeep 2
logage 0
rtschedule 0 rtschedule 0
} }
@ -414,6 +415,11 @@ settings method logkeep {{num -1}} {
return $val return $val
} }
settings method logage {{num -1}} {
set val [$self _nval_setting logage $num]
return $val
}
settings method dedup_template {{pattern -1}} { settings method dedup_template {{pattern -1}} {
set val [$self _tval_setting dedup_template $pattern] set val [$self _tval_setting dedup_template $pattern]
if {$val eq "0"} { set val "s%se%E - " } if {$val eq "0"} { set val "s%se%E - " }