]>
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 "file" => \
&LogFacilityFile
,
23 "syslog" => \
&LogFacilitySyslog
,
28 ## The "Init" (Logger) function.
30 ## This function is responsible to initialize the Logger as a class based object.
31 ## It has to be called once before logging can be done.
33 ## The following arguments must be passed, when initializing a new Logger:
34 ## "LogLevel" and "LogFacility" with valid values from above.
37 my ( $class, %args ) = @_;
40 # Fail, if some critical arguments are missing.
41 unless ((exists($self->{LogLevel
})) && (exists($self->{LogFacility
}))) {
42 die "Could not initialize the Logger: Too less arguments are given.\n";
45 # Use bless to make "$self" to an object of class "$class".
48 # Return the class object.
53 ## The main "Log" function.
55 ## This function is used to handle the messages which are generated on various
56 ## points in the main programm or its modules. Those messages will contain usefull
57 ## information or deeper details about errors.
59 ## The Log function takes care about the configured loglevel and transmitts the
60 ## log messages to the configured log facility.
64 my ($level, $message) = @_;
66 # Check if we got an invalid loglevel.
67 unless(exists($loglevels{$level})) {
68 &Log
("err", "The logger has been called with an invalid loglevel ($level)!\n");
72 # Get value for the current used loglevel.
73 my $current_level = $loglevels{$self->{LogLevel
}};
75 # Get value for the required loglevel.
76 my $required_level = $loglevels{$level};
78 # Compare the current and required level to determine,
79 # if the message should be handled.
80 if ($current_level >= $required_level) {
81 # Get the facility, which should be used.
82 my $use_facility = $self->{LogFacility
};
84 # Transmit log message to the correct log facility.
85 $logfacilities{$use_facility}->($self, $level, $message);
90 ## GetLogLevels function.
92 ## This really simple function just returns the hash which
93 ## contains all supported log levels.
96 # Nothing to do, just return the loglevels hash.
101 ## LogFacilityConsole function.
103 ## This is a very simple log facility which just prints the given log
104 ## message to STDOUT.
106 sub LogFacilityConsole
($$) {
108 my ($type, $message) = @_;
110 # Print message on STDOUT.
111 print STDOUT
"\[$type\] $message\n";
115 ## LogFacilitySyslog function.
117 ## This log facility sends a given log message to the system log service (syslog).
119 sub LogFacilitySyslog
($$) {
121 my ($type, $message) = @_;
123 # The syslog function works best with an array based input,
124 # so generate one before passing the message details to syslog.
125 my @syslog = ("$type", "<$type> $message");
127 # Establish the connection to the syslog service.
128 openlog
('guardian', 'cons,pid', 'user');
130 # Send the log message.
133 # Close the log handle.
138 ## LogFacilityFile function.
140 ## This log facility will write any given log messages to a specified log file.
142 sub LogFacilityFile
($$) {
144 my ($type, $message) = @_;
146 # Open the logfile for writing.
147 open(LOGFILE
, '>>', $self->{LogFile
}) or die "Could not write to $self->{LogFile}: $!\n";
149 # Write log message to file.
150 print LOGFILE
"\[$type\] $message\n";