source: LATMOS-Accounts/lib/LATMOS/Accounts/Log.pm @ 479

Last change on this file since 479 was 476, checked in by nanardon, 15 years ago
  • raise default log level on console to LA_NOTICE
  • Property svn:keywords set to Id Rev
File size: 3.4 KB
Line 
1package LATMOS::Accounts::Log;
2
3use strict;
4use warnings;
5use Sys::Syslog qw(:standard :macros);
6use Exporter ();
7
8=head1 NAME
9
10LATMOS::Accounts::Log - Log dispatcher for LATMOS::Accounts
11
12=head1 SYNOPSYS
13
14    use LATMOS::Accounts::Log
15    la_log(LA_ERR, "An error has occur");
16
17=head1 DESCRIPTION
18
19This module provide facilities to log to both console, syslog or using callback.
20
21When the environement variable C<LA_DEBUG> is set, all message are print to
22C<stderr>.
23
24=cut
25
26our @loglevels = qw(
27    LA_EMERG
28    LA_ALERT
29    LA_CRIT
30    LA_ERR
31    LA_ERROR
32    LA_WARN
33    LA_WARNING
34    LA_NOTICE
35    LA_INFO
36    LA_DEBUG
37);
38
39use vars qw(@EXPORT_OK @EXPORT %EXPORT_TAGS @ISA);
40@ISA = qw(Exporter);
41@EXPORT = (qw(
42    la_set_log la_log
43    ), @loglevels);
44
45@EXPORT_OK = @EXPORT;
46%EXPORT_TAGS = (LOGLEVELS => [ @loglevels ]);
47
48our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
49
50my %lastmessages = ();
51
52=head1 LOG LEVEL
53
54=over 4
55
56=item LA_EMERG
57
58=cut
59
60sub LA_EMERG { LOG_EMERG } # system is unusable
61
62=item LA_ALERT
63
64=cut
65
66sub LA_ALERT { LOG_ALERT } # action must be taken immediately
67
68=item LA_CRIT
69
70=cut
71sub LA_CRIT { LOG_CRIT } # critical conditions
72
73=item LA_ERR
74=item LA_ERROR
75
76=cut
77
78sub LA_ERR   { LOG_ERR } # error conditions
79sub LA_ERROR { LA_ERR } # Alias for ERR
80
81=item LA_WARN
82=item LA_WARNING
83
84=cut
85
86sub LA_WARNING { LOG_WARNING } # warning conditions
87sub LA_WARN    { LA_WARNING } # warning conditions
88
89=item LA_NOTICE
90
91=cut
92
93sub LA_NOTICE { LOG_NOTICE } # normal, but significant, condition
94
95=item LA_INFO
96
97=cut
98
99sub LA_INFO { LOG_INFO } # informational message
100
101=item LA_DEBUG
102
103=cut
104
105sub LA_DEBUG { LOG_DEBUG } # debug-level message
106
107=back
108
109=cut
110
111my %log_method = (
112    syslog => undef,
113    console => LA_NOTICE,
114    callback => undef,
115);
116
117=head1 FUNCTIONS
118
119=head2 la_set_log(%options)
120
121Set options to log dispatcher:
122
123=over 4
124
125=item syslog
126
127=item console
128
129=item callback
130
131=back
132
133=cut
134
135sub la_set_log {
136    my (%options) = @_;
137    while (my ($key, $val) = each(%options)) {
138        if ($key eq 'syslog') {
139            if ($val) {
140                my ($ident, $logopt, $facility) =  @{ ref $val ? $val : [] };
141                $ident ||= 'LATMOS::Accounts';
142                $logopt ||= 'pid';
143                $facility ||= 'LOG_USER';
144                openlog($ident, $logopt, $facility);
145                $log_method{syslog} = 1;
146            }
147            next;
148        }
149        $log_method{$key} = $val;
150    }
151    1;
152}
153
154sub lastmessage {
155    my ($level) = @_;
156    return $lastmessages{$level};
157}
158
159=head2 la_log($level, @sprintf_args)
160
161=cut
162
163sub la_log {
164    my ($level, $msg, @args) = @_;
165    $lastmessages{$level} = sprintf($msg, @args);
166    if ($log_method{syslog}) {
167        syslog($level, $msg, @args) unless($level >= LA_DEBUG);
168    }
169    if ($ENV{'LA_DEBUG'}) {
170        my @caller = caller;
171        my $debug = ($level == LA_DEBUG) ? "$caller[0]:$caller[2] " : '';
172        warn sprintf("$debug$msg", @args) . "\n";
173    } elsif ($log_method{console} && $level <= $log_method{console}) {
174        if ($level >= LA_NOTICE && $level < LA_DEBUG) {
175            printf("$msg\n", @args);
176        } else {
177            my $debug = '';
178            if ($level == LA_DEBUG) {
179                my @caller = caller;
180                $debug = "$caller[0]:$caller[2] ";
181            }
182            warn sprintf("$debug$msg", @args) . "\n";
183        }
184    }
185    if ($log_method{callback}) {
186        $log_method{callback}->($level, $msg, @args);
187    }
188    1;
189}
190   
1911;
Note: See TracBrowser for help on using the repository browser.