From: Mark B. <md...@ji...> - 2002-09-08 18:17:09
|
Hi. I noticed in Log::Log4perl::Layout::PatternLayout the comment, # TODO: 'd', 't', 'x', 'X' # lib4j PatternLayout as documented in # http://jakarta.apache.org/log4j/docs/api/org/apache/log4j/PatternLayout.html Is anyone currently working in this area? In particular, is "%d{}" support in the CVS tree (I only have release 0.22 at my disposal now). I took a quick hack at it (context diff is appended). It basically adds in the dedicated data formatters, For better results it is recommended to use the log4j date formatters. These can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" for specifying AbsoluteTimeDateFormat, DateTimeDateFormat and respectively ISO8601DateFormat. For example, %d{ISO8601} or %d{ABSOLUTE}. and uses strftime(3) formatting otherwise. If (a) nobody is currently working on this; and (b) this approach is acceptable I can make a proper diff with test cases, more error handling and some documentation. But I didn't want to devote a lot fo time to this if either (a) or (b) are untrue. Thanks. -- -mb- ======================================== *** PatternLayout.pm.orig Sat Jul 20 12:16:36 2002 --- PatternLayout.pm Sun Sep 8 11:15:37 2002 *************** *** 132,143 **** $info{r} = int((tv_interval ( $PROGRAM_START_TIME ))*1000); if($self->{info_needed}->{d}) { ! my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = ! localtime(time); ! $info{d} = sprintf "%d/%02d/%02d %02d:%02d:%02d", ! $year + 1900, $mon+1, $mday, ! $hour, $min, $sec; } # As long as they're not implemented yet .. --- 132,144 ---- $info{r} = int((tv_interval ( $PROGRAM_START_TIME ))*1000); if($self->{info_needed}->{d}) { ! # Formats a Date in the format "YYYY-mm-dd HH:mm:ss,SSS" for example ! # "1999-11-27 15:49:37,459". [default format if no curlies] ! my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = ! localtime(time); ! $info{d} = sprintf "%04d-%02d-%02d %02d:%02d:%02d", ! $year + 1900, $mon+1, $mday, $hour, $min, $sec; } # As long as they're not implemented yet .. *************** *** 170,175 **** --- 171,178 ---- $data = shrink_category($data, $curlies); } elsif($ops eq "C") { $data = shrink_category($data, $curlies); + } elsif($ops eq "d") { + $data = format_time($data, $curlies); } return $data; *************** *** 188,193 **** --- 191,241 ---- } return $category; + } + + ################################################## + sub format_time { + ################################################## + use POSIX qw(strftime mktime); + + my($time, $fmt) = @_; + + my $date_time = "N/A"; + + if ($fmt =~ /ISO8601/) { + $date_time = $time; # default (already formatted) + } elsif ($time =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/) { + my ($year,$mon,$mday,$hour,$min,$sec) = ($1,$2,$3,$4,$5,$6); + if ($fmt =~ /ABSOLUTE/) { + # Formats a Date in the format "HH:mm:ss,SSS" for example, + # "15:49:37,459". + + $date_time = sprintf "%02d:%02d:%02d", $hour, $min, $sec; + + } elsif ($fmt =~ /DATE/) { + # Formats a Date in the format "dd MMM YYYY HH:mm:ss,SSS" for example, + # "06 Nov 1994 15:49:37,459". + + $date_time = sprintf "%02d %3s %04d %02d:%02d:%02d", + $mday, + ( qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) )[$mon-1], + $year, $hour, $min, $sec; + } else { + # simple strftime(3) formatting for now + ### TODO: SimpleDateFormat support: + # http://java.sun.com/j2se/1.3/docs/api/java/text/SimpleDateFormat.html + + my $time = mktime($sec,$min,$hour-1,$mday,$mon-1,$year-1900); + $date_time = strftime($fmt, localtime($time)); + + } + + } else { + warn "INTERNAL ERROR"; + + } + + return $date_time; } 1; |