File Coverage

blib/lib/Log/LogMethods/Log4perlLogToString.pm
Criterion Covered Total %
statement 33 33 100.0
branch 1 2 50.0
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 45 46 97.8


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   566432 use Modern::Perl;
  3         18  
  3         25  
36 3     3   487 use Exporter qw(import);
  3         7  
  3         74  
37 3     3   1009 use IO::Scalar;
  3         9566  
  3         124  
38 3     3   1355 use Log::LogMethods;
  3         11  
  3         108  
39 3     3   21 use Log::Log4perl::Appender;
  3         7  
  3         66  
40 3     3   15 use Log::Log4perl::Layout::PatternLayout;
  3         6  
  3         65  
41 3     3   1447 use Log::Dispatch::Handle;
  3         602152  
  3         830  
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 755 my ($class,$string,$format)=@_;
66 6         47 my $fh=IO::Scalar->new(\$_[1]);
67              
68 6         404 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 21 my ($class,$fh,$format)=@_;
79 6 50       21 $format=$DEFAULT_LAYOUT unless defined($format);
80            
81 6         61 my $layout = Log::Log4perl::Layout::PatternLayout->new($format);
82 6         4717 my $appender=Log::Log4perl::Appender->new(
83             'Log::Dispatch::Handle',
84             handle=>$fh
85             );
86              
87 6         1534 $appender->layout($layout);
88 6         97 my $log=Log::Log4perl->get_logger($class);
89 6         1990 $log->add_appender($appender);
90 6         6395 $log->level($Log::LogMethods::LEVEL_MAP{DEBUG});
91 6         5974 return $log;
92             }
93              
94             =back
95              
96             =head1 AUTHOR
97              
98             Mike Shipper <AKALINUX@CPAN.ORG>
99              
100             =cut
101              
102             1;