带有符号 link 的 Perl File::Tail
Perl File::Tail with a symbolic link
背景
我正在使用 File::Tail 来跟踪日志文件符号 link。符号 link 在午夜后更新以包含一个新的日期戳,不幸的是我的脚本在符号 link 更新后没有拖尾新文件。否则,我的脚本会按预期工作。
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Data::Dumper;
use charnames':full';
use Cwd 'abs_path';
use File::Tail;
my $symlink = sub {
my($log) = '/home/user/log';
};
my $file=File::Tail->new(
name=>&$symlink,
ignore_nonexistant=>1,
tail=>0,
interval=>0,
maxinterval=>1,
name_changes=>\&$symlink
) || warn $!;
print Dumper $file;
while (defined($_=$file->read)) {
# do a bunch of stuff;
}
问题
如何让 perl 遵循更新的符号 link?
来自 File::Tail documentation:
name_changes
Some logging systems change the name of the file they are writing to, sometimes to include a date, sometimes a sequence number,
sometimes other, even more bizarre changes.
Instead of trying to implement various clever detection methods, File::Tail will call the code reference defined in name_changes. The
code reference should return the string which is the new name of the
file to try opening.
Note that if the file does not exist, File::Tail will report a fatal error (unless ignore_nonexistant has also been specified).
所以您的代码引用应该return 新 文件的名称,根据您的问题,该文件中有一个日期字符串。也许这样的事情会奏效:
use Path::Tiny; # file system agnostic path utilty
use Time::Piece; # data utilties
my $symlink = sub {
my $time = localtime; # a Time::Piece object
return path(
'/home/user/log',
join('', $time->year, $time->mon, $time->mday),
)->canonpath;
};
今天这个子会return:/home/user/log20151112
我从 sub
中遗漏了一个 return
my $symlink = sub {
my($log) = '/home/user/log';
return $log;
};
现在完美运行!
背景
我正在使用 File::Tail 来跟踪日志文件符号 link。符号 link 在午夜后更新以包含一个新的日期戳,不幸的是我的脚本在符号 link 更新后没有拖尾新文件。否则,我的脚本会按预期工作。
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Data::Dumper;
use charnames':full';
use Cwd 'abs_path';
use File::Tail;
my $symlink = sub {
my($log) = '/home/user/log';
};
my $file=File::Tail->new(
name=>&$symlink,
ignore_nonexistant=>1,
tail=>0,
interval=>0,
maxinterval=>1,
name_changes=>\&$symlink
) || warn $!;
print Dumper $file;
while (defined($_=$file->read)) {
# do a bunch of stuff;
}
问题
如何让 perl 遵循更新的符号 link?
来自 File::Tail documentation:
name_changes
Some logging systems change the name of the file they are writing to, sometimes to include a date, sometimes a sequence number, sometimes other, even more bizarre changes.
Instead of trying to implement various clever detection methods, File::Tail will call the code reference defined in name_changes. The code reference should return the string which is the new name of the file to try opening.
Note that if the file does not exist, File::Tail will report a fatal error (unless ignore_nonexistant has also been specified).
所以您的代码引用应该return 新 文件的名称,根据您的问题,该文件中有一个日期字符串。也许这样的事情会奏效:
use Path::Tiny; # file system agnostic path utilty
use Time::Piece; # data utilties
my $symlink = sub {
my $time = localtime; # a Time::Piece object
return path(
'/home/user/log',
join('', $time->year, $time->mon, $time->mday),
)->canonpath;
};
今天这个子会return:/home/user/log20151112
我从 sub
中遗漏了一个 returnmy $symlink = sub {
my($log) = '/home/user/log';
return $log;
};
现在完美运行!