| 1 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | package Jifty::Logger; | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | Jifty::Logger -- A master class for Jifty's logging framework | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | Jifty uses the Log4perl module to log error messages. In Jifty | ||||||||||||||||||||
| 13 | programs there's two ways you can get something logged: | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | Firstly, Jifty::Logger captures all standard warnings that Perl | ||||||||||||||||||||
| 16 | emits. So in addition to everything output from perl via the | ||||||||||||||||||||
| 17 | warnings pragmas, you can also log messages like so: | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | warn("The WHAM is overheating!"); | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | This doesn't give you much control however. The second way | ||||||||||||||||||||
| 22 | allows you to specify the level that you want logging to | ||||||||||||||||||||
| 23 | occur at: | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | Jifty->log->debug("Checking the WHAM"); | ||||||||||||||||||||
| 26 | Jifty->log->info("Potential WHAM problem detected"); | ||||||||||||||||||||
| 27 | Jifty->log->warn("The WHAM is overheating"); | ||||||||||||||||||||
| 28 | Jifty->log->error("PANIC!"); | ||||||||||||||||||||
| 29 | Jifty->log->fatal("Someone call Eddie Murphy!"); | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =head2 Configuring Log4perl | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | Unless you specify otherwise in the configuration file, Jifty will | ||||||||||||||||||||
| 34 | supply a default Log4perl configuration. | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | The default log configuration that logs all messages to the screen | ||||||||||||||||||||
| 37 | (i.e. to STDERR, be that directly to the terminal or to the FastCGI | ||||||||||||||||||||
| 38 | log file.) It will log all messages of equal or higher priority | ||||||||||||||||||||
| 39 | to the LogLevel configuration option. | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | --- | ||||||||||||||||||||
| 42 | framework: | ||||||||||||||||||||
| 43 | LogLevel: DEBUG | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | You can override the LogLevel configuration option by setting JIFTY_LOG_LEVEL | ||||||||||||||||||||
| 46 | in your environment. | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | You can tell Jifty to use an entirely different Logging | ||||||||||||||||||||
| 49 | configuration by specifying the filename of a standard Log4perl | ||||||||||||||||||||
| 50 | config file in the LogConfig config option (see L<Log::Log4perl> for | ||||||||||||||||||||
| 51 | the format of this config file.) | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | --- | ||||||||||||||||||||
| 54 | framework: | ||||||||||||||||||||
| 55 | LogConfig: etc/log4perl.conf | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | Note that specifying your own config file prevents the LogLevel | ||||||||||||||||||||
| 58 | config option from having any effect. | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | You can tell Log4perl to check that file periodically for changes. | ||||||||||||||||||||
| 61 | This costs you a little in application performance, but allows | ||||||||||||||||||||
| 62 | you to change the logging level of a running application. You | ||||||||||||||||||||
| 63 | need to set LogReload to the frequency, in seconds, that the | ||||||||||||||||||||
| 64 | file should be checked. | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | --- | ||||||||||||||||||||
| 67 | framework: | ||||||||||||||||||||
| 68 | LogConfig: etc/log4perl.conf | ||||||||||||||||||||
| 69 | LogReload: 10 | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | (This is implemented with Log4perl's init_and_watch functionality) | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | ▶ | use Log::Log4perl; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 76 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 1 | ▶ | use base qw/Jifty::Object/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =head1 METHODS | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =head2 new COMPONENT | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | This class method instantiates a new C<Jifty::Logger> object. This | ||||||||||||||||||||
| 85 | object deals with logging for the system. | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | Takes an optional name for this Jifty's logging "component" - See | ||||||||||||||||||||
| 88 | L<Log::Log4perl> for some detail about what that is. It sets up a "warn" | ||||||||||||||||||||
| 89 | handler which logs warnings to the specified component. | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 94 | 0 | my $class = shift; | |||||||||||||||||||
| 95 | 0 | my $component = shift; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | my $self = {}; | |||||||||||||||||||
| 98 | 0 | bless $self, $class; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | ▶ | $component = '' unless defined $component; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | # configure Log::Log4perl unless we've done it already | ||||||||||||||||||||
| 103 | 0 | ▶ | if (not Log::Log4perl->initialized) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 0 | $class->_initialize_log4perl; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | # create a log4perl object that answers to this component name | ||||||||||||||||||||
| 108 | 0 | my $logger = Log::Log4perl->get_logger($component); | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # whenever Perl wants to warn something out capture it with a signal | ||||||||||||||||||||
| 111 | # handler and pass it to log4perl | ||||||||||||||||||||
| 112 | 0 | my $previous_warning_handler = $SIG{__WARN__}; | |||||||||||||||||||
| 113 | 0 | ▶ | $SIG{__WARN__} = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | # This caller_depth line tells Log4perl to report | ||||||||||||||||||||
| 116 | # the error as coming from on step further up the | ||||||||||||||||||||
| 117 | # caller chain (i.e., where the warning originated) | ||||||||||||||||||||
| 118 | # instead of from the $logger->warn line. | ||||||||||||||||||||
| 119 | 0 | local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | # If the logger has been taken apart by global destruction, | ||||||||||||||||||||
| 122 | # don't try to use it to log warnings | ||||||||||||||||||||
| 123 | 0 | ▶ | if (Log::Log4perl->initialized) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 124 | 0 | my $action = $self->_warning_action(@_); | |||||||||||||||||||
| 125 | # @_ often has read-only scalars, so we need to break | ||||||||||||||||||||
| 126 | # the aliasing so we can remove trailing newlines | ||||||||||||||||||||
| 127 | 0 | my @lines = map {"$_"} @_; | |||||||||||||||||||
| 128 | 0 | $logger->$action(map {chomp; $_} @lines); | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | elsif ($previous_warning_handler) { | ||||||||||||||||||||
| 131 | # Fallback to the old handler | ||||||||||||||||||||
| 132 | 0 | goto &$previous_warning_handler; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||
| 135 | # Now handler - just carp about it for now | ||||||||||||||||||||
| 136 | 0 | local $SIG{__WARN__}; | |||||||||||||||||||
| 137 | 0 | carp(@_); | |||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | 0 | }; | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 0 | return $self; | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 0 | ▶ | sub _initialize_log4perl { | ||||||||||||||||||
|
Subroutine
_initialize_log4perl: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 145 | 0 | my $class = shift; | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | my $log_config | |||||||||||||||||||
| 148 | = Jifty::Util->absolute_path( Jifty->config->framework('LogConfig') ); | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 0 | ▶ | if ( defined Jifty->config->framework('LogReload') ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 151 | 0 | Log::Log4perl->init_and_watch( $log_config, | |||||||||||||||||||
| 152 | Jifty->config->framework('LogReload') ); | ||||||||||||||||||||
| 153 | ▶ | } elsif ( -f $log_config and -r $log_config ) { | |||||||||||||||||||
|
Condition0%0/3
-f $log_config and -r $log_config
MC/DC0%0/2
-f $log_config and -r $log_config
-f $log_config
-r $log_config
Truth table0%0/3
A and B
A= -f $log_configB= -r $log_config
| |||||||||||||||||||||
| 154 | 0 | Log::Log4perl->init($log_config); | |||||||||||||||||||
| 155 | } else { | ||||||||||||||||||||
| 156 | 0 | ▶ | my $log_level = uc($ENV{JIFTY_LOG_LEVEL} || Jifty->config->framework('LogLevel')); | ||||||||||||||||||
|
Condition0%0/3
$ENV{'JIFTY_LOG_LEVEL'} || 'Jifty'->config->framework('LogLevel')
MC/DC0%0/2
$ENV{'JIFTY_LOG_LEVEL'} || 'Jifty'->config->framework('LogLevel')
$ENV{'JIFTY_LOG_LEVEL'}
'Jifty'->config->framework('LogLevel')
Truth table0%0/3
A || B
A= $ENV{'JIFTY_LOG_LEVEL'}B= 'Jifty'->config->framework('LogLevel')
| |||||||||||||||||||||
| 157 | 0 | my %default = ( | |||||||||||||||||||
| 158 | 'log4perl.rootLogger' => "$log_level,Screen", | ||||||||||||||||||||
| 159 | 'log4perl.appender.Screen' => 'Log::Log4perl::Appender::Screen', | ||||||||||||||||||||
| 160 | 'log4perl.appender.Screen.stderr' => 1, | ||||||||||||||||||||
| 161 | 'log4perl.appender.Screen.layout' => | ||||||||||||||||||||
| 162 | 'Log::Log4perl::Layout::SimpleLayout' | ||||||||||||||||||||
| 163 | ); | ||||||||||||||||||||
| 164 | 0 | Log::Log4perl->init( \%default ); | |||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =head2 _warning_action | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | change the Log4Perl action from warn to error|info|etc based | ||||||||||||||||||||
| 171 | on the content of the warning. | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Added because DBD::Pg throws up NOTICE and other messages | ||||||||||||||||||||
| 174 | as warns, and we really want those to be info (or error, depending | ||||||||||||||||||||
| 175 | on the code). List based on Postgres documentation | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | TODO: needs to be smarter than just string matching | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | returns a valid Log::Log4Perl action, if nothing matches | ||||||||||||||||||||
| 180 | will return the default of warn since we're in a __WARN__ handler | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 0 | ▶ | sub _warning_action { | ||||||||||||||||||
|
Subroutine
_warning_action: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 185 | 0 | my $self = shift; | |||||||||||||||||||
| 186 | 0 | my $warnings = join('',@_); | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 0 | my %pg_notices = ('DEBUG\d+' => 'debug', | |||||||||||||||||||
| 189 | 'INFO' => 'info', | ||||||||||||||||||||
| 190 | 'NOTICE' => 'info', | ||||||||||||||||||||
| 191 | '.*ERROR.*database .* does not exist' => 'info', | ||||||||||||||||||||
| 192 | '.*couldn.t execute the query .DROP DATABASE.' => 'info', | ||||||||||||||||||||
| 193 | 'WARNING' => 'warn', | ||||||||||||||||||||
| 194 | 'DBD::Pg.+ERROR' => 'error', | ||||||||||||||||||||
| 195 | 'LOG' => 'warn', | ||||||||||||||||||||
| 196 | 'FATAL' => 'fatal', | ||||||||||||||||||||
| 197 | 'PANIC' => 'fatal' ); | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 0 | foreach my $notice (keys %pg_notices) { | |||||||||||||||||||
| 200 | 0 | ▶ | if ($warnings =~ /^$notice/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 201 | 0 | return $pg_notices{$notice}; | |||||||||||||||||||
| 202 | } | ||||||||||||||||||||
| 203 | } | ||||||||||||||||||||
| 204 | 0 | return 'warn'; | |||||||||||||||||||
| 205 | } | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | =head1 AUTHOR | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | Various folks at Best Practical Solutions, LLC. | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | Mark Fowler <mark@twoshortplanks.com> fiddled a bit. | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | =cut | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 1 | 1; | |||||||||||||||||||