-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.pm
177 lines (156 loc) · 3.09 KB
/
Logger.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package Perluim::Logger;
use strict;
use warnings;
use File::Copy;
use Nimbus::API;
use IO::Handle;
use Scalar::Util qw(looks_like_number);
# LOGLEVEL Header
our %loglevel = (
0 => "[CRITICAL]",
1 => "[ERROR] ",
2 => "[WARNING] ",
3 => "[INFO] ",
4 => "[DEBUG] ",
5 => " "
);
sub new {
my ($class,$opt) = @_;
my $this = {
file => $opt->{file},
level => defined $opt->{level} ? $opt->{level} : 3,
size => defined $opt->{size} ? $opt->{size} * 1024 : 0,
_header => "",
closed => 0,
startTime => time()
};
my $blessed = bless($this,ref($class) || $class);
nimLogSet($blessed->{file},"",$blessed->{level});
nimLogTruncateSize($blessed->{size}) if $blessed->{size} != 0;
return $blessed;
}
#
# set log level
#
sub setLevel {
my ($self,$level) = @_;
nimLogSetLevel($level) if defined $level && looks_like_number($level);
}
#
# set log size.
#
sub setSize {
my ($self,$size) = @_;
nimLogTruncateSize($size * 1024) if defined $size;
}
#
# set log header (log prefix)
#
sub setHeader {
my ($self,$headerStr,$reset) = @_;
return if !defined $headerStr;
$reset = defined $reset ? $reset : 1;
if($reset) {
$self->{_header} = $headerStr;
return;
}
$self->{_header} .= $headerStr;
}
#
# reset header
#
sub resetHeader {
my ($self) = @_;
$self->{_header} = "";
}
#
# get a formatted date (on system datetime).
#
sub getFormattedDate {
my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $timetwoDigits = sprintf("%02d %02d:%02d:%02d",$mday,$hour,$min,$sec);
return "$months[$mon] $timetwoDigits";
}
#
# Trace log from another emitter!
#
sub trace {
my($self,$emitter) = @_;
return if !defined $emitter;
$emitter->on(log => sub {
$self->info(shift);
});
}
#
# Catch error from another emitter!
#
sub catch {
my($self,$emitter) = @_;
return if !defined $emitter;
$emitter->on(error => sub {
$self->error(shift);
});
}
#
# Truncate log file!
#
sub truncate {
my ($self) = @_;
nimLogTruncate();
}
#
# close log!
#
sub close {
my ($self) = @_;
$self->{closed} = 1;
nimLogClose();
}
#
# copyTo a new destination!
#
sub copyTo {
my ($self,$path) = @_;
return if !defined $path;
$self->close();
copy("$self->{file}","$path/$self->{file}") or warn "Failed to copy logfile $self->{file} to $path!";
}
#
# All logs routines
#
sub log {
my ($self,$level,$msg) = @_;
return if $self->{closed};
if(!defined($level)) {
$level = 3;
}
my $date = getFormattedDate();
my $header = $self->{_header};
nimLog($level,"$date $loglevel{$level} - ${header}${msg}");
}
sub fatal {
my ($self,$msg) = @_;
$self->log(0,$msg);
}
sub error {
my ($self,$msg) = @_;
$self->log(1,$msg);
}
sub warn {
my ($self,$msg) = @_;
$self->log(2,$msg);
}
sub info {
my ($self,$msg) = @_;
$self->log(3,$msg);
}
sub debug {
my ($self,$msg) = @_;
$self->log(4,$msg);
}
sub nolevel {
my ($self,$msg) = @_;
$self->log(5,$msg);
}