如何将 Neo4j 日志从基本 UTC 转换为本地时区
随着 Neo4j 3.3.1 的引入,可以通过实现 dbms.logs.timezone
在 $NEO4J_HOME/logs/* 中以 UTC 或 SYSTEM 时区表示日期时间戳。
但是,对于之前的版本,所有 Neo4j 日志都会在每行开头加上格式为
<YYYY-MM-DD HH24:MM:SS.MMM+0000>
例如
2016-12-01 15:51:00.222+0000 INFO [o.n.k.i.DiagnosticsManager] --- INITIALIZED diagnostics START ---
其中上面的 +0000 表示日期/时间以 UTC 格式表示。在集群定义为包含不同时区的成员时,以 UTC 格式记录日志有助于分析。但是,当集群成员位于同一个时区或您运行单个实例时,您可能希望以本地时区记录日志。有一个待定的产品改进要求根据时区配置日期/时间字符串。
在没有此功能的情况下,可以使用以下 Perl 脚本将任何文件从 UTC 时区转换为运行 Perl 脚本的机器的时区。
对于大多数 Unix 实现,要确定时区,如果运行
$ date
这将返回类似于
Mon Jan 16 14:38:06 EST 2017
的输出,表示 EST 时区。
要将日志从 UTC 转换为 EST,请运行
$ ./utc.pl debug.log > debug.EST.log
要安装脚本,请将以下几行从这里复制到 Linux 服务器上的名为 utc.pl
的文件中。
#!/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) );
}
通过运行以下命令使脚本可执行
$ chmod +x utc.pl
以以下方式运行脚本
$ ./utc.pl <log file>
将 <日志文件>
替换为文件名。
使用 Neo4j 3.3,由于 PR 10127,可以通过参数 dbms.logs.timezone
配置时间戳时区。
此页面对您有帮助吗?