| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package MojoX::Log::Log4perl; | 
| 2 | 6 |  |  | 6 |  | 1384728 | use Mojo::Base 'Mojo::EventEmitter'; | 
|  | 6 |  |  |  |  | 313747 |  | 
|  | 6 |  |  |  |  | 42 |  | 
| 3 | 6 |  |  | 6 |  | 10655 | use Log::Log4perl; | 
|  | 6 |  |  |  |  | 237338 |  | 
|  | 6 |  |  |  |  | 29 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 6 |  |  | 6 |  | 292 | use warnings; | 
|  | 6 |  |  |  |  | 12 |  | 
|  | 6 |  |  |  |  | 160 |  | 
| 6 | 6 |  |  | 6 |  | 27 | use strict; | 
|  | 6 |  |  |  |  | 12 |  | 
|  | 6 |  |  |  |  | 1915 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.12'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | has history          => sub { [] }; | 
| 11 |  |  |  |  |  |  | has max_history_size => 10; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | my $format_warning_was_shown = 0; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | # development notes: Mojo::Log provides 'path' 'handle' and 'format' | 
| 16 |  |  |  |  |  |  | # to handle log location and formatting. Those make no sense in a Log4perl | 
| 17 |  |  |  |  |  |  | # environment (where you can set appenders as you wish) so they are | 
| 18 |  |  |  |  |  |  | # not implemented here; 'format' simply returns the passed-in strings joined by | 
| 19 |  |  |  |  |  |  | # newlines as otherwise Mojo::Log complains (RT #98034). | 
| 20 | 0 |  |  | 0 | 0 | 0 | sub path   { warn 'path() is not implemented in MojoX::Log::Log4perl. Please use appenders.'   } | 
| 21 | 0 |  |  | 0 | 0 | 0 | sub handle { warn 'handle() is not implemented in MojoX::Log::Log4perl. Please use appenders.' } | 
| 22 |  |  |  |  |  |  | sub format { | 
| 23 | 0 | 0 |  | 0 | 0 | 0 | if (!$format_warning_was_shown) { | 
| 24 | 0 |  |  |  |  | 0 | $format_warning_was_shown = 1; | 
| 25 | 0 |  |  |  |  | 0 | warn 'format() is not properly implemented in MojoX::Log::Log4perl. Please use appenders.'; | 
| 26 |  |  |  |  |  |  | } | 
| 27 | 0 |  |  | 0 |  | 0 | return sub { '[' . localtime(shift) . '] [' . shift() . '] ' . join("\n", @_, '') }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | # development notes: Mojo::Log provides 'context' | 
| 31 |  |  |  |  |  |  | # Mojo::Log (Mojolicious 8.23) was updated to add method context which | 
| 32 |  |  |  |  |  |  | # needs to exist here or we die | 
| 33 |  |  |  |  |  |  | sub context { | 
| 34 | 1 |  |  | 1 | 0 | 15333 | my ($self) = @_; | 
| 35 | 1 |  |  |  |  | 8 | return $self; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub new { | 
| 39 | 5 |  |  | 5 | 1 | 11341 | my ($class, $conf_file, $watch) = (@_); | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 5 |  | 100 |  |  | 39 | $conf_file ||= { | 
| 42 |  |  |  |  |  |  | 'log4perl.rootLogger' => 'DEBUG, SCREEN', | 
| 43 |  |  |  |  |  |  | 'log4perl.appender.SCREEN' => 'Log::Log4perl::Appender::Screen', | 
| 44 |  |  |  |  |  |  | 'log4perl.appender.SCREEN.layout' => 'PatternLayout', | 
| 45 |  |  |  |  |  |  | 'log4perl.appender.SCREEN.layout.ConversionPattern' => '[%d] [mojo] [%p] %m%n', | 
| 46 |  |  |  |  |  |  | }; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 5 | 50 |  |  |  | 17 | if ($watch) { | 
| 49 | 0 |  |  |  |  | 0 | Log::Log4perl::init_and_watch($conf_file, $watch); | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  | else { | 
| 52 | 5 |  |  |  |  | 50 | Log::Log4perl->init_once($conf_file); | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 5 |  |  |  |  | 16930 | my $self = $class->SUPER::new(); | 
| 56 | 5 |  |  |  |  | 69 | $self->on( message => \&_message ); | 
| 57 | 5 |  |  |  |  | 68 | return $self; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | # Hmm. Ah, a picture of my mommy. | 
| 61 |  |  |  |  |  |  | { | 
| 62 | 6 |  |  | 6 |  | 40 | no strict 'refs'; | 
|  | 6 |  |  |  |  | 13 |  | 
|  | 6 |  |  |  |  | 3919 |  | 
| 63 |  |  |  |  |  |  | for my $level ( | 
| 64 |  |  |  |  |  |  | qw/ trace | 
| 65 |  |  |  |  |  |  | debug | 
| 66 |  |  |  |  |  |  | info | 
| 67 |  |  |  |  |  |  | warn | 
| 68 |  |  |  |  |  |  | error | 
| 69 |  |  |  |  |  |  | fatal | 
| 70 |  |  |  |  |  |  | logwarn | 
| 71 |  |  |  |  |  |  | logdie | 
| 72 |  |  |  |  |  |  | error_warn | 
| 73 |  |  |  |  |  |  | error_die | 
| 74 |  |  |  |  |  |  | logcarp | 
| 75 |  |  |  |  |  |  | logcluck | 
| 76 |  |  |  |  |  |  | logcroak | 
| 77 |  |  |  |  |  |  | logconfess | 
| 78 |  |  |  |  |  |  | / ) { | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | *{ __PACKAGE__ . "::$level" } = | 
| 81 |  |  |  |  |  |  | sub { | 
| 82 | 15 |  |  | 15 |  | 10319 | return shift->emit( message => $level => @_ ); | 
| 83 |  |  |  |  |  |  | }; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub _message { | 
| 88 | 11 |  |  | 11 |  | 124 | my ($self, $level, @message ) = @_; | 
| 89 | 11 |  |  |  |  | 30 | my $depth = 3; | 
| 90 | 11 |  |  |  |  | 54 | local $Log::Log4perl::caller_depth | 
| 91 |  |  |  |  |  |  | = $Log::Log4perl::caller_depth + $depth; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 11 | 100 |  |  |  | 35 | if ($self->_get_logger( $depth )->$level( @message )) { | 
| 94 | 10 |  |  |  |  | 3415 | my $history = $self->history; | 
| 95 | 10 |  |  |  |  | 64 | my $max     = $self->max_history_size; | 
| 96 | 10 |  |  |  |  | 51 | push @$history => [ time, $level, @message ]; | 
| 97 | 10 | 100 |  |  |  | 27 | splice (@$history, 0, scalar @$history - $max) | 
| 98 |  |  |  |  |  |  | if scalar @$history > $max; | 
| 99 |  |  |  |  |  |  | } | 
| 100 | 11 |  |  |  |  | 52 | return $self; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 1 |  |  | 1 | 1 | 595 | sub log { shift->emit('message', lc(shift), @_) } | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 1 |  |  | 1 | 1 | 374 | sub is_trace { shift->_get_logger->is_trace } | 
| 106 | 3 |  |  | 3 | 1 | 1176 | sub is_debug { shift->_get_logger->is_debug } | 
| 107 | 1 |  |  | 1 | 1 | 241 | sub is_info  { shift->_get_logger->is_info  } | 
| 108 | 1 |  |  | 1 | 1 | 235 | sub is_warn  { shift->_get_logger->is_warn  } | 
| 109 | 1 |  |  | 1 | 1 | 234 | sub is_error { shift->_get_logger->is_error } | 
| 110 | 1 |  |  | 1 | 1 | 256 | sub is_fatal { shift->_get_logger->is_fatal } | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | sub is_level { | 
| 113 | 12 |  |  | 12 | 1 | 5479 | my ($self, $level) = (@_); | 
| 114 | 12 | 50 |  |  |  | 25 | return 0 unless $level; | 
| 115 |  |  |  |  |  |  |  | 
| 116 | 12 | 50 |  |  |  | 52 | if ($level =~ m/^(?:trace|debug|info|warn|error|fatal)$/o) { | 
| 117 | 12 |  |  |  |  | 23 | my $is_level = "is_$level"; | 
| 118 | 12 |  |  |  |  | 20 | return $self->_get_logger->$is_level; | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  | else { | 
| 121 | 0 |  |  |  |  | 0 | return 0; | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | sub level { | 
| 126 | 11 |  |  | 11 | 1 | 3746 | my ($self, $level) = (@_); | 
| 127 | 11 |  |  |  |  | 22 | my $logger = $self->_get_logger; | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 11 |  |  |  |  | 264 | require Log::Log4perl::Level; | 
| 130 | 11 | 100 |  |  |  | 25 | if ($level) { | 
| 131 | 9 |  |  |  |  | 29 | return $logger->level( Log::Log4perl::Level::to_priority(uc $level) ); | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  | else { | 
| 134 | 2 |  |  |  |  | 5 | return Log::Log4perl::Level::to_level( $logger->level() ); | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | # $_[0] == $self, $_[1] == optional caller level (defaults to 1) | 
| 139 |  |  |  |  |  |  | sub _get_logger { | 
| 140 | 42 |  | 100 | 42 |  | 220 | return Log::Log4perl->get_logger( scalar caller( $_[1] || 1 ) ); | 
| 141 |  |  |  |  |  |  | } | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | 1; | 
| 144 |  |  |  |  |  |  | __END__ |