# This hash contains the supported log facilities and their corresponding subroutines.
my %logfacilities = (
"console" => \&LogFacilityConsole,
+ "file" => \&LogFacilityFile,
"syslog" => \&LogFacilitySyslog,
);
#
-## The "New" (Logger) function.
+## The "Init" (Logger) function.
#
## This function is responsible to initialize the Logger as a class based object.
## It has to be called once before logging can be done.
## The following arguments must be passed, when initializing a new Logger:
## "LogLevel" and "LogFacility" with valid values from above.
#
-sub New (%) {
+sub Init (%) {
my ( $class, %args ) = @_;
my $self = \%args;
my $use_facility = $self->{LogFacility};
# Transmit log message to the correct log facility.
- $logfacilities{$use_facility}->($level, $message);
+ $logfacilities{$use_facility}->($self, $level, $message);
}
}
## This really simple function just returns the hash which
## contains all supported log levels.
#
-sub LogLevels () {
+sub GetLogLevels () {
# Nothing to do, just return the loglevels hash.
return %loglevels;
}
## message to STDOUT.
#
sub LogFacilityConsole ($$) {
+ my $self = shift;
my ($type, $message) = @_;
# Print message on STDOUT.
## 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,
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;