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   689591 use Modern::Perl;
  3         17  
  3         24  
36 3     3   483 use Exporter qw(import);
  3         6  
  3         86  
37 3     3   1041 use IO::Scalar;
  3         8057  
  3         161  
38 3     3   1381 use Log::LogMethods;
  3         11  
  3         122  
39 3     3   30 use Log::Log4perl::Appender;
  3         7  
  3         74  
40 3     3   21 use Log::Log4perl::Layout::PatternLayout;
  3         5  
  3         79  
41 3     3   1644 use Log::Dispatch::Handle;
  3         605348  
  3         946  
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 732 my ($class,$string,$format)=@_;
66 6         49 my $fh=IO::Scalar->new(\$_[1]);
67              
68 6         409 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 20 my ($class,$fh,$format)=@_;
79 6 50       22 $format=$DEFAULT_LAYOUT unless defined($format);
80            
81 6         60 my $layout = Log::Log4perl::Layout::PatternLayout->new($format);
82 6         4243 my $appender=Log::Log4perl::Appender->new(
83             'Log::Dispatch::Handle',
84             min_level=>'info',
85             handle=>$fh
86             );
87              
88 6         1420 $appender->layout($layout);
89 6         84 my $log=Log::Log4perl->get_logger($class);
90 6         1969 $log->add_appender($appender);
91 6         6669 $log->level($Log::LogMethods::LEVEL_MAP{DEBUG});
92 6         6456 return $log;
93             }
94              
95             =back
96              
97             =head1 AUTHOR
98              
99             Mike Shipper <AKALINUX@CPAN.ORG>
100              
101             =cut
102              
103             1;