]>
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);
10 use POSIX
qw(strftime);
12 # Hash which stores all supported log levels and their priority.
20 # This hash contains the supported log facilities and their corresponding subroutines.
22 "console" => \
&LogFacilityConsole
,
23 "file" => \
&LogFacilityFile
,
24 "syslog" => \
&LogFacilitySyslog
,
29 ## The "Init" (Logger) function.
31 ## This function is responsible to initialize the Logger as a class based object.
32 ## It has to be called once before logging can be done.
34 ## The following arguments must be passed, when initializing a new Logger:
35 ## "LogLevel" and "LogFacility" with valid values from above.
38 my ( $class, %args ) = @_;
41 # Fail, if some critical arguments are missing.
42 unless ((exists($self->{LogLevel
})) && (exists($self->{LogFacility
}))) {
43 die "Could not initialize the Logger: Too less arguments are given.\n";
46 # Use bless to make "$self" to an object of class "$class".
49 # Return the class object.
54 ## The main "Log" function.
56 ## This function is used to handle the messages which are generated on various
57 ## points in the main programm or its modules. Those messages will contain usefull
58 ## information or deeper details about errors.
60 ## The Log function takes care about the configured loglevel and transmitts the
61 ## log messages to the configured log facility.
65 my ($level, $message) = @_;
67 # Check if we got an invalid loglevel.
68 unless(exists($loglevels{$level})) {
69 &Log
("err", "The logger has been called with an invalid loglevel ($level)!\n");
73 # Get value for the current used loglevel.
74 my $current_level = $loglevels{$self->{LogLevel
}};
76 # Get value for the required loglevel.
77 my $required_level = $loglevels{$level};
79 # Compare the current and required level to determine,
80 # if the message should be handled.
81 if ($current_level >= $required_level) {
82 # Get the facility, which should be used.
83 my $use_facility = $self->{LogFacility
};
85 # Transmit log message to the correct log facility.
86 $logfacilities{$use_facility}->($self, $level, $message);
91 ## GetLogLevels function.
93 ## This really simple function just returns the hash which
94 ## contains all supported log levels.
97 # Nothing to do, just return the loglevels hash.
102 ## LogFacilityConsole function.
104 ## This is a very simple log facility which just prints the given log
105 ## message to STDOUT.
107 sub LogFacilityConsole
($$) {
109 my ($type, $message) = @_;
111 # Get current date and time and format like: "Jan 01 00:00:01"
112 my $now = strftime
"%b %e %H:%M:%S", localtime();
114 # Print message on STDOUT.
115 print STDOUT
"$now \[$type\] $message\n";
119 ## LogFacilitySyslog function.
121 ## This log facility sends a given log message to the system log service (syslog).
123 sub LogFacilitySyslog
($$) {
125 my ($type, $message) = @_;
127 # The syslog function works best with an array based input,
128 # so generate one before passing the message details to syslog.
129 my @syslog = ("$type", "<$type> $message");
131 # Establish the connection to the syslog service.
132 openlog
('guardian', 'cons,pid', 'user');
134 # Send the log message.
137 # Close the log handle.
142 ## LogFacilityFile function.
144 ## This log facility will write any given log messages to a specified log file.
146 sub LogFacilityFile
($$) {
148 my ($type, $message) = @_;
150 # Get current date and time and format like: "Jan 01 00:00:01"
151 my $now = strftime
"%b %e %H:%M:%S", localtime();
153 # Open the logfile for writing.
154 open(LOGFILE
, '>>', $self->{LogFile
}) or die "Could not write to $self->{LogFile}: $!\n";
156 # Write log message to file.
157 print LOGFILE
"$now \[$type\] $message\n";