From: Stefan Schantl Date: Tue, 19 Jul 2016 08:37:39 +0000 (+0200) Subject: Add timestamps when logging messages. X-Git-Tag: 2.0~5 X-Git-Url: http://git.ipfire.org/?p=people%2Fstevee%2Fguardian.git;a=commitdiff_plain;h=c2c5b739a22c9c1119e31d06c5acf5babd31c400 Add timestamps when logging messages. When using the logfacilities File or Console, timestamps now will be added to the log messages. Signed-off-by: Stefan Schantl --- diff --git a/modules/Logger.pm b/modules/Logger.pm index 2214e47..13f4ae3 100644 --- a/modules/Logger.pm +++ b/modules/Logger.pm @@ -7,6 +7,7 @@ use Exporter qw(import); our @EXPORT_OK = qw(New Log GetLogLevels); use Sys::Syslog qw(:DEFAULT setlogsock); +use POSIX qw(strftime); # Hash which stores all supported log levels and their priority. my %loglevels = ( @@ -107,8 +108,11 @@ sub LogFacilityConsole ($$) { my $self = shift; my ($type, $message) = @_; + # Get current date and time and format like: "Jan 01 00:00:01" + my $now = strftime "%b %e %H:%M:%S", localtime(); + # Print message on STDOUT. - print STDOUT "\[$type\] $message\n"; + print STDOUT "$now \[$type\] $message\n"; } # @@ -143,11 +147,14 @@ sub LogFacilityFile ($$) { my $self = shift; my ($type, $message) = @_; + # Get current date and time and format like: "Jan 01 00:00:01" + my $now = strftime "%b %e %H:%M:%S", localtime(); + # Open the logfile for writing. open(LOGFILE, '>>', $self->{LogFile}) or die "Could not write to $self->{LogFile}: $!\n"; # Write log message to file. - print LOGFILE "\[$type\] $message\n"; + print LOGFILE "$now \[$type\] $message\n"; # Close filehandle. close(FILE);