官网是使用新的perl脚本来完成的
1,新建 perl-log.pl脚本
2,脚本内容
#!/usr/bin/perl -w
use strict;
use Time::Local; #needed for timegm()
my $file = $ARGV[0] or die "USAGE: $0 <filename>\n";
open(my $data, '<', $file) or die "Could not open '$file' $!\n";
while (my $line = <$data>) {
# where a line might start as
# 2017-01-11 23:22:28.372+0000 INFO ... .... ....
chomp $line;
# check to make sure the line begins with a YYYY-MM-DD HH
if ( $line =~ /\d\d\d\d-\d\d-\d\d \d\d/ ) {
my $newstring = UTC2LocalString($line);
print "$newstring\n";
}
else {
print "$line\n";
}
}
sub UTC2LocalString
{
# below attributed to Marshall at http://www.perlmonks.org/?node_id=873435
my $t = shift;
my ($datehour, $rest) = split(/:/,$t,2);
# $datehour will represent YYYY-MM-DD HH (i.e. 2017-01-14 12)
# $rest represents the rest of the line after
# and this will reassemble and return $datehour (adjusted) + $rest
my ($year, $month, $day, $hour) =
$datehour =~ /(\d+)-(\d\d)-(\d\d)\s+(\d\d)/;
# proto: $time = timegm($sec,$min,$hour,$mday,$mon,$year);
my $epoch = timegm (0,0,$hour,$day,$month-1,$year);
# proto: ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
# localtime(time);
my ($lyear,$lmonth,$lday,$lhour,$isdst) =
(localtime($epoch))[5,4,3,2,-1];
$lyear += 1900; # year is 1900 based
$lmonth++; # month number is zero based
#print "isdst: $isdst\n"; #debug flag day-light-savings time
return ( sprintf("%04d-%02d-%02d %02d:%s",
$lyear,$lmonth,$lday,$lhour,$rest) );
}
3,赋执行的权限,使用的时候 ./perl-log.pl <日志地址>
4,效果
以前的
修改后的(虽然时区显示的有问题,但是时间已经改过来了。不影响观看)