Codebase list liblog-report-perl / v0.91
distribution Log-Report-0.91.tar.gz Mark Overmeer authored 13 years ago Mark Overmeer committed 6 years ago
9 changed file(s) with 125 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
00
11 ==== version history of Log::Report
2
3 version 0.91: Wed Jan 26 16:24:25 CET 2011
4
5 Fixes:
6
7 - enabling and disabling dispatchers did not work
8 [Patrick Powell]
9
10 Improvements:
11
12 - produce nice error when __x received even length list.
13
14 - added Log::Report::Dispatcher::Callback
15
16 - typos in new Callback.pm [Patrick Powell]
17
18 - disable test which fails on bug in confess on Windows
19 http://rt.perl.org/rt3/Ticket/Display.html?id=81586
20
21 - improved output with new OODoc
222
323 version 0.90: Wed Dec 22 16:29:51 CET 2010
424
55 lib/Log/Report.pm
66 lib/Log/Report/Die.pm
77 lib/Log/Report/Dispatcher.pm
8 lib/Log/Report/Dispatcher/Callback.pm
89 lib/Log/Report/Dispatcher/File.pm
910 lib/Log/Report/Dispatcher/Log4perl.pm
1011 lib/Log/Report/Dispatcher/LogDispatch.pm
11
22 use 5.008;
33
4 my $version = '0.90';
4 my $version = '0.91';
55
66 my %prereq =
77 ( Test::More => 0.86
0 use warnings;
1 use strict;
2
3 package Log::Report::Dispatcher::Callback;
4 use base 'Log::Report::Dispatcher';
5
6 use Log::Report 'log-report';
7
8 =chapter NAME
9 Log::Report::Dispatcher::Callback - call a code-ref for each log-line
10
11 =chapter SYNOPSIS
12 sub cb($$$)
13 { my ($options, $reason, $message) = @_;
14 ...
15 }
16
17 dispatcher Log::Report::Dispatcher::Callback => 'cb'
18 , callback => \&cb;
19
20 dispatcher CALLBACK => 'cb' # same
21 , callback => \&cb;
22
23 =chapter DESCRIPTION
24 This basic file logger accepts a callback, which is called for each
25 message which is to be logged. When you need complex things, you
26 may best make your own extension to M<Log::Report::Dispatcher>, but
27 for simple things this will do.
28
29 =example
30 sub send_mail($$$)
31 { my ($disp, $options, $reason, $message) = @_;
32 my $msg = Mail::Send->new(Subject => $reason
33 , To => 'admin@localhost');
34 my $fh = $msg->open('sendmail');
35 print $fh $disp->translate($reason, $message);
36 close $fh;
37 }
38
39 dispatcher CALLBACK => 'mail', callback => \&send_mail;
40
41 =chapter METHODS
42
43 =section Constructors
44
45 =c_method new TYPE, NAME, OPTIONS
46
47 =requires callback CODE
48 Your C<callback> is called with four parameters: this dispatcher object,
49 the options, a reason and a message. The C<options> are the first
50 parameter of M<Log::Report::report()> (read over there). The C<reason>
51 is a capitized string like C<ERROR>. Finally, the C<message> is a
52 M<Log::Report::Message>.
53
54 =cut
55
56 sub init($)
57 { my ($self, $args) = @_;
58 $self->SUPER::init($args);
59
60 $self->{callback} = $args->{callback}
61 or error __x"dispatcher {name} needs a 'callback'", name => $self->name;
62
63 $self;
64 }
65
66 =section Accessors
67
68 =method callback
69 Returns the code reference which will handle each logged message.
70 =cut
71
72 sub callback() {shift->{callback}}
73
74 =section Logging
75 =cut
76
77 sub log($$$)
78 { my $self = shift;
79 $self->{callback}->($self, @_);
80 }
81
82 1;
2121 my @default_accept = ('NOTICE-', 'INFO-', 'ASSERT-', 'ALL');
2222
2323 my %predef_dispatchers = map { (uc($_) => __PACKAGE__.'::'.$_) }
24 qw/File Perl Syslog Try/;
24 qw/File Perl Syslog Try Callback/;
2525
2626 =chapter NAME
2727 Log::Report::Dispatcher - manage dispatching
219219
220220 # only to be called from Log::Report::dispatcher()!!
221221 # because requires re-investigating needs
222 sub _disable($)
222 sub _disabled($)
223223 { my $self = shift;
224224 @_ ? ($self->{disabled} = shift) : $self->{disabled};
225225 }
100100 Alternative for C<_class>, which cannot be used at the same time.
101101 =cut
102102
103 sub new($@)
104 { my ($class, %args) = @_;
105 bless \%args, $class;
106 }
103 sub new($@) { my $class = shift; bless {@_}, $class }
107104
108105 =method clone OPTIONS, VARIABLES
109106 Returns a new object which copies info from original, and updates it
434434
435435 # _whats_needed
436436 # Investigate from all dispatchers which reasons will need to be
437 # passed on. After dispatchers are added, enabled, or disabled,
437 # passed on. After dispatchers are added, enabled, or disabled,
438438 # this method shall be called to re-investigate the back-ends.
439439
440440 sub _whats_needed()
632632
633633 # label "msgid" added before first argument
634634 sub __x($@)
635 { Log::Report::Message->new
635 { @_%2 or error __x"even length parameter list for __x at {where}",
636 where => join(' line ', (caller)[1,2]);
637
638 Log::Report::Message->new
636639 ( _msgid => @_
637640 , _expand => 1
638641 , _domain => _default_domain(caller)
22 use strict;
33 use lib 'lib', '../lib';
44
5 use Test::More tests => 15;
5 use Test::More tests => 16;
66
77 # The versions of the following packages are reported to help understanding
88 # the environment in which the tests are run. This is certainly not a
3434 use_ok('Log::Report::Dispatcher::File');
3535 use_ok('Log::Report::Dispatcher::Try');
3636 use_ok('Log::Report::Dispatcher::Perl');
37 use_ok('Log::Report::Dispatcher::Callback');
3738 use_ok('Log::Report::Exception');
3839 use_ok('Log::Report::Lexicon::Index');
3940 use_ok('Log::Report::Lexicon::PO');
144144 main::simple_wrapper()#t/41die.t#XX
145145 __OUT
146146
147
148 if($^O eq 'Win32')
149 { # perl bug http://rt.perl.org/rt3/Ticket/Display.html?id=81586
150 pass 'Win32/confess bug #81586';
151 }
152 else
153 {
154
147155 eval { $! = $errno; confess "ouch $!\n" };
148156 my $confess_text4 = $@;
149157 is(process($confess_text4), <<__OUT, "confess");
157165
158166 }
159167
168 } # run_tests()
169
160170 1;