]>
git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Logger.pm
6a70bc37fba82acdce837dc82bd9cc3ae31297b4
1 package Guardian
::Logger
;
5 use Exporter
qw(import);
7 our @EXPORT_OK = qw(New Log GetLogLevels);
9 use Sys
::Syslog
qw(:DEFAULT setlogsock);
11 # Hash which stores all supported log levels and their priority.
19 # This hash contains the supported log facilities and their corresponding subroutines.
21 "console" => \
&LogFacilityConsole
,
22 "syslog" => \
&LogFacilitySyslog
,
27 ## The "New" (Logger) function.
29 ## This function is responsible to initialize the Logger as a class based object.
30 ## It has to be called once before logging can be done.
32 ## The following arguments must be passed, when initializing a new Logger:
33 ## "LogLevel" and "LogFacility" with valid values from above.
36 my ( $class, %args ) = @_;
39 # Fail, if some critical arguments are missing.
40 unless ((exists($self->{LogLevel
})) && (exists($self->{LogFacility
}))) {
41 die "Could not initialize the Logger: Too less arguments are given.\n";
44 # Use bless to make "$self" to an object of class "$class".
47 # Return the class object.
52 ## The main "Log" function.
54 ## This function is used to handle the messages which are generated on various
55 ## points in the main programm or its modules. Those messages will contain usefull
56 ## information or deeper details about errors.
58 ## The Log function takes care about the configured loglevel and transmitts the
59 ## log messages to the configured log facility.
63 my ($level, $message) = @_;
65 # Check if we got an invalid loglevel.
66 unless(exists($loglevels{$level})) {
67 &Log
("err", "The logger has been called with an invalid loglevel ($level)!\n");
71 # Get value for the current used loglevel.
72 my $current_level = $loglevels{$self->{LogLevel
}};
74 # Get value for the required loglevel.
75 my $required_level = $loglevels{$level};
77 # Compare the current and required level to determine,
78 # if the message should be handled.
79 if ($current_level >= $required_level) {
80 # Get the facility, which should be used.
81 my $use_facility = $self->{LogFacility
};
83 # Transmit log message to the correct log facility.
84 $logfacilities{$use_facility}->($level, $message);
89 ## GetLogLevels function.
91 ## This really simple function just returns the hash which
92 ## contains all supported log levels.
95 # Nothing to do, just return the loglevels hash.
100 ## LogFacilityConsole function.
102 ## This is a very simple log facility which just prints the given log
103 ## message to STDOUT.
105 sub LogFacilityConsole
($$) {
106 my ($type, $message) = @_;
108 # Print message on STDOUT.
109 print STDOUT
"\[$type\] $message\n";
113 ## LogFacilitySyslog function.
115 ## This log facility sends a given log message to the system log service (syslog).
117 sub LogFacilitySyslog
($$) {
118 my ($type, $message) = @_;
120 # The syslog function works best with an array based input,
121 # so generate one before passing the message details to syslog.
122 my @syslog = ("$type", "<$type> $message");
124 # Establish the connection to the syslog service.
125 openlog
('guardian', 'cons,pid', 'user');
127 # Send the log message.
130 # Close the log handle.