| 1 | package Log::Log4perl::Layout::PatternLayout::Elapsed; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Log::Log4perl::Layout::PatternLayout::Elapsed - Logs the time elapsed between the last log event | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | From a log4perl configuration file: | ||
| 10 | |||
| 11 | log4perl.rootLogger = ALL, DEV, FILE | ||
| 12 | |||
| 13 | # Colored console with PatternLayout::Elapsed | ||
| 14 | log4perl.appender.DEV = Log::Log4perl::Appender::ScreenColoredLevels | ||
| 15 | log4perl.appender.DEV.layout = Log::Log4perl::Layout::PatternLayout::Elapsed | ||
| 16 | log4perl.appender.DEV.layout.ConversionPattern = %5Rms %-5p %m [%M:%L]%n | ||
| 17 | log4perl.appender.DEV.Threshold = ALL | ||
| 18 | |||
| 19 | |||
| 20 | # Log file with timestamps and elapsed time | ||
| 21 | log4perl.appender.FILE = Log::Dispatch::FileRotate | ||
| 22 | log4perl.appender.FILE.filename = 'logs/app.log' | ||
| 23 | log4perl.appender.FILE.Threshold = ALL | ||
| 24 | |||
| 25 | # Using both an the time elapsed since the begining and the last event | ||
| 26 | log4perl.appender.FILE.layout = Log::Log4perl::Layout::PatternLayout::Elapsed | ||
| 27 | log4perl.appender.FILE.layout.ConversionPattern = %d{ISO8601} (%5rms) %-5p [%-12c] %C{2} %M:%L - %m [%5Rms]%n | ||
| 28 | |||
| 29 | Through Perl code (why would you do that?): | ||
| 30 | |||
| 31 | use Log::Log4perl::Layout::PatternLayout::Elapsed; | ||
| 32 | |||
| 33 | my $layout = Log::Log4perl::Layout::PatternLayout::Elapsed->new( | ||
| 34 | '%5Rms %-5p %m [%M:%L]%n' | ||
| 35 | ); | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | B<NOTE>: L<Log::Log4perl> 1.25 includes already the functionality of this module | ||
| 40 | in L<Log::Log4perl::Layout::PatternLayout>. It's strongly suggested that you use | ||
| 41 | Log::Log4perl's implementation as it fixes some consistency issues when mixing | ||
| 42 | %r and %R in the same logging message. If you can't upgrade to Log::Log4perl | ||
| 43 | 1.25 or higher then you should be able to use this module and enjoy the same | ||
| 44 | functionality. | ||
| 45 | |||
| 46 | This layout adds the placeholder C<%R>, which is used to display the time | ||
| 47 | elapsed between two consecutive logging events. In the case of the first logging | ||
| 48 | event the time elapsed since the beginning of the application will be displayed. | ||
| 49 | |||
| 50 | This module is a sub class of L<Log::Log4perl::Layout::PatternLayout>, which | ||
| 51 | means that it implements that same formats and can used the same placeholders. | ||
| 52 | The module L<Log::Log4perl::Layout::PatternLayout> is in turn based on | ||
| 53 | L<http://jakarta.apache.org/log4j/docs/api/org/apache/log4j/PatternLayout.html>. | ||
| 54 | |||
| 55 | The C<new()> method creates a new PatternLayout, specifying its log format. The | ||
| 56 | format string supports all placeholders implemented by | ||
| 57 | L<Log::Log4perl::Layout::PatternLayout>, with the addition of the new | ||
| 58 | placeholder: | ||
| 59 | |||
| 60 | %R Number of milliseconds elapsed since the last logging event | ||
| 61 | |||
| 62 | =head1 IMPLEMENTATION | ||
| 63 | |||
| 64 | This module is implemented in order to ensure that each appender will track it's | ||
| 65 | own elapsed time. This way the time displayed is truly the time spent between | ||
| 66 | two consecutive log events for each appender. Thus if different thresholds are | ||
| 67 | applied to two appenders logging in the same application it's normal if they | ||
| 68 | both show different values for the time elapsed for a same log statement. This | ||
| 69 | is because the previous logging message might have not been issued at the same | ||
| 70 | time due to the different thresholds. | ||
| 71 | |||
| 72 | Therefore the following Perl code: | ||
| 73 | |||
| 74 | use Time::HiRes qw(sleep); | ||
| 75 | INFO "Start"; | ||
| 76 | |||
| 77 | sleep 0.1; | ||
| 78 | DEBUG "Pause: 0.1 sec"; | ||
| 79 | |||
| 80 | sleep 1.5; | ||
| 81 | INFO "Pause: 1.5 secs"; | ||
| 82 | |||
| 83 | sleep 0.5; | ||
| 84 | DEBUG "Pause: 0.5 sec"; | ||
| 85 | |||
| 86 | WARN "End"; | ||
| 87 | |||
| 88 | When executed with the following Log4perl configuration: | ||
| 89 | |||
| 90 | log4perl.rootLogger = ALL, A, B | ||
| 91 | |||
| 92 | log4perl.appender.A = Log::Log4perl::Appender::Screen | ||
| 93 | log4perl.appender.A.layout = Log::Log4perl::Layout::PatternLayout::Elapsed | ||
| 94 | log4perl.appender.A.layout.ConversionPattern = %5rms %-5p A %5Rms %m%n | ||
| 95 | log4perl.appender.A.Threshold = ALL | ||
| 96 | |||
| 97 | log4perl.appender.B = Log::Log4perl::Appender::Screen | ||
| 98 | log4perl.appender.B.layout = Log::Log4perl::Layout::PatternLayout::Elapsed | ||
| 99 | log4perl.appender.B.layout.ConversionPattern = B %5Rms %m%n | ||
| 100 | log4perl.appender.B.Threshold = INFO | ||
| 101 | |||
| 102 | Will produce the following results (output merged side by side manually): | ||
| 103 | |||
| 104 | %r %p Logger %R %m Logger %R %m | ||
| 105 | 44ms INFO A 0ms Start | B 0ms Start | ||
| 106 | 144ms DEBUG A 100ms Pause: 0.1 sec | | ||
| 107 | 1644ms INFO A 1500ms Pause: 1.5 secs | B 1600ms Pause: 1.5 secs | ||
| 108 | 2144ms DEBUG A 500ms Pause: 0.5 sec | | ||
| 109 | 2145ms WARN A 1ms End | B 501ms End | ||
| 110 | |||
| 111 | =head1 RATIONALE | ||
| 112 | |||
| 113 | When a program is taking a long time to execute the first instinct is to check | ||
| 114 | the logs and to find which logging statements where issued after the hotspot. | ||
| 115 | |||
| 116 | The problem is that the time elapsed between to logging events is not directly | ||
| 117 | available in the logs. This value needs to be computed, usually by another | ||
| 118 | program by subtracting the times logged between to consecutive logging events. | ||
| 119 | This can be tedious as log patterns can change and might not always be on a | ||
| 120 | single line. In fact, in a single application different appenders might even | ||
| 121 | use different patterns and different thresholds. | ||
| 122 | |||
| 123 | That's why this Perl module was created. Now the time elapsed between two | ||
| 124 | consecutive log events can be automatically inserted into the log statement. | ||
| 125 | This is now performed by Log4perl and doesn't require an external script in | ||
| 126 | order to compute the values. | ||
| 127 | |||
| 128 | =head1 METHODS | ||
| 129 | |||
| 130 | This module defines the following methods: | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | use 5.006; | ||
| 135 | |||
| 136 | use strict; | ||
| 137 | use warnings; | ||
| 138 | |||
| 139 | use Log::Log4perl; | ||
| 140 | use Log::Log4perl::Util; | ||
| 141 | |||
| 142 | use base qw(Log::Log4perl::Layout::PatternLayout); | ||
| 143 | |||
| 144 | # This functonality was merged in Log4perl 1.25 and causes this module to clash | ||
| 145 | # with those releases. If the code is already there then we simply skip our | ||
| 146 | # current implementation. | ||
| 147 | my $IS_MERGED = ($Log::Log4perl::VERSION >= 1.25); | ||
| 148 | |||
| 149 | # Indicates if Time::HiRes is available | ||
| 150 | my $TIME_HIRES_AVAILABLE = Log::Log4perl::Util::module_available('Time::HiRes'); | ||
| 151 | |||
| 152 | |||
| 153 | our $VERSION = '0.07'; | ||
| 154 | |||
| 155 | =head2 new | ||
| 156 | |||
| 157 | This is the class constructor it ensures that the placeholder C<%R> is handled | ||
| 158 | by Log4perl. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub new { | ||
| 163 | # If the code is already merged in Log4perl we use that version | ||
| 164 | return Log::Log4perl::Layout::PatternLayout->new(@_) if $IS_MERGED; | ||
| 165 | |||
| 166 | my $type = shift; | ||
| 167 | |||
| 168 | # | ||
| 169 | # The placeholder %R must be added through the options. Using add_layout_cspec | ||
| 170 | # doesn't work. So we have to make sure that options are available in order to | ||
| 171 | # add the definition of the placeholder. | ||
| 172 | # | ||
| 173 | |||
| 174 | # Get the options passed, if there are no options provide our own | ||
| 175 | my $options; | ||
| 176 | if (ref $_[0] eq 'HASH') { | ||
| 177 | $options = $_[0]; | ||
| 178 | } | ||
| 179 | else { | ||
| 180 | $options = {}; | ||
| 181 | unshift @_, $options; | ||
| 182 | } | ||
| 183 | |||
| 184 | # Provide our implementation of %R | ||
| 185 | $options->{cspec}{R}{value} = \&compute_elapsed_time; | ||
| 186 | |||
| 187 | # Create the new instance | ||
| 188 | my $self = $type->SUPER::new(@_); | ||
| 189 | |||
| 190 | # Set the start time of the program as the time of the last logging event | ||
| 191 | my $start = $Log::Log4perl::Layout::PatternLayout::PROGRAM_START_TIME; | ||
| 192 | if ($TIME_HIRES_AVAILABLE) { | ||
| 193 | $start = _to_milliseconds(@{ $start }); | ||
| 194 | } | ||
| 195 | $self->{last_time} = $start; | ||
| 196 | |||
| 197 | return $self; | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | =head2 compute_elapsed_time | ||
| 202 | |||
| 203 | Compute the value for the placeholder C<%R>. | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | sub compute_elapsed_time { | ||
| 208 | my ($self, $message, $category, $priority, $caller_level) = @_; | ||
| 209 | |||
| 210 | # Get the time of the current event | ||
| 211 | my $current_time; | ||
| 212 | if ($TIME_HIRES_AVAILABLE) { | ||
| 213 | $current_time = _to_milliseconds(Time::HiRes::gettimeofday()); | ||
| 214 | } | ||
| 215 | else { | ||
| 216 | $current_time = time(); | ||
| 217 | } | ||
| 218 | |||
| 219 | # Get the time of the last event and set this as the last event | ||
| 220 | my $last_time = $self->{last_time}; | ||
| 221 | $self->{last_time} = $current_time; | ||
| 222 | |||
| 223 | # Compute the elapsed time | ||
| 224 | return $current_time - $last_time; | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | # | ||
| 229 | # Returns the number of milliseconds from the given time values. The arguments | ||
| 230 | # are expected to be a two-element array with the seconds and microseconds since | ||
| 231 | # the epoch, as returned by Time::HiRes::gettimeofday(). | ||
| 232 | # | ||
| 233 | sub _to_milliseconds { | ||
| 234 | my ($seconds, $microseconds) = @_; | ||
| 235 | return ($seconds * 1_000) + int($microseconds / 1_000); | ||
| 236 | } | ||
| 237 | |||
| 238 | |||
| 239 | 1; | ||
| 240 | |||
| 241 | =head1 BUGS | ||
| 242 | |||
| 243 | If %d, %r or %R are used simultaneously it could happen that the values don't | ||
| 244 | match within a single logging statement. This is because Log4perl computes the | ||
| 245 | current time for each placeholder and the time can change between the different | ||
| 246 | invocations of the clock's time. | ||
| 247 | |||
| 248 | B<NOTE>: This bugs is fixed in Log::Log4perl 1.25. | ||
| 249 | |||
| 250 | =head1 SEE ALSO | ||
| 251 | |||
| 252 | L<Log::Log4perl::Layout::PatternLayout> distributed with Log::Log4perl 1.25 or | ||
| 253 | higher. | ||
| 254 | |||
| 255 | =head1 AUTHOR | ||
| 256 | |||
| 257 | Emmanuel Rodriguez, E<lt>emmanuel.rodriguez@gmail.comE<gt>. | ||
| 258 | |||
| 259 | =head1 COPYRIGHT AND LICENSE | ||
| 260 | |||
| 261 | Copyright (C) 2008 by Emmanuel Rodriguez | ||
| 262 | |||
| 263 | This library is free software; you can redistribute it and/or modify | ||
| 264 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 265 | at your option, any later version of Perl 5 you may have available. | ||
| 266 | |||
| 267 | =cut |