| 1 | package Log::Log4perl::Catalyst; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Log::Log4perl qw(:levels); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Log::Log4perl::Logger; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $VERSION = '1.53'; | |||||||||
| 8 | 1 | our $CATALYST_APPENDER_SUFFIX = "catalyst_buffer"; | |||||||||
| 9 | 1 | our $LOG_LEVEL_ADJUSTMENT = 1; | |||||||||
| 10 | |||||||||||
| 11 | 1 | init(); | |||||||||
| 12 | |||||||||||
| 13 | ################################################## | ||||||||||
| 14 | 1 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=2 SCAR=9.2
Pod
undocumented
| |||||||||||
| 15 | ################################################## | ||||||||||
| 16 | |||||||||||
| 17 | 1 | my @levels = qw[ trace debug info warn error fatal ]; | |||||||||
| 18 | |||||||||||
| 19 | 1 | Log::Log4perl->wrapper_register(__PACKAGE__); | |||||||||
| 20 | |||||||||||
| 21 | 1 | for my $level (@levels) { | |||||||||
| 22 | 1 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 6 | ▶ | *{$level} = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=4.6
| |||||||||||
| 25 | 0 | my ( $self, @message ) = @_; | |||||||||
| 26 | |||||||||||
| 27 | 0 | local $Log::Log4perl::caller_depth = | |||||||||
| 28 | $Log::Log4perl::caller_depth + | ||||||||||
| 29 | $LOG_LEVEL_ADJUSTMENT; | ||||||||||
| 30 | |||||||||||
| 31 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||
| 32 | 0 | $logger->$level(@message); | |||||||||
| 33 | 0 | return 1; | |||||||||
| 34 | 6 | }; | |||||||||
| 35 | |||||||||||
| 36 | 6 | ▶ | *{"is_$level"} = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=4.6
| |||||||||||
| 37 | 0 | my ( $self, @message ) = @_; | |||||||||
| 38 | |||||||||||
| 39 | 0 | local $Log::Log4perl::caller_depth = | |||||||||
| 40 | $Log::Log4perl::caller_depth + | ||||||||||
| 41 | $LOG_LEVEL_ADJUSTMENT; | ||||||||||
| 42 | |||||||||||
| 43 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||
| 44 | 0 | my $func = "is_" . $level; | |||||||||
| 45 | 0 | return $logger->$func; | |||||||||
| 46 | 6 | }; | |||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | ################################################## | ||||||||||
| 51 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=10 SCAR=23.3
Pod
documented
| |||||||||||
| 52 | ################################################## | ||||||||||
| 53 | 2 | my($class, $config, %options) = @_; | |||||||||
| 54 | |||||||||||
| 55 | 2 | my $self = { | |||||||||
| 56 | autoflush => 0, | ||||||||||
| 57 | abort => 0, | ||||||||||
| 58 | watch_delay => 0, | ||||||||||
| 59 | %options, | ||||||||||
| 60 | }; | ||||||||||
| 61 | |||||||||||
| 62 | 2 | ▶ | if( !Log::Log4perl->initialized() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 1 | ▶ | if( defined $config ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 1 | ▶ | if( $self->{watch_delay} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | Log::Log4perl::init_and_watch( $config, $self->{watch_delay} ); | |||||||||
| 66 | } else { | ||||||||||
| 67 | 1 | Log::Log4perl::init( $config ); | |||||||||
| 68 | } | ||||||||||
| 69 | } else { | ||||||||||
| 70 | 0 | Log::Log4perl->easy_init({ | |||||||||
| 71 | level => $DEBUG, | ||||||||||
| 72 | layout => "[%d] [catalyst] [%p] %m%n", | ||||||||||
| 73 | }); | ||||||||||
| 74 | } | ||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | # Unless we have autoflush, Catalyst likes to buffer all messages | ||||||||||
| 78 | # until it calls flush(). This is somewhat unusual for Log4perl, | ||||||||||
| 79 | # but we just put an army of buffer appenders in front of all | ||||||||||
| 80 | # appenders defined in the system. | ||||||||||
| 81 | |||||||||||
| 82 | 2 | ▶ | if(! $options{autoflush} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 2 | for my $appender (values %Log::Log4perl::Logger::APPENDER_BY_NAME) { | |||||||||
| 84 | 6 | ▶ | next if $appender->{name} =~ /_$CATALYST_APPENDER_SUFFIX$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | |||||||||||
| 86 | # put a buffering appender in front of every appender | ||||||||||
| 87 | # defined so far | ||||||||||
| 88 | |||||||||||
| 89 | 4 | my $buf_app_name = "$appender->{name}_$CATALYST_APPENDER_SUFFIX"; | |||||||||
| 90 | |||||||||||
| 91 | # Somebody's already given this appender a buffer, so leave it | ||||||||||
| 92 | # alone. Building a second one here would drop it into the | ||||||||||
| 93 | # registry on top of the first, but the loggers already writing to | ||||||||||
| 94 | # the first one never get moved across, because the loop below | ||||||||||
| 95 | # only picks up loggers still on the raw appender. They'd carry on | ||||||||||
| 96 | # filling a buffer that _flush() can no longer see, and their | ||||||||||
| 97 | # messages would never come out. Happens as soon as two Catalyst | ||||||||||
| 98 | # apps share a process and a config. | ||||||||||
| 99 | next if exists | ||||||||||
| 100 | 4 | ▶ | $Log::Log4perl::Logger::APPENDER_BY_NAME{ $buf_app_name }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | |||||||||||
| 102 | my $buf_app = Log::Log4perl::Appender->new( | ||||||||||
| 103 | 'Log::Log4perl::Appender::Buffer', | ||||||||||
| 104 | name => $buf_app_name, | ||||||||||
| 105 | appender => $appender->{name}, | ||||||||||
| 106 | 1 | ▶ | trigger => sub { 0 }, # only trigger on explicit flush() | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 107 | 2 | ); | |||||||||
| 108 | |||||||||||
| 109 | 2 | Log::Log4perl->add_appender($buf_app); | |||||||||
| 110 | 2 | $buf_app->post_init(); | |||||||||
| 111 | 2 | $buf_app->composite(1); | |||||||||
| 112 | |||||||||||
| 113 | # Point all loggers currently connected to the previously defined | ||||||||||
| 114 | # appenders to the chained buffer appenders instead. | ||||||||||
| 115 | |||||||||||
| 116 | 2 | foreach my $logger ( | |||||||||
| 117 | values %$Log::Log4perl::Logger::LOGGERS_BY_NAME){ | ||||||||||
| 118 | 4 | ▶ | if(defined $logger->remove_appender( $appender->{name}, 0, 1)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 2 | $logger->add_appender( $buf_app ); | |||||||||
| 120 | } | ||||||||||
| 121 | } | ||||||||||
| 122 | } | ||||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 2 | bless $self, $class; | |||||||||
| 126 | |||||||||||
| 127 | 2 | return $self; | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | ################################################## | ||||||||||
| 131 | 1 | ▶ | sub _flush { | ||||||||
|
Subroutine
_flush: called CC=4 SCAR=14.1
| |||||||||||
| 132 | ################################################## | ||||||||||
| 133 | 1 | my ($self) = @_; | |||||||||
| 134 | |||||||||||
| 135 | 1 | for my $appender (values %Log::Log4perl::Logger::APPENDER_BY_NAME) { | |||||||||
| 136 | 4 | ▶ | next if $appender->{name} !~ /_$CATALYST_APPENDER_SUFFIX$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | |||||||||||
| 138 | 2 | ▶ | if ($self->abort) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | 0 | $appender->{appender}{buffer} = []; | |||||||||
| 140 | } | ||||||||||
| 141 | else { | ||||||||||
| 142 | 2 | $appender->flush(); | |||||||||
| 143 | } | ||||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | 1 | $self->abort(undef); | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | ################################################## | ||||||||||
| 150 | 3 | ▶ | sub abort { | ||||||||
|
Subroutine
abort: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 151 | ################################################## | ||||||||||
| 152 | 3 | my $self = shift; | |||||||||
| 153 | |||||||||||
| 154 | 3 | ▶ | $self->{abort} = $_[0] if @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | |||||||||||
| 156 | 3 | return $self->{abort}; | |||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | ################################################## | ||||||||||
| 160 | 0 | ▶ | sub levels { | ||||||||
|
Subroutine
levels: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 161 | ################################################## | ||||||||||
| 162 | # stub function, until we have something meaningful | ||||||||||
| 163 | 0 | return 0; | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | ################################################## | ||||||||||
| 167 | 0 | ▶ | sub enable { | ||||||||
|
Subroutine
enable: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 168 | ################################################## | ||||||||||
| 169 | # stub function, until we have something meaningful | ||||||||||
| 170 | 0 | return 0; | |||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | ################################################## | ||||||||||
| 174 | 0 | ▶ | sub disable { | ||||||||
|
Subroutine
disable: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 175 | ################################################## | ||||||||||
| 176 | # stub function, until we have something meaningful | ||||||||||
| 177 | 0 | return 0; | |||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | 1 | 1; | |||||||||
| 181 | |||||||||||
| 182 | __END__ | ||||||||||