]>
Commit | Line | Data |
---|---|---|
a17e021e SS |
1 | package Guardian::Logger; |
2 | use strict; | |
3 | use warnings; | |
4 | ||
5 | use Exporter qw(import); | |
6 | ||
7 | our @EXPORT_OK = qw(New Log GetLogLevels); | |
8 | ||
9 | use Sys::Syslog qw(:DEFAULT setlogsock); | |
c2c5b739 | 10 | use POSIX qw(strftime); |
a17e021e SS |
11 | |
12 | # Hash which stores all supported log levels and their priority. | |
13 | my %loglevels = ( | |
14 | 'off' => '0', | |
15 | 'err' => '1', | |
16 | 'info' => '1', | |
17 | 'debug' => '2', | |
18 | ); | |
19 | ||
20 | # This hash contains the supported log facilities and their corresponding subroutines. | |
21 | my %logfacilities = ( | |
22 | "console" => \&LogFacilityConsole, | |
d79cbb10 | 23 | "file" => \&LogFacilityFile, |
a17e021e SS |
24 | "syslog" => \&LogFacilitySyslog, |
25 | ); | |
26 | ||
27 | ||
28 | # | |
aab61123 | 29 | ## The "Init" (Logger) function. |
a17e021e SS |
30 | # |
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. | |
33 | # | |
34 | ## The following arguments must be passed, when initializing a new Logger: | |
35 | ## "LogLevel" and "LogFacility" with valid values from above. | |
36 | # | |
aab61123 | 37 | sub Init (%) { |
a17e021e SS |
38 | my ( $class, %args ) = @_; |
39 | my $self = \%args; | |
40 | ||
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"; | |
44 | } | |
45 | ||
46 | # Use bless to make "$self" to an object of class "$class". | |
47 | bless($self, $class); | |
48 | ||
49 | # Return the class object. | |
50 | return $self; | |
51 | } | |
52 | ||
53 | # | |
54 | ## The main "Log" function. | |
55 | # | |
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. | |
59 | # | |
60 | ## The Log function takes care about the configured loglevel and transmitts the | |
61 | ## log messages to the configured log facility. | |
62 | # | |
63 | sub Log ($$) { | |
64 | my $self = shift; | |
65 | my ($level, $message) = @_; | |
66 | ||
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"); | |
70 | return; | |
71 | } | |
72 | ||
73 | # Get value for the current used loglevel. | |
74 | my $current_level = $loglevels{$self->{LogLevel}}; | |
75 | ||
76 | # Get value for the required loglevel. | |
77 | my $required_level = $loglevels{$level}; | |
78 | ||
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}; | |
84 | ||
85 | # Transmit log message to the correct log facility. | |
d79cbb10 | 86 | $logfacilities{$use_facility}->($self, $level, $message); |
a17e021e SS |
87 | } |
88 | } | |
89 | ||
a17e021e SS |
90 | # |
91 | ## GetLogLevels function. | |
92 | # | |
93 | ## This really simple function just returns the hash which | |
94 | ## contains all supported log levels. | |
95 | # | |
43ab646a | 96 | sub GetLogLevels () { |
a17e021e SS |
97 | # Nothing to do, just return the loglevels hash. |
98 | return %loglevels; | |
99 | } | |
100 | ||
101 | # | |
102 | ## LogFacilityConsole function. | |
103 | # | |
104 | ## This is a very simple log facility which just prints the given log | |
105 | ## message to STDOUT. | |
106 | # | |
107 | sub LogFacilityConsole ($$) { | |
d79cbb10 | 108 | my $self = shift; |
a17e021e SS |
109 | my ($type, $message) = @_; |
110 | ||
c2c5b739 SS |
111 | # Get current date and time and format like: "Jan 01 00:00:01" |
112 | my $now = strftime "%b %e %H:%M:%S", localtime(); | |
113 | ||
a17e021e | 114 | # Print message on STDOUT. |
c2c5b739 | 115 | print STDOUT "$now \[$type\] $message\n"; |
a17e021e SS |
116 | } |
117 | ||
118 | # | |
119 | ## LogFacilitySyslog function. | |
120 | # | |
121 | ## This log facility sends a given log message to the system log service (syslog). | |
122 | # | |
123 | sub LogFacilitySyslog ($$) { | |
d79cbb10 | 124 | my $self = shift; |
a17e021e SS |
125 | my ($type, $message) = @_; |
126 | ||
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"); | |
130 | ||
131 | # Establish the connection to the syslog service. | |
132 | openlog('guardian', 'cons,pid', 'user'); | |
133 | ||
134 | # Send the log message. | |
135 | syslog(@syslog); | |
136 | ||
137 | # Close the log handle. | |
138 | closelog(); | |
139 | } | |
140 | ||
d79cbb10 SS |
141 | # |
142 | ## LogFacilityFile function. | |
143 | # | |
144 | ## This log facility will write any given log messages to a specified log file. | |
145 | # | |
146 | sub LogFacilityFile ($$) { | |
147 | my $self = shift; | |
148 | my ($type, $message) = @_; | |
149 | ||
c2c5b739 SS |
150 | # Get current date and time and format like: "Jan 01 00:00:01" |
151 | my $now = strftime "%b %e %H:%M:%S", localtime(); | |
152 | ||
d79cbb10 SS |
153 | # Open the logfile for writing. |
154 | open(LOGFILE, '>>', $self->{LogFile}) or die "Could not write to $self->{LogFile}: $!\n"; | |
155 | ||
156 | # Write log message to file. | |
c2c5b739 | 157 | print LOGFILE "$now \[$type\] $message\n"; |
d79cbb10 SS |
158 | |
159 | # Close filehandle. | |
160 | close(FILE); | |
161 | } | |
162 | ||
a17e021e | 163 | 1; |