]>
git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Logger.pm
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 ## The Update (Logger) settings function
91 ## This function is used to update the object settings of the
92 ## initialized Logger class.
95 my ($self, %args) = @_;
97 # Map the new hash keys and values to the existing one inside
99 map { $self->{ $_ } = $args{ $_ } } keys %args;
103 ## GetLogLevels function.
105 ## This really simple function just returns the hash which
106 ## contains all supported log levels.
109 # Nothing to do, just return the loglevels hash.
114 ## LogFacilityConsole function.
116 ## This is a very simple log facility which just prints the given log
117 ## message to STDOUT.
119 sub LogFacilityConsole
($$) {
120 my ($type, $message) = @_;
122 # Print message on STDOUT.
123 print STDOUT
"\[$type\] $message\n";
127 ## LogFacilitySyslog function.
129 ## This log facility sends a given log message to the system log service (syslog).
131 sub LogFacilitySyslog
($$) {
132 my ($type, $message) = @_;
134 # The syslog function works best with an array based input,
135 # so generate one before passing the message details to syslog.
136 my @syslog = ("$type", "<$type> $message");
138 # Establish the connection to the syslog service.
139 openlog
('guardian', 'cons,pid', 'user');
141 # Send the log message.
144 # Close the log handle.