blib/lib/CGI/Builder/LogDispatch.pm untested

filter: Statementstmt 0.0% 0 / 40 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 56 CC 2 SCAR 10.6
CC
2
Cov
0%
logger_new
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.9
1 package CGI::Builder::LogDispatch;
2  
3 $VERSION = 0.1;
4  
5 use strict;
6 use Log::Dispatch 2.0 ();
7 use Date::Format ();
8  
9 $Carp::Internal{ 'Log::Dispatch' }++;
10 $Carp::Internal{+__PACKAGE__}++;
11  
12 use Class::groups(
13 { name => 'logger_config',
14 default => { }
15 } ,
16 );
17  
18 use Class::props(
19 { name => 'logger',
20 default => sub { shift()->logger_new( @_ ) }
21 } ,
22 );
23  
24 # Note name and min_level are required params to new(), so we hard-code them
25 # but allow overrides from the user configuration
26 sub logger_new {
27 my ($self) = @_;
28 my %defaults;
29 my $logger = Log::Dispatch->new();
30 if ( $self->can('r') and $self->r->can('log') ) {
31 require Log::Dispatch::ApacheLog;
32 %defaults = (name=>'default', min_level=>'warning',apache=>$self->r);
33 $logger->add(
34 Log::Dispatch::ApacheLog->new( %defaults, %{$self->logger_config} )
35 );
36 } else {
37 require Log::Dispatch::Screen;
38 %defaults = ( name=>'default'
39 , min_level=>'warning'
40 , callbacks => sub { sprintf "[%s] %s\n", Date::Format::time2str('%Y-%m-%d %H:%M:%S',time), $_[1] }
41 );
42 $logger->add(
43 Log::Dispatch::Screen->new( %defaults, %{$self->logger_config} )
44 );
45 }
46 return $logger;
47 }
48  
49  
50 1;
51  
52 __END__
53  
54 =head1 NAME
55  
56 CGI::Builder::LogDispatch - integrated logging system for CGI::Builder
57  
58 =head1 VERSION 0.01
59  
60 =head1 INSTALLATION
61  
62 =over
63  
64 =item Prerequisites
65  
66 CGI::Builder >= 1.12
67 Log::Dispatch >= 2.0
68  
69 =back
70  
71 =head1 SYNOPSIS
72  
73 # just include it in your build
74 use CGI::Builder
75 qw| CGI::Builder::LogDispatch
76 |;
77
78 # Logger can write to different "Log Levels". The default level is
79 # "warning". Messages of lesser importance will not be written. When
80 # developing, you probably want this cranked to "debug":
81 sub OH_init {
82 my ($webapp) = @_;
83 # This MUST be done before calling $webapp->logger or it will have no effect!
84 $webapp->logger_config('min_level' => 'debug');
85 }
86
87 # Then use it to write nicely formatted messages to your web server log
88 sub PH_AUTOLOAD {
89 my ($webapp) = @_;
90 # The default min_level
91 $webapp->logger->debug("This message only gets logged if you set min_level=>debug.");
92 if ( $webapp->page_error ) {
93 $webapp->logger->error("Oh no! There is a problem!");
94 }
95 }
96  
97 =head1 DESCRIPTION
98  
99 The module should do what you want with no prodding. Just include it and start
100 using the logger property to log things. If you are using Apache::CGI::Builder
101 and Apache::Log, it will automatically use Apache's native log mechanism.
102 Otherwise it prints log messages to STDERR (which goes to the web server error
103 log), prepending them with a timestamp.
104  
105 If you want to do anything fancier than this you can override the logger_new
106 method in your build to construct the object any way you like. See
107 L<Log::Dispatch> for details.
108  
109 =head1 SUPPORT
110  
111 Support is provided via the CGI::Builder Users mailing list. You can join the CBF
112 mailing list at this url:
113  
114 http://lists.sourceforge.net/lists/listinfo/cgi-builder-users
115  
116 =head1 ACKNOWLEDGMENTS
117  
118 Many thanks to Domizio Demichelis, author of the CGI::Builder framework,
119 and Dave Rolsky, author of Log::Dispatch.
120  
121 =head1 AUTHORS
122  
123 Vince Veselosky (L<http://control-escape.com>)
124  
125 =head1 COPYRIGHT
126  
127 (c) 2005 by Vincent Veselosky
128  
129 This module is free software. It may be used,
130 redistributed and/or modified under the same terms as perl itself.