在 Perl 中如何将多个日志文件视为一个虚拟文件?

发布于 2024-09-19 19:20:41 字数 512 浏览 2 评论 0原文

我在日志目录中有多个访问日志,遵循以下命名约定:

access.log.1284642120
access.log.1284687600
access.log.1284843260

基本上,日志由 Apache 每天“轮换”,因此可以按顺序对它们进行排序。

我试图“依次阅读它们”,以便将它们视为一个日志文件。

my @logs = glob('logs/access.log.*');

上面的代码将全局所有日志,但我不确定:

  • 日志将按字母顺序组织什么顺序?
  • 如果我想查看“唯一IP的最新访问时间”,我该怎么做?

我有一个 Perl 脚本,可以读取单个访问日志并轻松检查(我的算法是有一个大哈希,它使用 IP 地址作为键,访问时间作为值,然后继续将键/值对推送到它...)。 但我不想只是为了这一过程而将所有访问文件合并到一个临时文件中。

有什么建议吗?非常感谢。

I've got multiple access logs in the logs directory, following the naming convention below:

access.log.1284642120
access.log.1284687600
access.log.1284843260

Basically, the logs are "rotated" by Apache per day, so they can be sorted in order.

I am trying to "read them one after another", so that they can be treated as one log file.

my @logs = glob('logs/access.log.*');

The above code will glob all the logs, but I am not sure:

  • In which order will the logs be organized, alphabetically?
  • if I want to check "the latest access time from an unique IP", how could I do this?

I have a Perl script that can read a single access log and check this easily (my algorithm is to have a big hash which uses IP address as the key and the access time as the value, and just keep pushing key/value pairs to it...).
But I don't want to just merge all access files into one temporary file just for this process.

Any suggestions? Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你的占有欲 2024-09-26 19:20:41

如果您想确保特定的顺序,请自己对其进行排序,即使只是为了确保结果正确:

 my @files = sort { ... } glob( ... );

在这种情况下,除了特定数字外,文件名都相同,您可能不需要排序块:

 my @files = sort glob( ... );

要将它们作为一个 über 文件读取,我喜欢使用 local @ARGV 这样我就可以使用菱形运算符,这实际上就是神奇的 ARGV 文件句柄。当到达 @ARGV 中一个文件的末尾时,它会转到下一个文件。这通过在程序内分配给 @ARGV 来伪造在命令行上指定所有文件:

 {
 local @ARGV = sort { ... } glob( ... );

 while( <> ) {
      ...;
      }
 }

如果您需要知道当前正在处理的文件,请查看 $ARGV

如果您需要更奇特的东西,您可能不得不诉诸蛮力。

If you want to ensure a particular order, sort it yourself, even if just to assure yourself that it will come out right:

 my @files = sort { ... } glob( ... );

In this case, where the filenames are all the same except for the particular digits, you might not need the sort block:

 my @files = sort glob( ... );

To read them as one über-file, I like to use a local @ARGV so I can use the diamond operator, which is really just the magic ARGV filehandle. When it gets to the end of one file in @ARGV, it moves on to the next. This fakes specifying all the files on the command line by assigning to @ARGV inside the program:

 {
 local @ARGV = sort { ... } glob( ... );

 while( <> ) {
      ...;
      }
 }

If you need to know the file you are currently processing, look in $ARGV.

If you need something more fancy, you might have to resort to brute force.

塔塔猫 2024-09-26 19:20:41

在 Unix-y 环境中,您可以利用 shell 将文件分组在一起:

my @files = glob("$dir/access.log.*");
open my $one_big_logfile, "-|", "cat @files" or die ...;
while (<$one_big_logfile>) {
   ...
}

In a Unix-y environment, you can leverage the shell to group your files together:

my @files = glob("$dir/access.log.*");
open my $one_big_logfile, "-|", "cat @files" or die ...;
while (<$one_big_logfile>) {
   ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文