File Coverage

blib/lib/Log/ger/Output/LogDispatchOutput.pm
Criterion Covered Total %
statement 26 26 100.0
branch 5 8 62.5
condition 2 6 33.3
subroutine 6 6 100.0
pod 0 2 0.0
total 39 48 81.2


line stmt bran cond sub pod time code
1             package Log::ger::Output::LogDispatchOutput;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2020-03-11'; # DATE
5             our $DIST = 'Log-ger-Output-LogDispatchOutput'; # DIST
6             our $VERSION = '0.006'; # VERSION
7              
8 1     1   1780 use strict;
  1         3  
  1         26  
9 1     1   4 use warnings;
  1         2  
  1         22  
10              
11 1     1   4 use Log::ger::Util;
  1         2  
  1         279  
12              
13             sub meta { +{
14 1     1 0 16 v => 2,
15             } }
16              
17             sub get_hooks {
18 1     1 0 14 my %plugin_conf = @_;
19              
20 1 50       3 $plugin_conf{output} or die "Please specify output (e.g. ".
21             "ArrayWithLimits for Log::Dispatch::ArrayWithLimits)";
22              
23 1         473 require Log::Dispatch;
24 1         219408 my $mod = "Log::Dispatch::$plugin_conf{output}";
25 1         6 (my $mod_pm = "$mod.pm") =~ s!::!/!g;
26 1         422 require $mod_pm;
27              
28             return {
29             create_outputter => [
30             __PACKAGE__, # key
31             # we want to handle all levels, thus we need to be higher priority
32             # than default Log::ger hooks (10) which will install null loggers
33             # for less severe levels.
34             9, # priority
35             sub { # hook
36 12     12   1745 my %hook_args = @_; # see Log::ger::Manual::Internals/"Arguments passed to hook"
37              
38             my $outputter = sub {
39 3         649 my ($per_target_conf, $msg, $per_msg_conf) = @_;
40 3   33     14 my $level = $per_msg_conf->{level} // $hook_args{level};
41              
42 3 100       9 return if $level > $Log::ger::Current_Level;
43              
44             # we can use per-target conf to store per-target stuffs
45             $hook_args{per_target_conf}{_ld} ||= Log::Dispatch->new(
46             outputs => [
47             $plugin_conf{_output} ? $plugin_conf{_output} :
48             [
49             $plugin_conf{output},
50             min_level => 'warning',
51 2 50 33     10 %{ $plugin_conf{args} || {} },
  2 50       14  
52             ],
53             ],
54             );
55 2         460 $hook_args{per_target_conf}{_ld}->warning($msg);
56 12         41 };
57 12         28 [$outputter];
58 1         30148 }],
59             };
60             }
61              
62             1;
63             # ABSTRACT: Send logs to a Log::Dispatch output
64              
65             __END__
66              
67             =pod
68              
69             =encoding UTF-8
70              
71             =head1 NAME
72              
73             Log::ger::Output::LogDispatchOutput - Send logs to a Log::Dispatch output
74              
75             =head1 VERSION
76              
77             This document describes version 0.006 of Log::ger::Output::LogDispatchOutput (from Perl distribution Log-ger-Output-LogDispatchOutput), released on 2020-03-11.
78              
79             =head1 SYNOPSIS
80              
81             use Log::ger::Output LogDispatchOutput => (
82             output => 'Screen', # choose Log::Dispatch::Screen
83             args => {stderr=>1, newline=>1},
84             );
85              
86             =head1 DESCRIPTION
87              
88             This output sends logs to a Log::Dispatch output.
89              
90             =for Pod::Coverage ^(.+)$
91              
92             =head1 CONFIGURATION
93              
94             =head2 output
95              
96             =head2 args
97              
98             =head1 HOMEPAGE
99              
100             Please visit the project's homepage at L<https://metacpan.org/release/Log-ger-Output-LogDispatchOutput>.
101              
102             =head1 SOURCE
103              
104             Source repository is at L<https://github.com/perlancar/perl-Log-ger-Output-LogDispatchOutput>.
105              
106             =head1 BUGS
107              
108             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Log-ger-Output-LogDispatchOutput>
109              
110             When submitting a bug or request, please include a test-file or a
111             patch to an existing test-file that illustrates the bug or desired
112             feature.
113              
114             =head1 SEE ALSO
115              
116             L<Log::ger::Output::LogDispatch>
117              
118             L<Log::ger>
119              
120             L<Log::Dispatch>
121              
122             =head1 AUTHOR
123              
124             perlancar <perlancar@cpan.org>
125              
126             =head1 COPYRIGHT AND LICENSE
127              
128             This software is copyright (c) 2020, 2017 by perlancar@cpan.org.
129              
130             This is free software; you can redistribute it and/or modify it under
131             the same terms as the Perl 5 programming language system itself.
132              
133             =cut