| 1 | package Dancer2::Logger::Log4perl; | ||||||||||
| 2 | # ABSTRACT: Dancer2 logger interface for Log4perl. | ||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use Log::Log4perl qw( :easy ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Dancer2::Core::Types; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | with 'Dancer2::Core::Role::Logger'; | |||||||||
| 9 | |||||||||||
| 10 | 1 | has config_file => ( | |||||||||
| 11 | is => 'ro', | ||||||||||
| 12 | isa => ReadableFilePath, | ||||||||||
| 13 | ); | ||||||||||
| 14 | |||||||||||
| 15 | has config_watch_interval => ( | ||||||||||
| 16 | is => 'ro', | ||||||||||
| 17 | isa => sub { | ||||||||||
| 18 | 0 | ▶ | my $config_watch_interval = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 19 | |||||||||||
| 20 | # Can either be 'HUP' or a number | ||||||||||
| 21 | 0 | Str->( $config_watch_interval ); | |||||||||
| 22 | 0 | ▶ | if( $config_watch_interval eq 'HUP' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 0 | return; | |||||||||
| 24 | } | ||||||||||
| 25 | else { | ||||||||||
| 26 | 0 | Num->( $config_watch_interval ); | |||||||||
| 27 | } | ||||||||||
| 28 | }, | ||||||||||
| 29 | 1 | ); | |||||||||
| 30 | |||||||||||
| 31 | sub _initialize_log4perl { | ||||||||||
| 32 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_initialize_log4perl: not called CC=3 SCAR=24.8
| |||||||||||
| 33 | |||||||||||
| 34 | # If config_file is defined, then use that | ||||||||||
| 35 | 0 | ▶ | if( defined $self->config_file ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | |||||||||||
| 37 | # ...optionally with the watch interval | ||||||||||
| 38 | 0 | ▶ | if( defined $self->config_watch_interval ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 0 | Log::Log4perl->init_and_watch( $self->config_file, $self->config_watch_interval ); | |||||||||
| 40 | } | ||||||||||
| 41 | else { | ||||||||||
| 42 | 0 | Log::Log4perl->init( $self->config_file ); | |||||||||
| 43 | } | ||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | # Otherwise we'll easy init with the appropriate log level | ||||||||||
| 47 | else { | ||||||||||
| 48 | 0 | my $log_level_mapping = { | |||||||||
| 49 | 'error' => $ERROR, | ||||||||||
| 50 | 'warn' => $WARN, | ||||||||||
| 51 | 'warning' => $WARN, | ||||||||||
| 52 | 'info' => $INFO, | ||||||||||
| 53 | 'debug' => $DEBUG, | ||||||||||
| 54 | 'trace' => $TRACE, | ||||||||||
| 55 | 'core' => $TRACE, | ||||||||||
| 56 | }; | ||||||||||
| 57 | |||||||||||
| 58 | 0 | Log::Log4perl->easy_init( $log_level_mapping->{ $self->log_level } ); | |||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | sub log { | ||||||||||
| 63 | my ( $self, $level, $message ) = @_; | ||||||||||
| 64 | |||||||||||
| 65 | # Need to initialize Log4perl if it isn't yet | ||||||||||
| 66 | if( !Log::Log4perl->initialized() ) { | ||||||||||
| 67 | $self->_initialize_log4perl(); | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | # Need to convert Dancer2 log levels to Log4perl levels | ||||||||||
| 71 | $level = 'warn' if $level eq 'warning'; | ||||||||||
| 72 | $level = 'trace' if $level eq 'core'; | ||||||||||
| 73 | |||||||||||
| 74 | # TODO: Couldn't get $Log::Log4perl::caller_depth to work | ||||||||||
| 75 | Log::Log4perl->get_logger( scalar caller(4) )->$level( $message ); | ||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 1 | 1; | |||||||||
| 79 | |||||||||||
| 80 | __END__ | ||||||||||
| 81 | |||||||||||
| 82 | =pod | ||||||||||
| 83 | |||||||||||
| 84 | =encoding UTF-8 | ||||||||||
| 85 | |||||||||||
| 86 | =head1 NAME | ||||||||||
| 87 | |||||||||||
| 88 | Dancer2::Logger::Log4perl - Dancer2 logger interface for Log4perl. | ||||||||||
| 89 | |||||||||||
| 90 | =head1 VERSION | ||||||||||
| 91 | |||||||||||
| 92 | version 0.05 | ||||||||||
| 93 | |||||||||||
| 94 | =head1 SYNOPSIS | ||||||||||
| 95 | |||||||||||
| 96 | In your F<config.yml>: | ||||||||||
| 97 | |||||||||||
| 98 | logger: log4perl | ||||||||||
| 99 | log: core | ||||||||||
| 100 | engines: | ||||||||||
| 101 | logger: | ||||||||||
| 102 | log4perl: | ||||||||||
| 103 | config_file: log4perl.conf | ||||||||||
| 104 | |||||||||||
| 105 | In your F<log4perl.conf>: | ||||||||||
| 106 | |||||||||||
| 107 | log4perl.rootLogger = DEBUG, LOG1 | ||||||||||
| 108 | log4perl.appender.LOG1 = Log::Log4perl::Appender::File | ||||||||||
| 109 | log4perl.appender.LOG1.filename = /var/log/mylog.log | ||||||||||
| 110 | log4perl.appender.LOG1.mode = append | ||||||||||
| 111 | log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout | ||||||||||
| 112 | log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n | ||||||||||
| 113 | |||||||||||
| 114 | =head1 DESCRIPTION | ||||||||||
| 115 | |||||||||||
| 116 | This class is an interface between L<Dancer2>'s logging engine abstraction | ||||||||||
| 117 | layer and the L<Log::Log4perl> library. In order to use it, you have to | ||||||||||
| 118 | set the C<logger> engine to C<log4perl>. | ||||||||||
| 119 | |||||||||||
| 120 | L<Dancer2>'s C<core> level messages are passed to L<Log4perl> as level C<trace> | ||||||||||
| 121 | but will not be passed unless L<Dancer2>'s C<log> config is C<core>. | ||||||||||
| 122 | |||||||||||
| 123 | C<log> should be set a lower priority than the lowest priority as set in your | ||||||||||
| 124 | L<Log4perl> configuration. If it isn't, the log messages will not be passed | ||||||||||
| 125 | to L<Log4perl>. | ||||||||||
| 126 | |||||||||||
| 127 | =head1 CONFIGURATION | ||||||||||
| 128 | |||||||||||
| 129 | If you don't specify C<config_file> then Log4perl will easy init with the | ||||||||||
| 130 | appropriate log level, as specified by Dancer2. | ||||||||||
| 131 | |||||||||||
| 132 | =over 4 | ||||||||||
| 133 | |||||||||||
| 134 | =item B<< config_file >> | ||||||||||
| 135 | |||||||||||
| 136 | You can specify the log4perl configuration file using the C<config_file> option: | ||||||||||
| 137 | |||||||||||
| 138 | logger: log4perl | ||||||||||
| 139 | engines: | ||||||||||
| 140 | logger: | ||||||||||
| 141 | log4perl: | ||||||||||
| 142 | config_file: log4perl.conf | ||||||||||
| 143 | |||||||||||
| 144 | =item B<< config_watch_interval >> | ||||||||||
| 145 | |||||||||||
| 146 | You can optionally specify the watch interval, either in seconds or as 'HUP': | ||||||||||
| 147 | |||||||||||
| 148 | logger: log4perl | ||||||||||
| 149 | engines: | ||||||||||
| 150 | logger: | ||||||||||
| 151 | log4perl: | ||||||||||
| 152 | config_file: log4perl.conf | ||||||||||
| 153 | config_watch_interval: 30 | ||||||||||
| 154 | |||||||||||
| 155 | =back | ||||||||||
| 156 | |||||||||||
| 157 | =head1 CREDITS | ||||||||||
| 158 | |||||||||||
| 159 | This was originally developed by Ryan Larscheidt and Jon Miner at the | ||||||||||
| 160 | University of Wisconsin. The initial version of this was built upon their | ||||||||||
| 161 | final version. I cannot thank them enough for their work on this - it not | ||||||||||
| 162 | only solved my needs at C<$work>, but also plugs a hole in our ecosystem. | ||||||||||
| 163 | |||||||||||
| 164 | =head1 AUTHOR | ||||||||||
| 165 | |||||||||||
| 166 | Jason A. Crome <cromedome@cpan.org> | ||||||||||
| 167 | |||||||||||
| 168 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 169 | |||||||||||
| 170 | This software is copyright (c) 2018 by Jason A. Crome. | ||||||||||
| 171 | |||||||||||
| 172 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 173 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 174 | |||||||||||
| 175 | =cut | ||||||||||