#!/usr/bin/env perl
# compresses and archives yyyy-mm-dd.sql (Log::Dispatch::File)
# rotates debug.log, sql.txt & trace.log
# skips empty files using io($file)->size
# run from cron once per day eg 00:00
use strict;
use warnings;
use feature 'say';
use lib '/home/raj/perl5/lib/perl5';
use IO::All;
use Logfile::Rotate;
use IO::Compress::Gzip qw(gzip $GzipError);
use FindBin qw($Bin); # warn $Bin; exit;
use lib $Bin . '/../../../lib';
use LIMS::Local::ScriptHelpers;
my $path_to_logs = $Bin . '/../../../logs';
my $archive = $path_to_logs . '/old';
my $tools = LIMS::Local::ScriptHelpers->new();
# get yesterday's date (allows for BST in DT construct):
my $yesterday = $tools->date_subtract(days => 1); # say $yesterday;
# time format for gz file:
my $archive_time_format = '%Y_%m%d';
# replaced by Log::Dispatch::File format 04/2019:
{ # Log::Dispatch::File::Rolling log-file format (yyyy_mmdd_sql.log) -----------
my $src = sprintf '%s/%s_sql.log',
$path_to_logs, $yesterday->strftime('%Y_%m%d'); # say $src;
if ( -e $src ) { # if file exists:
my $o = io($src);
# compress and archive if it has data:
if ($o->size) {
my $output = sprintf '%s/%s_sql.log.gz',
$archive, $yesterday->strftime($archive_time_format);
my $input = $o->name; # warn $input;
my $status = gzip $input => $output, Minimal => 1 # avoids full path info
or die "$0 - gzip failed: $GzipError\n";
# `gzip -c $src > $output`; # system command alternative
}
$o->unlink; # archived if has data, deleted anyway
}
}
{ # Log::Dispatch::File log-file format (yyyy_mm_dd.sql) -----------------------
my $src = sprintf '%s/%s.sql',
$path_to_logs, $yesterday->strftime('%Y-%m-%d'); # say $src;
if ( -e $src ) { # if file exists:
my $o = io($src);
# compress and archive if it has data:
if ($o->size) {
my $output = sprintf '%s/%s_sql.log.gz',
$archive, $yesterday->strftime($archive_time_format);
my $input = $o->name; # warn $input;
my $status = gzip $input => $output, Minimal => 1 # avoids full path info
or die "$0 - gzip failed: $GzipError\n";
# `gzip -c $src > $output`; # system command alternative
}
$o->unlink; # archived if has data, deleted anyway
}
}
=begin sql.log (replaced by yyyy_mmdd_sql.log)
my $sql_log = "$path_to_logs/sql.log";
if ( io($sql_log)->size ) {
Logfile::Rotate->new(
File => $sql_log, # base filename
Dir => $archive, # move old files to here
Count => 365, # how many to keep before overwriting
Gzip => 'lib', # use Compress::Zlib for compression (recommended)
Flock => 'yes', # flock if supported
Persist => 'yes', # copy current logfile chown settings to any new log files
)->rotate;
}
=cut
# debug.log --------------------------------------------------------------------
my $debug_file = "$path_to_logs/debug.log"; #
if ( io($debug_file)->size ) {
Logfile::Rotate->new(
File => $debug_file, # base filename
Count => 3, # how many to keep before overwriting
Flock => 'yes', # flock if supported
Persist => 'yes', # copy current logfile chown settings to any new log files
)->rotate;
}
# sql.txt ----------------------------------------------------------------------
my $sql_file = "$path_to_logs/sql.txt";
if ( io($sql_file)->size ) {
Logfile::Rotate->new(
File => $sql_file, # base filename
Count => 3, # how many to keep before overwriting
Flock => 'yes', # flock if supported
Persist => 'yes', # copy current logfile chown settings to any new log files
)->rotate;
}
# dev_sql.txt ----------------------------------------------------------------------
my $dev_sql_file = "$path_to_logs/dev_sql.txt";
if ( io($dev_sql_file)->size ) {
Logfile::Rotate->new(
File => $dev_sql_file, # base filename
Count => 3, # how many to keep before overwriting
Flock => 'yes', # flock if supported
Persist => 'yes', # copy current logfile chown settings to any new log files
)->rotate;
}
# dev_sql.txt ----------------------------------------------------------------------
my $dev_sql_log = "$path_to_logs/dev_sql.log";
if ( io($dev_sql_log)->size ) {
Logfile::Rotate->new(
File => $dev_sql_log, # base filename
Count => 3, # how many to keep before overwriting
Flock => 'yes', # flock if supported
Persist => 'yes', # copy current logfile chown settings to any new log files
)->rotate;
}
# trace.log --------------------------------------------------------------------
my $dbi_trace = "$path_to_logs/trace.log";
if ( io($dbi_trace)->size ) {
Logfile::Rotate->new(
File => $dbi_trace, # base filename
Dir => $archive, # move old files to here
Count => 365, # how many to keep before overwriting
Flock => 'yes', # flock if supported
Persist => 'yes', # copy current logfile chown settings to any new log files
)->rotate;
}