File Coverage

blib/lib/Dancer2/Logger/LogReport.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Dancer2-Plugin-LogReport version 2.02.
2             # The POD got stripped from this file by OODoc version 3.05.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2015-2025 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11             #oodist: *** DO NOT USE THIS VERSION FOR PRODUCTION ***
12             #oodist: This file contains OODoc-style documentation which will get stripped
13             #oodist: during its release in the distribution. You can use this file for
14             #oodist: testing, however the code of this development version may be broken!
15              
16             package Dancer2::Logger::LogReport;{
17             our $VERSION = '2.02';
18             }
19              
20             # ABSTRACT: Dancer2 logger engine for Log::Report
21              
22 2     2   1839643 use strict;
  2         6  
  2         91  
23 2     2   12 use warnings;
  2         3  
  2         169  
24              
25 2     2   15 use Log::Report 'dancer2-plugin-logreport', syntax => 'REPORT';
  2         5  
  2         60  
26              
27 2     2   599 use Moo;
  2         4  
  2         18  
28 2     2   1103 use Dancer2::Core::Types;
  2         3  
  2         40  
29 2     2   30720 use Scalar::Util qw/blessed/;
  2         4  
  2         1250  
30              
31             our $AUTHORITY = 'cpan:MARKOV';
32              
33             my %level_dancer2lr = (
34             core => 'TRACE',
35             debug => 'TRACE',
36             );
37              
38             with 'Dancer2::Core::Role::Logger';
39              
40             # Set by calling function
41             has dispatchers => (is => 'ro', isa => Maybe[HashRef]);
42              
43             sub BUILD
44             { my $self = shift;
45             my $configs = $self->dispatchers || +{ default => undef };
46             $self->{use} = [ keys %$configs ];
47              
48             dispatcher 'do-not-reopen';
49              
50             foreach my $name (keys %$configs)
51             { my $config = $configs->{$name} || {};
52             if(keys %$config)
53             { my $type = delete $config->{type}
54             or die "dispatcher configuration $name without type"; # No LR yet
55              
56             dispatcher $type, $name, %$config;
57             }
58             }
59             }
60              
61             around 'error' => sub {
62             my ($orig, $self) = (shift, shift);
63              
64             # If it's a route exception (generated by Dancer) and we're also using the
65             # Plugin, then the plugin will handle the exception using its own hook into
66             # the error system. This should be able to removed in the future with
67             # https://github.com/PerlDancer/Dancer2/pull/1287
68             return if $_[0] =~ /^Route exception/ && $INC{'Dancer2/Plugin/LogReport.pm'};
69              
70             $self->log(error => @_);
71             };
72              
73             #--------------------
74              
75             sub log # no protoypes in Dancer2
76             { my ($self, $level, $msg) = @_;
77              
78             my %options;
79             # If only using the logger on its own (without the associated plugin), make
80             # it behave like a normal Dancer logger
81             unless($INC{'Dancer2/Plugin/LogReport.pm'})
82             { $msg = $self->format_message($level, $msg);
83             $options{is_fatal} = 0;
84             }
85              
86             # the levels are nearly the same.
87             my $reason = $level_dancer2lr{$level} || uc $level;
88              
89             report \%options, $reason => $msg;
90             undef;
91             }
92              
93             1;