| 1 | # This code is part of Perl distribution Log-Report version 1.46. | ||
| 2 | # The POD got stripped from this file by OODoc version 3.06. | ||
| 3 | # For contributors see file ChangeLog. | ||
| 4 | |||
| 5 | # This software is copyright (c) 2007-2026 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 | |||
| 12 | package Log::Report::Dispatcher::Log4perl;{ | ||
| 13 | our $VERSION = '1.46'; | ||
| 14 | } | ||
| 15 | |||
| 16 | use base 'Log::Report::Dispatcher'; | ||
| 17 | |||
| 18 | use warnings; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | use Log::Report 'log-report', import => [ qw/__ __x error panic/ ]; | ||
| 22 | |||
| 23 | use Log::Report::Util qw/@reasons expand_reasons/; | ||
| 24 | use Log::Log4perl qw/:levels/; | ||
| 25 | |||
| 26 | my %default_reasonToLevel = ( | ||
| 27 | TRACE => $DEBUG, | ||
| 28 | ASSERT => $DEBUG, | ||
| 29 | INFO => $INFO, | ||
| 30 | NOTICE => $INFO, | ||
| 31 | WARNING => $WARN, | ||
| 32 | MISTAKE => $WARN, | ||
| 33 | ERROR => $ERROR, | ||
| 34 | FAULT => $ERROR, | ||
| 35 | ALERT => $FATAL, | ||
| 36 | FAILURE => $FATAL, | ||
| 37 | PANIC => $FATAL, | ||
| 38 | ); | ||
| 39 | |||
| 40 | @reasons==keys %default_reasonToLevel | ||
| 41 | or panic __"Not all reasons have a default translation"; | ||
| 42 | |||
| 43 | # Do not show these as source of the error: one or more caller frames up | ||
| 44 | Log::Log4perl->wrapper_register($_) for qw/ | ||
| 45 | Log::Report | ||
| 46 | Log::Report::Dispatcher | ||
| 47 | Log::Report::Dispatcher::Try | ||
| 48 | /; | ||
| 49 | |||
| 50 | #-------------------- | ||
| 51 | |||
| 52 | |||
| 53 | sub init($) | ||
| 54 | { my ($self, $args) = @_; | ||
| 55 | $args->{accept} ||= 'ALL'; | ||
| 56 | $self->SUPER::init($args); | ||
| 57 | |||
| 58 | my $name = $self->name; | ||
| 59 | |||
| 60 | $self->{LRDL_levels} = { %default_reasonToLevel }; | ||
| 61 | if(my $to_level = delete $args->{to_level}) | ||
| 62 | { my @to = @$to_level; | ||
| 63 | while(@to) | ||
| 64 | { my ($reasons, $level) = splice @to, 0, 2; | ||
| 65 | my @reasons = expand_reasons $reasons; | ||
| 66 | |||
| 67 | $level =~ m/^[0-5]$/ | ||
| 68 | or error __x"Log4perl level '{level}' must be in 0-5", level => $level; | ||
| 69 | |||
| 70 | $self->{LRDL_levels}{$_} = $level for @reasons; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | if(my $config = delete $args->{config}) { | ||
| 75 | Log::Log4perl->init($config) or return; | ||
| 76 | } | ||
| 77 | |||
| 78 | $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | #-------------------- | ||
| 82 | |||
| 83 | sub logger(;$) | ||
| 84 | { my ($self, $domain) = @_; | ||
| 85 | defined $domain | ||
| 86 | or return Log::Log4perl->get_logger($self->name); | ||
| 87 | |||
| 88 | # get_logger() creates a logger if that does not exist. But we | ||
| 89 | # want to route it to default | ||
| 90 | $Log::Log4perl::LOGGERS_BY_NAME->{$domain} ||= Log::Log4perl->get_logger($self->name); | ||
| 91 | } | ||
| 92 | |||
| 93 | #-------------------- | ||
| 94 | |||
| 95 | sub log($$$$) | ||
| 96 | { my ($self, $opts, $reason, $msg, $domain) = @_; | ||
| 97 | my $text = $self->translate($opts, $reason, $msg) or return; | ||
| 98 | my $level = $self->reasonToLevel($reason); | ||
| 99 | |||
| 100 | local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 3; | ||
| 101 | |||
| 102 | $text =~ s/\s+$//s; # log4perl adds own \n | ||
| 103 | $self->logger($domain)->log($level, $text); | ||
| 104 | $self; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | sub reasonToLevel($) { $_[0]->{LRDL_levels}{$_[1]} } | ||
| 109 | |||
| 110 | 1; |