]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/Utils/Logger.pm
typo
[OpenSRF.git] / src / perl / lib / OpenSRF / Utils / Logger.pm
1 package OpenSRF::Utils::Logger;
2 # vim:ts=4:noet:
3 use strict;
4 use vars qw($AUTOLOAD @EXPORT_OK %EXPORT_TAGS);
5 use Exporter;
6 use Unix::Syslog qw(:macros :subs);
7 use base qw/OpenSRF Exporter/;
8 use FileHandle;
9 use Time::HiRes qw(gettimeofday);
10 use OpenSRF::Utils::Config;
11 use Fcntl;
12
13 =head1
14
15 Logger code
16
17 my $logger = OpenSRF::Utils::Logger;
18 $logger->error( $msg );
19
20 For backwards compability, a log level may also be provided to each log
21 function thereby overriding the level defined by the function.
22
23 i.e. $logger->error( $msg, WARN );  # logs at log level WARN
24
25 =cut
26
27 @EXPORT_OK = qw/ NONE ERROR WARN INFO DEBUG INTERNAL /;
28 push @EXPORT_OK, '$logger';
29
30 %EXPORT_TAGS = ( level => [ qw/ NONE ERROR WARN INFO DEBUG INTERNAL / ], logger => [ '$logger' ] );
31
32 my $config;                                                     # config handle
33 my $loglevel = INFO();                          # global log level
34 my $logfile;                                            # log file
35 my $facility;                                           # syslog facility
36 my $actfac;                                                     # activity log syslog facility
37 my $actfile;                                            # activity log file
38 my $service = $0;                       # default service name
39 my $syslog_enabled = 0;                 # is syslog enabled?
40 my $act_syslog_enabled = 0;     # is syslog enabled?
41 my $logfile_enabled = 1;                # are we logging to a file?
42 my $act_logfile_enabled = 1;    # are we logging to a file?
43
44 our $logger = "OpenSRF::Utils::Logger";
45
46 # log levels
47 sub ACTIVITY    { return -1; }
48 sub NONE                        { return 0;     }
49 sub ERROR               { return 1;     }
50 sub WARN                        { return 2;     }
51 sub INFO                        { return 3;     }
52 sub DEBUG               { return 4;     }
53 sub INTERNAL    { return 5;     }
54 sub ALL                 { return 100; }
55
56 my $isclient;  # true if we control the osrf_xid
57
58 # load up our config options
59 sub set_config {
60
61         return if defined $config;
62
63         $config = OpenSRF::Utils::Config->current;
64         if( !defined($config) ) {
65                 $loglevel = INFO();
66                 warn "*** Logger found no config.  Using STDERR ***\n";
67                 return;
68         }
69
70         $loglevel =  $config->bootstrap->loglevel; 
71
72         $logfile = $config->bootstrap->logfile;
73         if($logfile =~ /^syslog/) {
74                 $syslog_enabled = 1;
75                 $logfile_enabled = 0;
76         $logfile = $config->bootstrap->syslog;
77                 $facility = $logfile;
78                 $logfile = undef;
79                 $facility = _fac_to_const($facility);
80                 openlog($service, 0, $facility);
81
82         } else { $logfile = "$logfile"; }
83
84
85     if($syslog_enabled) {
86         # --------------------------------------------------------------
87         # if we're syslogging, see if we have a special syslog facility 
88         # for activity logging.  If not, use the syslog facility for
89         # standard logging
90         # --------------------------------------------------------------
91         $act_syslog_enabled = 1;
92         $act_logfile_enabled = 0;
93         $actfac = $config->bootstrap->actlog || $config->bootstrap->syslog;
94         $actfac = _fac_to_const($actfac);
95         $actfile = undef;
96     } else {
97         # --------------------------------------------------------------
98         # we're not syslogging, use any specified activity log file.
99         # Fall back to the standard log file otherwise
100         # --------------------------------------------------------------
101                 $act_syslog_enabled = 0;
102                 $act_logfile_enabled = 1;
103         $actfile = $config->bootstrap->actlog || $config->bootstrap->logfile;
104     }
105
106         my $client = OpenSRF::Utils::Config->current->bootstrap->client();
107         if (!$client) {
108                 $isclient = 0;
109                 return;
110         }
111         $isclient = ($client =~ /^true$/iog) ?  1 : 0;
112 }
113
114 sub _fac_to_const {
115         my $name = shift;
116         return LOG_LOCAL0 unless $name;
117         return LOG_LOCAL0 if $name =~ /local0/i;
118         return LOG_LOCAL1 if $name =~ /local1/i;
119         return LOG_LOCAL2 if $name =~ /local2/i;
120         return LOG_LOCAL3 if $name =~ /local3/i;
121         return LOG_LOCAL4 if $name =~ /local4/i;
122         return LOG_LOCAL5 if $name =~ /local5/i;
123         return LOG_LOCAL6 if $name =~ /local6/i;
124         return LOG_LOCAL7 if $name =~ /local7/i;
125         return LOG_LOCAL0;
126 }
127
128 sub is_syslog {
129         set_config();
130         return $syslog_enabled;
131 }
132
133 sub is_act_syslog {
134         set_config();
135         return $act_syslog_enabled;
136 }
137
138 sub is_filelog {
139         set_config();
140         return $logfile_enabled;
141 }
142
143 sub is_act_filelog {
144         set_config();
145         return $act_logfile_enabled;
146 }
147
148 sub set_service {
149         my( $self, $svc ) = @_;
150         $service = $svc;        
151         if( is_syslog() ) {
152                 closelog();
153                 openlog($service, 0, $facility);
154         }
155 }
156
157 sub error {
158         my( $self, $msg, $level ) = @_;
159         $level = ERROR() unless defined ($level);
160         _log_message( $msg, $level );
161 }
162
163 sub warn {
164         my( $self, $msg, $level ) = @_;
165         $level = WARN() unless defined ($level);
166         _log_message( $msg, $level );
167 }
168
169 sub info {
170         my( $self, $msg, $level ) = @_;
171         $level = INFO() unless defined ($level);
172         _log_message( $msg, $level );
173 }
174
175 sub debug {
176         my( $self, $msg, $level ) = @_;
177         $level = DEBUG() unless defined ($level);
178         _log_message( $msg, $level );
179 }
180
181 sub internal {
182         my( $self, $msg, $level ) = @_;
183         $level = INTERNAL() unless defined ($level);
184         _log_message( $msg, $level );
185 }
186
187 sub activity {
188         my( $self, $msg ) = @_;
189         _log_message( $msg, ACTIVITY() );
190 }
191
192 # for backward compability
193 sub transport {
194         my( $self, $msg, $level ) = @_;
195         $level = DEBUG() unless defined ($level);
196         _log_message( $msg, $level );
197 }
198
199
200 # ----------------------------------------------------------------------
201 # creates a new xid if necessary
202 # ----------------------------------------------------------------------
203 my $osrf_xid = '';
204 my $osrf_xid_inc = 0;
205 sub mk_osrf_xid {
206    return unless $isclient;
207    $osrf_xid_inc++;
208    return $osrf_xid = "$^T${$}$osrf_xid_inc";
209 }
210
211 sub set_osrf_xid { 
212    return if $isclient; # if we're a client, we control our xid
213    $osrf_xid = $_[1]; 
214 }
215
216 sub get_osrf_xid { return $osrf_xid; }
217 # ----------------------------------------------------------------------
218
219    
220 sub _log_message {
221         my( $msg, $level ) = @_;
222         return if $level > $loglevel;
223
224         my $l; my $n; 
225         my $fac = $facility;
226
227         if ($level == ERROR())                  {$l = LOG_ERR; $n = "ERR "; }
228         elsif ($level == WARN())                {$l = LOG_WARNING; $n = "WARN"; }
229         elsif ($level == INFO())                {$l = LOG_INFO; $n = "INFO"; }  
230         elsif ($level == DEBUG())               {$l = LOG_DEBUG; $n = "DEBG"; }
231         elsif ($level == INTERNAL())    {$l = LOG_DEBUG; $n = "INTL"; }
232         elsif ($level == ACTIVITY())    {$l = LOG_INFO; $n = "ACT"; $fac = $actfac; }
233
234         my( undef, $file, $line_no ) = caller(1);
235    $file =~ s#/.*/##og;
236
237         # help syslog with the formatting
238         $msg =~ s/\%/\%\%/gso if( is_act_syslog() or is_syslog() );
239
240         $msg = "[$n:"."$$".":$file:$line_no:$osrf_xid] $msg";
241
242         $msg = substr($msg, 0, 1536); 
243
244         if( $level == ACTIVITY() ) {
245                 if( is_act_syslog() ) { syslog( $fac | $l, $msg ); } 
246                 elsif( is_act_filelog() ) { _write_file( $msg, 1 ); }
247
248         } else {
249                 if( is_syslog() ) { syslog( $fac | $l, $msg ); }
250                 elsif( is_filelog() ) { _write_file($msg); }
251         }
252 }
253
254
255 sub _write_file {
256         my( $msg, $isact) = @_;
257         my $file = $logfile;
258         $file = $actfile if $isact;
259         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);  
260         $year += 1900; $mon += 1;
261         sysopen( SINK, $file, O_NONBLOCK|O_WRONLY|O_APPEND|O_CREAT ) 
262                 or die "Cannot sysopen $logfile: $!";
263         binmode(SINK, ':utf8');
264         printf SINK "[%04d-%02d-%02d %02d:%02d:%02d] %s %s\n", $year, $mon, $mday, $hour, $min, $sec, $service, $msg;
265         close( SINK );
266 }
267
268
269
270 1;