From d79cbb10a6c67b54da71f7684f0e052dcf44fdf5 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Mon, 22 Feb 2016 11:58:26 +0100 Subject: [PATCH] Add LogFacility for logging to a single file. Introduce the LogFacility "file", which just sends and logs all given log messages to a given file. Signed-off-by: Stefan Schantl --- modules/Logger.pm | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/modules/Logger.pm b/modules/Logger.pm index dbd3a81..2214e47 100644 --- a/modules/Logger.pm +++ b/modules/Logger.pm @@ -19,6 +19,7 @@ my %loglevels = ( # This hash contains the supported log facilities and their corresponding subroutines. my %logfacilities = ( "console" => \&LogFacilityConsole, + "file" => \&LogFacilityFile, "syslog" => \&LogFacilitySyslog, ); @@ -81,7 +82,7 @@ sub Log ($$) { my $use_facility = $self->{LogFacility}; # Transmit log message to the correct log facility. - $logfacilities{$use_facility}->($level, $message); + $logfacilities{$use_facility}->($self, $level, $message); } } @@ -103,6 +104,7 @@ sub GetLogLevels () { ## message to STDOUT. # sub LogFacilityConsole ($$) { + my $self = shift; my ($type, $message) = @_; # Print message on STDOUT. @@ -115,6 +117,7 @@ sub LogFacilityConsole ($$) { ## This log facility sends a given log message to the system log service (syslog). # sub LogFacilitySyslog ($$) { + my $self = shift; my ($type, $message) = @_; # The syslog function works best with an array based input, @@ -131,4 +134,23 @@ sub LogFacilitySyslog ($$) { closelog(); } +# +## LogFacilityFile function. +# +## This log facility will write any given log messages to a specified log file. +# +sub LogFacilityFile ($$) { + my $self = shift; + my ($type, $message) = @_; + + # 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"; + + # Close filehandle. + close(FILE); +} + 1; -- 2.39.2