line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::Any::Adapter::FileHandle; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Log::Any::Adapter::FileHandle::VERSION = '0.008'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Log::Any::Adapter::FileHandle - A basic Log::Any::Adapter to forward messages to a filehandle |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
version 0.008 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use Log::Any qw($log); |
17
|
|
|
|
|
|
|
use Log::Any::Adapter; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Send all logs to Log::Any::Adapter::FileHandle |
20
|
|
|
|
|
|
|
Log::Any::Adapter->set('FileHandle'); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$log->info("Hello world"); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 DESCRIPTION |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
This module is a basic adapter that will simply forward log messages to a filehandle, or any object that |
27
|
|
|
|
|
|
|
supports a 'print' method (L, L, Plack's $env->{psgi.errors} object, etc). |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
I've created it so that my scripts running under damontools or runit can output |
30
|
|
|
|
|
|
|
messages to locally defined logs. It does not timestamp messages, that responsbility is |
31
|
|
|
|
|
|
|
delegated to the external log capture mechanism. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
You can override the default configuration by passing extra arguments to the |
34
|
|
|
|
|
|
|
C set_adapter method: |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=over |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=item fh |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Pass in your IO::Handle-like object here. If this isn't specified, it will default to opening STDERR. |
41
|
|
|
|
|
|
|
If the object supports an autoflush method, autoflush will be enabled, unless no_autoflush is set. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=item no_autoflush |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Disable automatically turning on autoflush on the fh object. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=item format |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
A sprintf string that controls the formatting of the message. It is supplied 2 |
50
|
|
|
|
|
|
|
arguments: the log level as supplied by Log::Any (usually all-lowercase), and |
51
|
|
|
|
|
|
|
the message to be logged. The default is "[%s] %s\n". This value should contain the log |
52
|
|
|
|
|
|
|
record terminator, such as a newline. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=back |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Copyright 2010 by Jason Jay Rodrigues |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Log::Any::Adapter::FileHandle is provided "as is" and without any express or |
62
|
|
|
|
|
|
|
implied warranties, including, without limitation, the implied warranties of |
63
|
|
|
|
|
|
|
merchantibility and fitness for a particular purpose. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under |
66
|
|
|
|
|
|
|
the same terms as Perl itself. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=cut |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
3
|
|
|
3
|
|
92354
|
use strict; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
155
|
|
72
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
107
|
|
73
|
3
|
|
|
3
|
|
1071
|
use Log::Any::Adapter::Util qw(make_method); |
|
3
|
|
|
|
|
8552
|
|
|
3
|
|
|
|
|
353
|
|
74
|
3
|
|
|
3
|
|
22
|
use Scalar::Util qw(blessed); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
221
|
|
75
|
3
|
|
|
3
|
|
18
|
use IO::Handle; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
132
|
|
76
|
3
|
|
|
3
|
|
16
|
use base qw(Log::Any::Adapter::Base); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
3645
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub init { |
79
|
11
|
|
|
11
|
0
|
20213
|
my ($self, %attr) = @_; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# if no fh object is set, we default to STDERR. |
82
|
11
|
50
|
|
|
|
71
|
if(!exists($self->{fh})) { |
83
|
0
|
|
|
|
|
0
|
$self->{fh} = IO::Handle->new_from_fd(fileno(STDERR),'w'); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
11
|
100
|
100
|
|
|
209
|
if($self->{fh}->can('autoflush') && !$self->{no_autoflush}) { |
87
|
6
|
|
|
|
|
36
|
$self->{fh}->autoflush(1); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# if no format is set, we default to a reasonable sane default. |
91
|
11
|
100
|
|
|
|
142
|
if(!exists($self->{format})) { |
92
|
9
|
|
|
|
|
26
|
$self->{format} = "[%s] %s\n"; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
11
|
100
|
|
|
|
41
|
if(!exists($self->{escape})) { |
96
|
9
|
|
|
|
|
39
|
$self->{escape} = 'none'; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
{ |
102
|
|
|
|
|
|
|
# setup logging methods, that simply print to the given io object. |
103
|
|
|
|
|
|
|
foreach my $method ( Log::Any->logging_methods() ) { |
104
|
|
|
|
|
|
|
my $logger = sub { |
105
|
22
|
|
|
22
|
|
5446
|
my $self = shift; |
106
|
22
|
|
|
|
|
54
|
my $message = join('',@_); |
107
|
22
|
100
|
100
|
|
|
127
|
if($self->{escape} eq 'newline' || $self->{escape} eq 'nonascii') { |
108
|
5
|
|
|
|
|
18
|
$message =~ s/\n/\\n/sg; |
109
|
5
|
|
|
|
|
11
|
$message =~ s/\r/\\r/sg; |
110
|
|
|
|
|
|
|
} |
111
|
22
|
100
|
|
|
|
56
|
if($self->{escape} eq 'nonascii') { |
112
|
3
|
|
|
3
|
|
19747
|
$message =~ s/(\P{ASCII}|\p{PosixCntrl})/sprintf("\\x{%x}",ord($1))/eg; |
|
3
|
|
|
|
|
27
|
|
|
3
|
|
|
|
|
48
|
|
|
3
|
|
|
|
|
22
|
|
|
4
|
|
|
|
|
20
|
|
113
|
|
|
|
|
|
|
} |
114
|
22
|
50
|
|
|
|
73
|
if($self->{fh}) { |
115
|
22
|
|
|
|
|
163
|
$self->{fh}->print(sprintf($self->{format}, $method, $message)); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
}; |
118
|
|
|
|
|
|
|
make_method($method, $logger); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
9
|
|
|
9
|
|
3246
|
my $true = sub { 1 }; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# In FileHandle, we log *everything*, and let the |
124
|
|
|
|
|
|
|
# log seperation happen in external programs. |
125
|
|
|
|
|
|
|
foreach my $method ( Log::Any->detection_methods() ) { |
126
|
|
|
|
|
|
|
make_method($method, $true); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
1; |