Log-Fine
view release on metacpan or search on metacpan
t/13-formatter-template.t view on Meta::CPAN
custom_placeholders => {
foobar => \&countplus,
FooBar => \&countplus,
});
};
ok($@ =~ /^Duplicate placeholder/);
# time
my $log_time =
Log::Fine::Formatter::Template->new(template => "%%TIME%%",
timestamp_format => "%Y%m");
isa_ok($log_time, "Log::Fine::Formatter::Template");
can_ok($log_time, "name");
can_ok($log_time, "format");
ok($log_time->name() =~ /\w\d+$/);
# Note we test time first to avoid a possible race condition
# that would occur at the end of every month.
# Validate
ok($log_time->format(INFO, $msg, 0) eq strftime("%Y%m", localtime(time)));
ok($log_level->format(INFO, $msg, 0) eq "INFO");
ok($log_msg->format(INFO, $msg, 0) eq $msg);
# Validate call within main
ok($log_package->format(INFO, $msg, 0) =~ /^\[.*?\] INFO main main $msg/);
ok($log_filename->format(INFO, $msg, 0) =~ /^\[.*?\] INFO .*?\.t\:\d+ $msg/);
#printf STDERR "\n%s\n", $log_package->format(CRIT, $msg, 0);
#printf STDERR "%s\n", $log_filename->format(DEBG, $msg, 0);
# Validate call within function
ok(myfunc($log_package, $msg) =~ /^\[.*?\] INFO main main\:\:myfunc $msg/);
ok(myfunc($log_filename, $msg) =~ /^\[.*?\] INFO .*?\.t\:\d+ $msg/);
#printf STDERR "%s\n", myfunc($log_package, $msg);
#printf STDERR "%s\n", myfunc($log_filename, $msg);
# Validate call within Package
ok(This::Test::doIt($log_package, $msg) =~ /^\[.*?\] WARN This\:\:Test This\:\:Test\:\:doIt $msg/);
ok(This::Test::doIt($log_filename, $msg) =~ /^\[.*?\] WARN .*?\.t\:\d+ $msg/);
#printf STDERR "%s\n", This::Test::doIt($log_package, $msg);
#printf STDERR "%s\n", This::Test::doIt($log_filename, $msg);
ok($log_longhost->format(INFO, $msg, 0) =~ /$hostname/);
ok($log_shorthost->format(INFO, $msg, 0) =~ /\w/);
# Note we test custom templates twice to see if dynamic
# content changed
ok($log_custom->format(INFO, $msg, 0) =~ /^$counter/);
ok($log_custom->format(INFO, $msg, 0) =~ /^$counter/);
SKIP: {
skip "Cannot accurately test user and group placeholders under MSWin32", 2
if ($^O =~ /MSWin32/);
ok($log_user->format(INFO, $msg, 0) eq getpwuid($<));
ok($log_group->format(INFO, $msg, 0) eq getgrgid((split(" ", $())[0]));
}
# Now test a combination string for good measure
my $log_basic =
Log::Fine::Formatter::Template->new(template => "[%%time%%] %%level%% %%msg%%",
timestamp_format => Log::Fine::Formatter->LOG_TIMESTAMP_FORMAT);
isa_ok($log_basic, "Log::Fine::Formatter::Template");
can_ok($log_basic, "name");
can_ok($log_basic, "format");
ok($log_basic->name() =~ /\w\d+$/);
ok($log_basic->format(INFO, $msg, 1) =~ /^\[.*?\] \w+ $msg/);
# Grab a logger
my $logger = Log::Fine->logger("formatlogger0");
isa_ok($logger, "Log::Fine::Logger");
can_ok($logger, "name");
can_ok($logger, "registerHandle");
can_ok($logger, "log");
ok($logger->name() =~ /\w\d+$/);
# If logfile already exists, hose it
unlink $logfile if (-e $logfile);
my $handle =
Log::Fine::Handle::File->new(
file => $logfile,
autoflush => 1,
formatter =>
Log::Fine::Formatter::Template->new(
template => "[%%TIME%%] %%LEVEL%% %%SUBROUT%%:%%LINENO%% %%MSG%%\n",
timestamp_format => "%H:%M:%S"
));
isa_ok($handle, "Log::Fine::Handle::File");
can_ok($handle, "name");
ok($handle->name() =~ /\w\d+$/);
$logger->registerHandle($handle);
# Output
$logger->log(DEBG, $msg);
logFunc($logger, $msg);
This::Test::doFunc($logger, $msg);
ok(-e $logfile);
# Check contents
my $fh = FileHandle->new($logfile);
my $logmain = <$fh>;
my $logfunc = <$fh>;
my $logpack = <$fh>;
$fh->close();
# Validate
ok($logmain =~ /^\[.*?\] DEBG main\:\d+ $msg/);
( run in 1.743 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )