如何在 Perl 中将 log4j 时间戳转换为毫秒?
我的 log4j 日志包含以下格式的时间戳:
2009-05-10 00:48:41,905
我需要使用以下 gawk 函数将其以 perl 格式转换为自纪元以来的毫秒数,在本例中为 124189673005。 我如何在 Perl 中做到这一点?
我对 Perl 的经验很少或没有,所以如果有人可以发布执行此操作的完整脚本,我将不胜感激
function log4jTimeStampToMillis(log4jts) {
# log4jts is of the form 2009-03-02 20:04:13,474
# extract milliseconds that is after the command
split(log4jts, tsparts, ",");
millis = tsparts[2];
# remove - : from tsstr
tsstr = tsparts[1];
gsub("[-:]", " ", tsstr);
seconds = mktime(tsstr);
print log4jts;
return seconds * 1000 + millis;
}
The log4j logs I have contain timestamps in the following format:
2009-05-10 00:48:41,905
I need to convert it in perl to millseconds since epoch, which in this case would be 124189673005, using the following gawk function. How do I do it in perl?
I have little or no experience in perl, so appreciate if someone can post an entire script that does this
function log4jTimeStampToMillis(log4jts) {
# log4jts is of the form 2009-03-02 20:04:13,474
# extract milliseconds that is after the command
split(log4jts, tsparts, ",");
millis = tsparts[2];
# remove - : from tsstr
tsstr = tsparts[1];
gsub("[-:]", " ", tsstr);
seconds = mktime(tsstr);
print log4jts;
return seconds * 1000 + millis;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽管我几乎总是告诉人们为此使用 CPAN 中众多优秀模块之一,但它们中的大多数确实有一个主要缺点 - 速度。 如果您实时解析大量日志文件,这有时可能会成为一个问题。 在这些情况下,自己推出通常是更合适的解决方案,但有许多陷阱和细微差别必须考虑和妥善处理。 因此,优先使用由其他人编写的已知正确、经过验证、可靠的模块。 :)
然而,在我考虑上面的建议之前,我查看了你的代码,并在我的脑海中将其转换为 perl...因此,这里或多或少将你的 gawk 代码直接转换为 perl。 我尝试尽可能简单地编写它,以便突出显示在 Perl 中手动处理日期和时间的一些更微妙的部分。
我的代码和你的代码之间的一个重要区别 - 我的 log4jTimeStampToMillis 子例程采用两个参数:
当然,您可以添加代码来检测该时间是否属于夏令时并自动调整,但我试图保持简单。 :)
注意:如果取消注释标记为 DEBUG 的行,请确保添加“use Data::Dumper;” 在程序中的该行之前,这样它就可以工作。
以下是如何测试该子例程的示例:
Though I almost always tell people to go use one of the many excellent modules from the CPAN for this, most of them do have one major drawback - speed. If you're parsing a large number of log files in real-time, that can sometimes be an issue. In those cases, rolling your own can often be a more suitable solution, but there are many pitfalls and nuances that must be considered and handled properly. Hence the preference for using a known-correct, proven, reliable module written by somebody else. :)
However, before I even considered my advice above, I looked at your code and had converted it to perl in my head... therefore, here is a more-or-less direct conversion of your gawk code into perl. I've tried to write it as simply as possible, so as to highlight some of the more delicate parts of dealing with dates and times in perl by hand.
One important difference between my code and yours - my log4jTimeStampToMillis subroutine takes two parameters:
Of course, you could just add code to detect if that time falls in DST or not and adjust automatically, but I was trying to keep it simple. :)
NOTE: If you uncomment the line marked DEBUG, make sure to add "use Data::Dumper;" before that line in your program so it will work.
Here's an example of how you could test that subroutine:
您应该利用出色的 DateTime 包,特别是使用 日期时间::格式::Strptime :
这使您免去了自己计算 DST 的麻烦(尽管您必须通过
time_zone
参数将服务器的 TZ 传递给 Strptime)。 如果它变得相关的话,它还可以让你免于处理所有事情(我相信它会)。You should take advantage of the great DateTime package, specifically use DateTime::Format::Strptime:
This saves you the pain of figuring out DST yourself (although you'll have to pass your server's TZ to Strptime via the
time_zone
parameter). It also saves you from dealing with leap everything if it becomes relevant (and I'm sure it will).还没有使用过它,但您可能想查看 Time ::解析日期。
Haven't used it, but you might want to check out Time::ParseDate.