Chandra
view release on metacpan or search on metacpan
t/58_log_edge.t view on Meta::CPAN
use File::Temp qw(tempdir);
use_ok('Chandra::Log');
my $TMPDIR = tempdir(CLEANUP => 1);
sub capture_log {
my ($log_sub) = @_;
my $file = "$TMPDIR/cap_$$.txt";
open my $save, '>&', \*STDOUT or die "dup: $!";
open STDOUT, '>', $file or die "redirect: $!";
$log_sub->();
STDOUT->flush();
open STDOUT, '>&', $save or die "restore: $!";
open my $fh, '<', $file or die "read: $!";
local $/;
my $content = <$fh>;
close $fh;
unlink $file;
return $content;
}
# ---- undef message ----
{
my @captured;
my $log = Chandra::Log->new(
output => { callback => sub { push @captured, $_[0] } },
);
$log->info(undef);
is($captured[0]{message}, '', 'undef message becomes empty string');
}
# ---- No data argument ----
{
my @captured;
my $log = Chandra::Log->new(
output => { callback => sub { push @captured, $_[0] } },
);
$log->info('no data');
ok(!exists $captured[0]{data} || !defined $captured[0]{data},
'no data when not provided');
}
# ---- Empty string message ----
{
my @captured;
my $log = Chandra::Log->new(
output => { callback => sub { push @captured, $_[0] } },
);
$log->info('');
is($captured[0]{message}, '', 'empty string message preserved');
}
# ---- Unicode message ----
{
my @captured;
my $log = Chandra::Log->new(
output => { callback => sub { push @captured, $_[0] } },
);
$log->info("Hello \x{263A} world");
like($captured[0]{message}, qr/Hello.*world/, 'unicode message logged');
}
# ---- Very long message ----
{
my @captured;
my $log = Chandra::Log->new(
output => { callback => sub { push @captured, $_[0] } },
);
my $long = 'x' x 100_000;
$log->info($long);
is(length($captured[0]{message}), 100_000, 'long message preserved');
}
# ---- Numeric message ----
{
my @captured;
my $log = Chandra::Log->new(
output => { callback => sub { push @captured, $_[0] } },
);
$log->info(42);
is($captured[0]{message}, '42', 'numeric message stringified');
}
# ---- Deep nested data ----
{
my @captured;
my $log = Chandra::Log->new(
output => { callback => sub { push @captured, $_[0] } },
);
my $deep = { a => { b => { c => { d => [1, 2, 3] } } } };
$log->info('deep', $deep);
is($captured[0]{data}{a}{b}{c}{d}[1], 2, 'deep nested data preserved');
}
# ---- Data as arrayref ----
{
my @captured;
my $log = Chandra::Log->new(
output => { callback => sub { push @captured, $_[0] } },
);
$log->info('arr data', [1, 2, 3]);
is(ref $captured[0]{data}, 'ARRAY', 'arrayref data preserved');
is($captured[0]{data}[1], 2, 'arrayref data values');
}
# ---- Rapid logging ----
{
my $count = 0;
my $log = Chandra::Log->new(
output => { callback => sub { $count++ } },
);
$log->info("msg $_") for 1..1000;
is($count, 1000, 'rapid logging: 1000 messages');
}
# ---- Multiple file writes don't corrupt ----
{
my $dir = tempdir(CLEANUP => 1);
my $file = "$dir/rapid.log";
( run in 1.823 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )