1 | package LATMOS::Accounts::Log; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use Sys::Syslog qw(:standard :macros); |
---|
6 | use Exporter (); |
---|
7 | use Mail::Sendmail; |
---|
8 | |
---|
9 | =head1 NAME |
---|
10 | |
---|
11 | LATMOS::Accounts::Log - Log dispatcher for LATMOS::Accounts |
---|
12 | |
---|
13 | =head1 SYNOPSYS |
---|
14 | |
---|
15 | use LATMOS::Accounts::Log |
---|
16 | la_log(LA_ERR, "An error has occur"); |
---|
17 | |
---|
18 | =head1 DESCRIPTION |
---|
19 | |
---|
20 | This module provide facilities to log to both console, syslog or using callback. |
---|
21 | |
---|
22 | When the environement variable C<LA_DEBUG> is set, all message are print to |
---|
23 | C<stderr>. |
---|
24 | |
---|
25 | =cut |
---|
26 | |
---|
27 | our @loglevels = qw( |
---|
28 | LA_EMERG |
---|
29 | LA_ALERT |
---|
30 | LA_CRIT |
---|
31 | LA_ERR |
---|
32 | LA_ERROR |
---|
33 | LA_WARN |
---|
34 | LA_WARNING |
---|
35 | LA_NOTICE |
---|
36 | LA_INFO |
---|
37 | LA_DEBUG |
---|
38 | ); |
---|
39 | |
---|
40 | use vars qw(@EXPORT_OK @EXPORT %EXPORT_TAGS @ISA); |
---|
41 | @ISA = qw(Exporter); |
---|
42 | @EXPORT = (qw( |
---|
43 | la_set_log la_log |
---|
44 | ), @loglevels); |
---|
45 | |
---|
46 | @EXPORT_OK = @EXPORT; |
---|
47 | %EXPORT_TAGS = (LOGLEVELS => [ @loglevels ]); |
---|
48 | |
---|
49 | our $VERSION = (q$Rev: 1915 $ =~ /^Rev: (\d+) /)[0]; |
---|
50 | |
---|
51 | my %lastmessages = (); |
---|
52 | |
---|
53 | =head1 LOG LEVEL |
---|
54 | |
---|
55 | =over 4 |
---|
56 | |
---|
57 | =item LA_EMERG |
---|
58 | |
---|
59 | =cut |
---|
60 | |
---|
61 | sub LA_EMERG { LOG_EMERG } # system is unusable |
---|
62 | |
---|
63 | =item LA_ALERT |
---|
64 | |
---|
65 | =cut |
---|
66 | |
---|
67 | sub LA_ALERT { LOG_ALERT } # action must be taken immediately |
---|
68 | |
---|
69 | =item LA_CRIT |
---|
70 | |
---|
71 | =cut |
---|
72 | |
---|
73 | sub LA_CRIT { LOG_CRIT } # critical conditions |
---|
74 | |
---|
75 | =item LA_ERR, LA_ERROR |
---|
76 | |
---|
77 | =cut |
---|
78 | |
---|
79 | sub LA_ERR { LOG_ERR } # error conditions |
---|
80 | sub LA_ERROR { LA_ERR } # Alias for ERR |
---|
81 | |
---|
82 | =item LA_WARN, LA_WARNING |
---|
83 | |
---|
84 | =cut |
---|
85 | |
---|
86 | sub LA_WARNING { LOG_WARNING } # warning conditions |
---|
87 | sub LA_WARN { LA_WARNING } # warning conditions |
---|
88 | |
---|
89 | =item LA_NOTICE |
---|
90 | |
---|
91 | =cut |
---|
92 | |
---|
93 | sub LA_NOTICE { LOG_NOTICE } # normal, but significant, condition |
---|
94 | |
---|
95 | =item LA_INFO |
---|
96 | |
---|
97 | =cut |
---|
98 | |
---|
99 | sub LA_INFO { LOG_INFO } # informational message |
---|
100 | |
---|
101 | =item LA_DEBUG |
---|
102 | |
---|
103 | =cut |
---|
104 | |
---|
105 | sub LA_DEBUG { LOG_DEBUG } # debug-level message |
---|
106 | |
---|
107 | =back |
---|
108 | |
---|
109 | =cut |
---|
110 | |
---|
111 | my %log_method = ( |
---|
112 | syslog => undef, |
---|
113 | console => LA_NOTICE, |
---|
114 | callback => undef, |
---|
115 | mail => undef, |
---|
116 | ); |
---|
117 | |
---|
118 | my @maillog = (); |
---|
119 | |
---|
120 | =head1 FUNCTIONS |
---|
121 | |
---|
122 | =head2 la_set_log(%options) |
---|
123 | |
---|
124 | Set options to log dispatcher: |
---|
125 | |
---|
126 | =over 4 |
---|
127 | |
---|
128 | =item syslog |
---|
129 | |
---|
130 | =item console |
---|
131 | |
---|
132 | =item callback |
---|
133 | |
---|
134 | =back |
---|
135 | |
---|
136 | =cut |
---|
137 | |
---|
138 | sub la_set_log { |
---|
139 | my (%options) = @_; |
---|
140 | while (my ($key, $val) = each(%options)) { |
---|
141 | if ($key eq 'syslog') { |
---|
142 | if ($val) { |
---|
143 | my ($ident, $logopt, $facility) = @{ ref $val ? $val : [] }; |
---|
144 | $ident ||= 'LATMOS::Accounts'; |
---|
145 | $logopt ||= 'pid'; |
---|
146 | $facility ||= 'LOG_USER'; |
---|
147 | openlog($ident, $logopt, $facility); |
---|
148 | $log_method{syslog} = 1; |
---|
149 | } |
---|
150 | next; |
---|
151 | } |
---|
152 | $log_method{$key} = $val; |
---|
153 | } |
---|
154 | 1; |
---|
155 | } |
---|
156 | |
---|
157 | =head2 lastmessage($level) |
---|
158 | |
---|
159 | Return the last message for C<$level> (LA_ERROR by default) |
---|
160 | |
---|
161 | =cut |
---|
162 | |
---|
163 | sub lastmessage { |
---|
164 | my ($level) = @_; |
---|
165 | return $lastmessages{$level || LA_ERROR}; |
---|
166 | } |
---|
167 | |
---|
168 | =head2 flush_last_message ($level) |
---|
169 | |
---|
170 | Clean message history, if defined on message for C<$level> are cleaned |
---|
171 | |
---|
172 | =cut |
---|
173 | |
---|
174 | sub flush_last_message { |
---|
175 | my ($level) = @_; |
---|
176 | |
---|
177 | if ( defined( $level ) ) { |
---|
178 | $lastmessages{$level} = undef; |
---|
179 | } else { |
---|
180 | %lastmessages = (); |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | =head2 la_log($level, @sprintf_args) |
---|
185 | |
---|
186 | =cut |
---|
187 | |
---|
188 | sub la_log { |
---|
189 | my ($level, $msg, @args) = @_; |
---|
190 | no warnings 'printf'; |
---|
191 | |
---|
192 | if (!$msg) { |
---|
193 | # Wrong la_log usage |
---|
194 | my @call = caller(); |
---|
195 | la_log(LA_WARN, 'empty message at %s:%s', $call[1], $call[2]); |
---|
196 | return; |
---|
197 | } |
---|
198 | if ($level !~ /^\d$/) { |
---|
199 | # Wrong la_log usage |
---|
200 | my @call = caller(); |
---|
201 | la_log(LA_WARN, 'unrecognize message level %s at %s:%s', $level, $call[1], $call[2]); |
---|
202 | return; |
---|
203 | } |
---|
204 | |
---|
205 | $lastmessages{$level} = sprintf($msg, map { defined($_) ? $_ : '' } @args); |
---|
206 | if ($log_method{syslog}) { |
---|
207 | syslog($level, $msg, @args) unless($level >= LA_DEBUG); |
---|
208 | } |
---|
209 | if ($ENV{'LA_DEBUG'}) { |
---|
210 | my @caller = caller; |
---|
211 | my $debug = ($level == LA_DEBUG) ? "$caller[0]:$caller[2] " : ''; |
---|
212 | warn sprintf("$debug$msg", @args) . "\n"; |
---|
213 | } elsif ($log_method{console} && $level <= $log_method{console}) { |
---|
214 | if ($level >= LA_NOTICE && $level < LA_DEBUG) { |
---|
215 | printf("$msg\n", @args); |
---|
216 | } else { |
---|
217 | my $debug = ''; |
---|
218 | if ($level == LA_DEBUG) { |
---|
219 | my @caller = caller; |
---|
220 | $debug = "$caller[0]:$caller[2] "; |
---|
221 | } |
---|
222 | warn sprintf("$debug$msg", map { defined($_) ? $_ : '' } @args) . "\n"; |
---|
223 | } |
---|
224 | } |
---|
225 | if ($log_method{callback}) { |
---|
226 | $log_method{callback}->($level, $msg, @args); |
---|
227 | } |
---|
228 | if ($log_method{mail}) { |
---|
229 | # store error to send it later, only ERROR |
---|
230 | push(@maillog, sprintf($msg, @args)) if ($level <= LA_ERROR); |
---|
231 | } |
---|
232 | 1; |
---|
233 | } |
---|
234 | |
---|
235 | sub _flush_mail { |
---|
236 | @maillog = (); |
---|
237 | } |
---|
238 | |
---|
239 | sub _send_mail_log { |
---|
240 | @maillog or return; |
---|
241 | sendmail( |
---|
242 | Subject => "LATMOS::Accounts error from $0", |
---|
243 | To => $log_method{mail}, |
---|
244 | From => 'LATMOS-Accounts@latmos.ipsl.fr', |
---|
245 | Message => join("\n", @maillog), |
---|
246 | ) or la_log(LA_ERR, "Cannot sent mail: " . $Mail::Sendmail::error); |
---|
247 | _flush_mail(); |
---|
248 | } |
---|
249 | |
---|
250 | END { |
---|
251 | _send_mail_log() if($log_method{mail}); |
---|
252 | } |
---|
253 | |
---|
254 | 1; |
---|
255 | |
---|
256 | __END__ |
---|
257 | |
---|
258 | =head1 SEE ALSO |
---|
259 | |
---|
260 | =head1 AUTHOR |
---|
261 | |
---|
262 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
263 | |
---|
264 | =head1 COPYRIGHT AND LICENSE |
---|
265 | |
---|
266 | Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier |
---|
267 | |
---|
268 | This library is free software; you can redistribute it and/or modify |
---|
269 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
270 | at your option, any later version of Perl 5 you may have available. |
---|
271 | |
---|
272 | =cut |
---|
273 | |
---|