File::Stamped 时间戳log文件
log类库的工具。可以使用myapp.log.%Y%m%d.log 这种形式的log文件
语法:
use File::Stamped; my $fh = File::Stamped->new(pattern => '/var/log/myapp.log.%Y%m%d.txt'); $fh->print("OK\n"); # with Log::Minimal use Log::Minimal; my $fh = File::Stamped->new(pattern => '/var/log/myapp.log.%Y%m%d.txt'); local $Log::Minimal::PRINT = sub { my ( $time, $type, $message, $trace) = @_; print {$fh} "$time [$type] $message at $trace\n"; };
方法:
my $fh = File::Stamped->new(%args);
方法创建一个实例,参数如下:
你需要指定一个模式或者回调函数
模式: 字符串
格式可以看The format is POSIX::strftime(), see also POSIX.
回调模式 : CodeRef
my $pattern = '/path/to/myapp.log.%Y%m%d.log'; my $f = File::Stamped->new(callback => sub { my $file_stamped = shift; local $_ = $pattern; s/!!/$$/ge; $_ = POSIX::strftime($_, localtime()); return $_; });
close_after_write : Bool
Default value is 1.
iomode: Str
打开文件的模式
Default value is '>>:utf8'. 默认是utf8
autoflush: Bool
属性改变$|.
rotationtime: Int
The time between log file generates in seconds. Default value is 1.
$fh->print($str: Str)
打印字符串到文件