line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBR::Util::Logger; |
2
|
|
|
|
|
|
|
|
3
|
18
|
|
|
18
|
|
119
|
use vars qw(@ISA @EXPORT); |
|
18
|
|
|
|
|
50
|
|
|
18
|
|
|
|
|
1461
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
@ISA = ('Exporter'); |
6
|
|
|
|
|
|
|
|
7
|
18
|
|
|
18
|
|
112
|
use strict; |
|
18
|
|
|
|
|
58
|
|
|
18
|
|
|
|
|
639
|
|
8
|
18
|
|
|
18
|
|
97
|
use Carp; |
|
18
|
|
|
|
|
37
|
|
|
18
|
|
|
|
|
1065
|
|
9
|
18
|
|
|
18
|
|
23677
|
use FileHandle; |
|
18
|
|
|
|
|
284940
|
|
|
18
|
|
|
|
|
279
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=pod |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
DBR::Util::Logger |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use DBR::Util::Logger; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$logger = new DBR::Util::Logger( [ |
23
|
|
|
|
|
|
|
-user_id => $user_id, # Optional |
24
|
|
|
|
|
|
|
-logPath => $alternatePath, |
25
|
|
|
|
|
|
|
-logLevel => 'level' # None, Info, Warn, Debug, Debug2, Debug3 |
26
|
|
|
|
|
|
|
-bDebug => $boolDebug, # Deprecated |
27
|
|
|
|
|
|
|
-noLog => $boolNoLog, # Deprecated ] |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 DESCRIPTION |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
The purpose of the Logger Object is to log script information |
33
|
|
|
|
|
|
|
on a per user basis, as well as keep a transaction log of |
34
|
|
|
|
|
|
|
all DB related API Calls. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 METHODS |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head2 new (Constructor) |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=over 4 |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=item B<-user_id> |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=item B<-logpath> |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=item B<-logLevel> |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=back |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=cut |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub new { |
53
|
17
|
|
|
17
|
1
|
113
|
my( $pkg, %in ) = @_; |
54
|
|
|
|
|
|
|
|
55
|
17
|
|
|
|
|
75
|
my( $self ) = {}; |
56
|
|
|
|
|
|
|
|
57
|
17
|
|
|
|
|
75
|
bless( $self, $pkg ); |
58
|
|
|
|
|
|
|
|
59
|
17
|
|
|
|
|
46
|
my $level; |
60
|
17
|
50
|
|
|
|
138
|
if ($in{-logLevel}) { |
61
|
17
|
|
|
|
|
67
|
$level = $in{-logLevel} |
62
|
|
|
|
|
|
|
} else { |
63
|
0
|
0
|
|
|
|
0
|
$level = 'none' if $in{-noLog}; |
64
|
0
|
0
|
|
|
|
0
|
$level = 'debug' if $in{-bDebug}; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
17
|
|
|
|
|
74
|
$level = lc($level); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
17
|
|
|
|
|
183
|
my @levels = qw'none error warn info debug debug2 debug3'; |
71
|
17
|
|
|
|
|
53
|
my $ct = 0; |
72
|
17
|
|
|
|
|
45
|
my %levmap; |
73
|
17
|
|
|
|
|
72
|
map {$levmap{$_} = $ct++} @levels; |
|
119
|
|
|
|
|
369
|
|
74
|
17
|
|
|
|
|
288
|
$self->{levmap} = \%levmap; |
75
|
|
|
|
|
|
|
|
76
|
17
|
50
|
|
|
|
106
|
$level = 'info' unless defined($levmap{$level}); |
77
|
17
|
|
|
|
|
67
|
$self->{loglevel} = $levmap{$level}; |
78
|
|
|
|
|
|
|
|
79
|
17
|
|
50
|
|
|
245
|
$self->{logbase} = $in{-logPath} || $in{-logpath} || ''; |
80
|
|
|
|
|
|
|
|
81
|
17
|
50
|
|
|
|
96
|
if ( $in{-user_id} ) { |
82
|
0
|
|
|
|
|
0
|
$self->{user_id} = $in{-user_id}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
17
|
|
|
|
|
152
|
return( $self ); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=pod |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head2 log |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
This method provides logging (optionally on a per user basis). |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=cut |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub log { |
97
|
1586
|
|
|
1586
|
1
|
3300
|
my $self = shift; |
98
|
1586
|
|
|
|
|
18054
|
my $msg = shift; |
99
|
1586
|
|
|
|
|
11081
|
my $caller = shift; |
100
|
1586
|
|
|
|
|
2700
|
my $type = shift; |
101
|
|
|
|
|
|
|
|
102
|
1586
|
50
|
|
|
|
5900
|
return unless( $self->{loglevel} ); |
103
|
|
|
|
|
|
|
|
104
|
1586
|
|
50
|
|
|
4221
|
$type ||= 'info'; |
105
|
1586
|
|
|
|
|
25038
|
$type = lc($type); |
106
|
|
|
|
|
|
|
|
107
|
1586
|
50
|
|
|
|
7111
|
return unless $self->{levmap}->{$type}; |
108
|
1586
|
50
|
|
|
|
8049
|
return unless $self->{levmap}->{$type} <= $self->{loglevel}; |
109
|
|
|
|
|
|
|
|
110
|
1586
|
|
|
|
|
5697
|
my $fh = $self->{HANDLE}; |
111
|
|
|
|
|
|
|
|
112
|
1586
|
100
|
|
|
|
6546
|
if (!defined($fh)) { |
113
|
17
|
|
|
|
|
37
|
my $logpath; |
114
|
17
|
50
|
|
|
|
127
|
if ( $self->{user_id} ) { |
115
|
0
|
|
|
|
|
0
|
my $user_id = ( ('0'x(9 - length ($self->{user_id}))) . $self->{user_id}); |
116
|
0
|
|
|
|
|
0
|
my $user_a = substr( $user_id, 0, 3 ); |
117
|
0
|
|
|
|
|
0
|
my $user_b = substr( $user_id, 3, 3 ); |
118
|
0
|
|
|
|
|
0
|
$logpath = "$self->{logbase}/$user_a/$user_b/$user_id"; |
119
|
|
|
|
|
|
|
} else { |
120
|
17
|
|
|
|
|
1222
|
$logpath = $self->{logbase}; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
17
|
|
|
|
|
209
|
$fh = new FileHandle; |
125
|
17
|
|
|
|
|
1049
|
$fh->autoflush(1); |
126
|
|
|
|
|
|
|
|
127
|
17
|
|
|
|
|
1228
|
my $dirpath = $logpath; |
128
|
17
|
|
|
|
|
168
|
$dirpath =~ s/[^\/]*$//; # strip filename |
129
|
|
|
|
|
|
|
|
130
|
17
|
50
|
|
|
|
1112
|
$self->_prepdir($dirpath) || print STDERR "DBR::Util::Logger: FAILED to Prepare log path $dirpath\n"; |
131
|
17
|
50
|
|
|
|
1115
|
sysopen( $fh, $logpath, O_WRONLY|O_CREAT|O_APPEND, 0666 ) || print STDERR "DBR::Util::Logger: FAILED to open log $logpath\n"; |
132
|
|
|
|
|
|
|
|
133
|
17
|
|
|
|
|
77
|
$self->{HANDLE} = $fh; |
134
|
|
|
|
|
|
|
|
135
|
17
|
|
|
|
|
255
|
$self->log( "New Logger $logpath opened by $caller",'DBR::Util::Logger','debug2'); # Yes, its recursive, but only once. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
1586
|
|
|
|
|
6681
|
my($s,$m,$h,$D,$M,$Y) = getTime(); |
140
|
1586
|
|
|
|
|
4677
|
$type = uc($type); |
141
|
1586
|
|
|
|
|
88345
|
print $fh "$Y$M$D$h$m$s\t$type\t$caller\t$msg\n"; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub _prepdir{ |
146
|
17
|
|
|
17
|
|
52
|
my $self = shift; |
147
|
17
|
|
|
|
|
44
|
my $dir = shift; |
148
|
|
|
|
|
|
|
|
149
|
17
|
|
|
|
|
95
|
$dir =~ s/\/$//g; # Strip trailing slashes |
150
|
17
|
50
|
|
|
|
538
|
return 1 if -d $dir; |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
0
|
my $path; |
153
|
0
|
|
|
|
|
0
|
for (split(/\/+/,$dir)){ |
154
|
0
|
0
|
|
|
|
0
|
$path .= ((defined($path)?'/':'') . $_); |
155
|
0
|
0
|
0
|
|
|
0
|
(!length($path) || -d $path) && next; |
156
|
0
|
0
|
|
|
|
0
|
if(-e _) { |
157
|
0
|
|
|
|
|
0
|
print STDERR "DBR::Util::Logger: ERROR! $path exists, but is not a directory.\n"; |
158
|
|
|
|
|
|
|
return undef |
159
|
0
|
|
|
|
|
0
|
} |
160
|
0
|
0
|
0
|
|
|
0
|
mkdir($path, 0775 ) || print STDERR "DBR::Util::Logger: Failed to mkdir $path\n" && return undef; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
0
|
return 1; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=pod |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 logErr,logWarn, logInfo, logDebug, logDebug2, logDebug3 |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
wrappers around log |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=cut |
173
|
|
|
|
|
|
|
|
174
|
0
|
|
|
0
|
1
|
0
|
sub logErr { my $self = shift; $self->log( shift, shift, 'ERROR' ); } |
|
0
|
|
|
|
|
0
|
|
175
|
0
|
|
|
0
|
1
|
0
|
sub logWarn { my $self = shift; $self->log( shift, shift, 'WARN' ); } |
|
0
|
|
|
|
|
0
|
|
176
|
0
|
|
|
0
|
1
|
0
|
sub logInfo { my $self = shift; $self->log( shift, shift, 'INFO' ); } |
|
0
|
|
|
|
|
0
|
|
177
|
0
|
|
|
0
|
1
|
0
|
sub logDebug { my $self = shift; $self->log( shift, shift, 'DEBUG' ); } |
|
0
|
|
|
|
|
0
|
|
178
|
0
|
|
|
0
|
1
|
0
|
sub logDebug2 { my $self = shift; $self->log( shift, shift, 'DEBUG2' ); } |
|
0
|
|
|
|
|
0
|
|
179
|
0
|
|
|
0
|
1
|
0
|
sub logDebug3 { my $self = shift; $self->log( shift, shift, 'DEBUG3' ); } |
|
0
|
|
|
|
|
0
|
|
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub DESTROY{ |
183
|
16
|
|
|
16
|
|
60
|
my $self = shift; |
184
|
|
|
|
|
|
|
|
185
|
16
|
50
|
|
|
|
369
|
if(defined($self->{HANDLE})){ |
186
|
16
|
|
|
|
|
196
|
$self->{HANDLE}->close(); |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=pod |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head2 getTime |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
accepts null or unix time as input (if null, current time is assumed) |
195
|
|
|
|
|
|
|
returns an array like localtime, except that year is adjust to 4 digits and |
196
|
|
|
|
|
|
|
month is 1-12 instead of 0-11 |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=cut |
199
|
|
|
|
|
|
|
sub getTime { |
200
|
1586
|
|
|
1586
|
1
|
2758
|
my($time) = @_; |
201
|
1586
|
|
33
|
|
|
12780
|
$time ||= time; |
202
|
1586
|
|
|
|
|
146619
|
my(@time) = localtime($time); |
203
|
1586
|
|
|
|
|
4097
|
$time[4]++; |
204
|
1586
|
|
|
|
|
2200
|
my($i); |
205
|
1586
|
|
|
|
|
17041
|
for ($i=0;$i<=$#time;$i++) { |
206
|
14274
|
100
|
|
|
|
89033
|
if (length($time[$i])<2) { |
207
|
8072
|
|
|
|
|
30682
|
$time[$i] = "0$time[$i]"; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
1586
|
|
|
|
|
4432
|
$time[5] += 1900; |
212
|
|
|
|
|
|
|
|
213
|
1586
|
|
|
|
|
18099
|
return(@time); |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
1; |