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

Last change on this file since 291 was 278, checked in by nanardon, 15 years ago
  • replace name process in syslog
  • Property svn:keywords set to Id Rev
File size: 3.2 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
50=head1 LOG LEVEL
51
52=over 4
53
54=item LA_EMERG
55
56=cut
57
58sub LA_EMERG { LOG_EMERG } # system is unusable
59
60=item LA_ALERT
61
62=cut
63
64sub LA_ALERT { LOG_ALERT } # action must be taken immediately
65
66=item LA_CRIT
67
68=cut
69sub LA_CRIT { LOG_CRIT } # critical conditions
70
71=item LA_ERR
72=item LA_ERROR
73
74=cut
75
76sub LA_ERR   { LOG_ERR } # error conditions
77sub LA_ERROR { LA_ERR } # Alias for ERR
78
79=item LA_WARN
80=item LA_WARNING
81
82=cut
83
84sub LA_WARNING { LOG_WARNING } # warning conditions
85sub LA_WARN    { LA_WARNING } # warning conditions
86
87=item LA_NOTICE
88
89=cut
90
91sub LA_NOTICE { LOG_NOTICE } # normal, but significant, condition
92
93=item LA_INFO
94
95=cut
96
97sub LA_INFO { LOG_INFO } # informational message
98
99=item LA_DEBUG
100
101=cut
102
103sub LA_DEBUG { LOG_DEBUG } # debug-level message
104
105=back
106
107=cut
108
109my %log_method = (
110    syslog => undef,
111    console => LA_INFO,
112    callback => undef,
113);
114
115=head1 FUNCTIONS
116
117=head2 la_set_log(%options)
118
119Set options to log dispatcher:
120
121=over 4
122
123=item syslog
124
125=item console
126
127=item callback
128
129=back
130
131=cut
132
133sub la_set_log {
134    my (%options) = @_;
135    while (my ($key, $val) = each(%options)) {
136        if ($key eq 'syslog') {
137            if ($val) {
138                my ($ident, $logopt, $facility) =  @{ ref $val ? $val : [] };
139                $ident ||= 'LATMOS::Accounts';
140                $logopt ||= 'pid';
141                $facility ||= 'LOG_USER';
142                openlog($ident, $logopt, $facility);
143                $log_method{syslog} = 1;
144            }
145            next;
146        }
147        $log_method{$key} = $val;
148    }
149    1;
150}
151
152=head2 la_log($level, @sprintf_args)
153
154=cut
155
156sub la_log {
157    my ($level, $msg, @args) = @_;
158    if ($log_method{syslog}) {
159        syslog($level, $msg, @args) unless($level >= LA_DEBUG);
160    }
161    if ($ENV{'LA_DEBUG'}) {
162        my @caller = caller;
163        my $debug = ($level == LA_DEBUG) ? "$caller[0]:$caller[2] " : '';
164        warn sprintf("$debug$msg", @args) . "\n";
165    } elsif ($log_method{console} && $level <= $log_method{console}) {
166        if ($level >= LA_NOTICE && $level < LA_DEBUG) {
167            printf("$msg\n", @args);
168        } else {
169            my $debug = '';
170            if ($level == LA_DEBUG) {
171                my @caller = caller;
172                $debug = "$caller[0]:$caller[2] ";
173            }
174            warn sprintf("$debug$msg", @args) . "\n";
175        }
176    }
177    if ($log_method{callback}) {
178        $log_method{callback}->($level, $msg, @args);
179    }
180    1;
181}
182   
1831;
Note: See TracBrowser for help on using the repository browser.