line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::LogMethods::Log4perlLogToString; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Log::LogMethods::Log4perlLogToString - Easy way to validate Logging to Log4Perl |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Moder::Perl; |
10
|
|
|
|
|
|
|
use Log::LogMethods::Log4perlLogToString; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my $string=''; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $class; |
15
|
|
|
|
|
|
|
my $string=''; |
16
|
|
|
|
|
|
|
my $log=LoggerToString($class,$string); |
17
|
|
|
|
|
|
|
$log->info("something to log"); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
print $string; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 DESCRIPTION |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
Created as a way to save time writting unit tests, Log::LogMethods::Log4perlLogToString does all the dirty work of creating a logger that writes to a string or File handle.. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=cut |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 Exports |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
All functions and variables are exported by default, if you only want to import a single funciton, just provide the statement in the use list. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=over 4 |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=cut |
34
|
|
|
|
|
|
|
|
35
|
3
|
|
|
3
|
|
598493
|
use Modern::Perl; |
|
3
|
|
|
|
|
25
|
|
|
3
|
|
|
|
|
26
|
|
36
|
3
|
|
|
3
|
|
509
|
use Exporter qw(import); |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
81
|
|
37
|
3
|
|
|
3
|
|
1040
|
use IO::Scalar; |
|
3
|
|
|
|
|
11938
|
|
|
3
|
|
|
|
|
139
|
|
38
|
3
|
|
|
3
|
|
1448
|
use Log::LogMethods; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
111
|
|
39
|
3
|
|
|
3
|
|
21
|
use Log::Log4perl::Appender; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
120
|
|
40
|
3
|
|
|
3
|
|
19
|
use Log::Log4perl::Layout::PatternLayout; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
71
|
|
41
|
3
|
|
|
3
|
|
1414
|
use Log::Dispatch::Handle; |
|
3
|
|
|
|
|
643986
|
|
|
3
|
|
|
|
|
911
|
|
42
|
|
|
|
|
|
|
Log::Log4perl->wrapper_register(__PACKAGE__); |
43
|
|
|
|
|
|
|
our $VERSION=$Log::LogMethods::VERSION; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
our @EXPORT=qw(LoggerToString LoggerToFh $DEFAULT_LAYOUT); |
46
|
|
|
|
|
|
|
our @EXPORT_OK=@EXPORT; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=item * $DEFAULT_LAYOUT |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
The default Log::Log4perl::Layout::PatternLayout. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
%H %P %d %p %k %S [%h] [%s] %b %j %B%n |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=cut |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
our $DEFAULT_LAYOUT='%H %P %d %p %k %S [%h] [%s] %b %j %B%n'; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=item * my $log=LoggerToString($class,$string,$format); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$log is a loger object created for $class. If $format is empty then $DEFAULT_FORMAT is used. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=cut |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub LoggerToString { |
65
|
6
|
|
|
6
|
1
|
832
|
my ($class,$string,$format)=@_; |
66
|
6
|
|
|
|
|
39
|
my $fh=IO::Scalar->new(\$_[1]); |
67
|
|
|
|
|
|
|
|
68
|
6
|
|
|
|
|
391
|
return LoggerToFh($class,$fh,$format); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item * my $log=LoggerToFh($class,$fh,$format); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Really the guts of this class, it creates a logger that writes to $fh. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=cut |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub LoggerToFh { |
78
|
6
|
|
|
6
|
1
|
18
|
my ($class,$fh,$format)=@_; |
79
|
6
|
50
|
|
|
|
57
|
$format=$DEFAULT_LAYOUT unless defined($format); |
80
|
|
|
|
|
|
|
|
81
|
6
|
|
|
|
|
54
|
my $layout = Log::Log4perl::Layout::PatternLayout->new($format); |
82
|
6
|
|
|
|
|
4663
|
my $appender=Log::Log4perl::Appender->new( |
83
|
|
|
|
|
|
|
'Log::Dispatch::Handle', |
84
|
|
|
|
|
|
|
handle=>$fh |
85
|
|
|
|
|
|
|
); |
86
|
|
|
|
|
|
|
|
87
|
6
|
|
|
|
|
1499
|
$appender->layout($layout); |
88
|
6
|
|
|
|
|
87
|
my $log=Log::Log4perl->get_logger($class); |
89
|
6
|
|
|
|
|
1985
|
$log->add_appender($appender); |
90
|
6
|
|
|
|
|
6638
|
$log->level($Log::LogMethods::LEVEL_MAP{DEBUG}); |
91
|
6
|
|
|
|
|
6572
|
return $log; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=back |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 AUTHOR |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Mike Shipper <AKALINUX@CPAN.ORG> |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |