File Coverage

blib/lib/Log/Dispatch/Code.pm
Criterion Covered Total %
statement 24 24 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 31 33 93.9


line stmt bran cond sub pod time code
1             package Log::Dispatch::Code;
2              
3 2     2   3197 use strict;
  2         4  
  2         123  
4 2     2   16 use warnings;
  2         6  
  2         107  
5              
6             our $VERSION = '2.70';
7              
8 2     2   15 use Log::Dispatch::Types;
  2         4  
  2         25  
9 2     2   59128 use Params::ValidationCompiler qw( validation_for );
  2         4  
  2         156  
10              
11 2     2   15 use base qw( Log::Dispatch::Output );
  2         5  
  2         953  
12              
13             {
14             my $validator = validation_for(
15             params => { code => { type => t('CodeRef') } },
16             slurpy => 1,
17             );
18              
19             sub new {
20 1     1 0 3 my $class = shift;
21              
22 1         24 my %p = $validator->(@_);
23              
24 1         21 my $self = bless { code => delete $p{code} }, $class;
25 1         10 $self->_basic_init(%p);
26              
27 1         5 return $self;
28             }
29             }
30              
31             sub log_message {
32 2     2 0 4 my $self = shift;
33 2         5 my %p = @_;
34              
35 2         3 delete $p{name};
36              
37 2         8 $self->{code}->(%p);
38             }
39              
40             1;
41              
42             # ABSTRACT: Object for logging to a subroutine reference
43              
44             __END__
45              
46             =pod
47              
48             =encoding UTF-8
49              
50             =head1 NAME
51              
52             Log::Dispatch::Code - Object for logging to a subroutine reference
53              
54             =head1 VERSION
55              
56             version 2.70
57              
58             =head1 SYNOPSIS
59              
60             use Log::Dispatch;
61              
62             my $log = Log::Dispatch->new(
63             outputs => [
64             [
65             'Code',
66             min_level => 'emerg',
67             code => \&_log_it,
68             ],
69             ]
70             );
71              
72             sub _log_it {
73             my %p = @_;
74              
75             warn $p{message};
76             }
77              
78             =head1 DESCRIPTION
79              
80             This module supplies a simple object for logging to a subroutine reference.
81              
82             =for Pod::Coverage new log_message
83              
84             =head1 CONSTRUCTOR
85              
86             The constructor takes the following parameters in addition to the standard
87             parameters documented in L<Log::Dispatch::Output>:
88              
89             =over 4
90              
91             =item * code ($)
92              
93             The subroutine reference.
94              
95             =back
96              
97             =head1 HOW IT WORKS
98              
99             The subroutine you provide will be called with a hash of named arguments. The
100             two arguments are:
101              
102             =over 4
103              
104             =item * level
105              
106             The log level of the message. This will be a string like "info" or "error".
107              
108             =item * message
109              
110             The message being logged.
111              
112             =back
113              
114             =head1 SUPPORT
115              
116             Bugs may be submitted at L<https://github.com/houseabsolute/Log-Dispatch/issues>.
117              
118             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
119              
120             =head1 SOURCE
121              
122             The source code repository for Log-Dispatch can be found at L<https://github.com/houseabsolute/Log-Dispatch>.
123              
124             =head1 AUTHOR
125              
126             Dave Rolsky <autarch@urth.org>
127              
128             =head1 COPYRIGHT AND LICENSE
129              
130             This software is Copyright (c) 2020 by Dave Rolsky.
131              
132             This is free software, licensed under:
133              
134             The Artistic License 2.0 (GPL Compatible)
135              
136             The full text of the license can be found in the
137             F<LICENSE> file included with this distribution.
138              
139             =cut