source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Log.pm @ 1014

Last change on this file since 1014 was 1014, checked in by nanardon, 12 years ago
  • fix/complete/improve documentation
  • Property svn:keywords set to Id Rev
File size: 4.4 KB
Line 
1package LATMOS::Accounts::Log;
2
3use strict;
4use warnings;
5use Sys::Syslog qw(:standard :macros);
6use Exporter ();
7use Mail::Sendmail;
8
9=head1 NAME
10
11LATMOS::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
20This module provide facilities to log to both console, syslog or using callback.
21
22When the environement variable C<LA_DEBUG> is set, all message are print to
23C<stderr>.
24
25=cut
26
27our @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
40use 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
49our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
50
51my %lastmessages = ();
52
53=head1 LOG LEVEL
54
55=over 4
56
57=item LA_EMERG
58
59=cut
60
61sub LA_EMERG { LOG_EMERG } # system is unusable
62
63=item LA_ALERT
64
65=cut
66
67sub LA_ALERT { LOG_ALERT } # action must be taken immediately
68
69=item LA_CRIT
70
71=cut
72
73sub LA_CRIT { LOG_CRIT } # critical conditions
74
75=item LA_ERR
76=item LA_ERROR
77
78=cut
79
80sub LA_ERR   { LOG_ERR } # error conditions
81sub LA_ERROR { LA_ERR } # Alias for ERR
82
83=item LA_WARN
84=item LA_WARNING
85
86=cut
87
88sub LA_WARNING { LOG_WARNING } # warning conditions
89sub LA_WARN    { LA_WARNING } # warning conditions
90
91=item LA_NOTICE
92
93=cut
94
95sub LA_NOTICE { LOG_NOTICE } # normal, but significant, condition
96
97=item LA_INFO
98
99=cut
100
101sub LA_INFO { LOG_INFO } # informational message
102
103=item LA_DEBUG
104
105=cut
106
107sub LA_DEBUG { LOG_DEBUG } # debug-level message
108
109=back
110
111=cut
112
113my %log_method = (
114    syslog => undef,
115    console => LA_NOTICE,
116    callback => undef,
117    mail => undef,
118);
119
120my @maillog = ();
121
122=head1 FUNCTIONS
123
124=head2 la_set_log(%options)
125
126Set options to log dispatcher:
127
128=over 4
129
130=item syslog
131
132=item console
133
134=item callback
135
136=back
137
138=cut
139
140sub la_set_log {
141    my (%options) = @_;
142    while (my ($key, $val) = each(%options)) {
143        if ($key eq 'syslog') {
144            if ($val) {
145                my ($ident, $logopt, $facility) =  @{ ref $val ? $val : [] };
146                $ident ||= 'LATMOS::Accounts';
147                $logopt ||= 'pid';
148                $facility ||= 'LOG_USER';
149                openlog($ident, $logopt, $facility);
150                $log_method{syslog} = 1;
151            }
152            next;
153        }
154        $log_method{$key} = $val;
155    }
156    1;
157}
158
159sub lastmessage {
160    my ($level) = @_;
161    return $lastmessages{$level || LA_ERROR};
162}
163
164=head2 la_log($level, @sprintf_args)
165
166=cut
167
168sub la_log {
169    my ($level, $msg, @args) = @_;
170    $lastmessages{$level} = sprintf($msg, @args);
171    if ($log_method{syslog}) {
172        syslog($level, $msg, @args) unless($level >= LA_DEBUG);
173    }
174    if ($ENV{'LA_DEBUG'}) {
175        my @caller = caller;
176        my $debug = ($level == LA_DEBUG) ? "$caller[0]:$caller[2] " : '';
177        warn sprintf("$debug$msg", @args) . "\n";
178    } elsif ($log_method{console} && $level <= $log_method{console}) {
179        if ($level >= LA_NOTICE && $level < LA_DEBUG) {
180            printf("$msg\n", @args);
181        } else {
182            my $debug = '';
183            if ($level == LA_DEBUG) {
184                my @caller = caller;
185                $debug = "$caller[0]:$caller[2] ";
186            }
187            warn sprintf("$debug$msg", @args) . "\n";
188        }
189    }
190    if ($log_method{callback}) {
191        $log_method{callback}->($level, $msg, @args);
192    }
193    if ($log_method{mail}) {
194        # store error to send it later, only ERROR
195        push(@maillog, sprintf($msg, @args)) if ($level <= LA_ERROR);
196    }
197    1;
198}
199
200sub _flush_mail {
201    @maillog = ();
202}
203
204sub _send_mail_log {
205    @maillog or return;
206    sendmail(
207        Subject => "LATMOS::Accounts error from $0",
208        To => $log_method{mail},
209        From => 'LATMOS-Accounts@latmos.ipsl.fr',
210        Message => join("\n", @maillog),
211    ) or la_log(LA_ERR, "Cannot sent mail: " . $Mail::Sendmail::error);
212    _flush_mail();
213}
214
215END {
216    _send_mail_log() if($log_method{mail});
217}
218
2191;
220
221__END__
222
223=head1 SEE ALSO
224
225=head1 AUTHOR
226
227Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
228
229=head1 COPYRIGHT AND LICENSE
230
231Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier
232
233This library is free software; you can redistribute it and/or modify
234it under the same terms as Perl itself, either Perl version 5.10.0 or,
235at your option, any later version of Perl 5 you may have available.
236
237=cut
238
Note: See TracBrowser for help on using the repository browser.