]> git.ipfire.org Git - people/stevee/guardian.git/commitdiff
Add LogFacility for logging to a single file.
authorStefan Schantl <stefan.schantl@ipfire.org>
Mon, 22 Feb 2016 10:58:26 +0000 (11:58 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Mon, 22 Feb 2016 10:58:26 +0000 (11:58 +0100)
Introduce the LogFacility "file", which just sends and logs
all given log messages to a given file.

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

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