]> git.ipfire.org Git - people/stevee/guardian.git/commitdiff
Add timestamps when logging messages.
authorStefan Schantl <stefan.schantl@ipfire.org>
Tue, 19 Jul 2016 08:37:39 +0000 (10:37 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Tue, 19 Jul 2016 08:37:39 +0000 (10:37 +0200)
When using the logfacilities File or Console,
timestamps now will be added to the log messages.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
modules/Logger.pm

index 2214e477e10d2cdd0c29b40337537d081debf000..13f4ae313a8dbe3431eab8bf389063ce60054f20 100644 (file)
@@ -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);