Test::...og4perl(3pm)
π test log4perl, but make it a whole vibe π
hi bestie π«Ά you've landed on the manual page for Test::...og4perl(3pm) β and yes, the title really is chopped off like that, that's canon, i don't make the rules π. The module living inside it is Test::Log::Log4perl, and it's here to help you test your Log::Log4perl logging. Let's get into it π
πΈ NAME πΈ
Test::Log::Log4perl β test log4perl. that's the name. short, sweet, iconic π
β¨ SYNOPSIS β¨
okay so here's the whole flow, copy-paste ready:
# setup l4p
use Log::Log4Perl;
# do your normal Log::Log4Perl setup here
use Test::Log::Log4perl;
# get the loggers
my $logger = Log::Log4perl->get_logger("Foo::Bar");
my $tlogger = Test::Log::Log4perl->get_logger("Foo::Bar");
# test l4p
Test::Log::Log4perl->start();
# declare we're going to log something
$tlogger->error("This is a test");
# log that something
$logger->error("This is a test");
# test that those things matched
Test::Log::Log4perl->end("Test that that logs okay");
# we also have a simplified version:
{
my $foo = Test::Log::Log4perl->expect(['foo.bar.quux', warn => qr/hello/ ]);
# ... do something that should log 'hello'
}
# $foo goes out of scope; this triggers the test.
π¦ DESCRIPTION π¦
so the whole point of this module is: you want to test that you're logging the right thing with Log::Log4perl. It checks that we get what β and only what β we expect your code to log. no extra, no missing. vibe check passed or failed, nothing in between β¨
The basic process is genuinely simple, fr fr. Inside your test script you grab one or more loggers from Test::Log::Log4perl with the get_logger method, exactly like you would with Log::Log4perl. Those loggers are where you declare what you think the code you're about to test should be logging.
# declare a bunch of test loggers
my $tlogger = Test::Log::Log4perl->get_logger("Foo::Bar");
Then, for every test you wanna run, you start the module up.
# start the test
Test::Log::Log4perl->start();
This is the glow-up moment: it diverts all subsequent attempts Log::Log4perl makes to log stuff, and records them internally instead of passing them through to the Log4perl appenders like normal. it's giving interception π
Then you declare, using the loggers we made earlier, what you hope Log4perl is gonna be asked to log. Same syntax as Test::Log::Log4perl uses β except if you want, you can throw regular expressions in there too π
$tlogger->debug("fish");
$tlogger->warn(qr/bar/);
Then you run the code you're testing, obviously.
# call some code that hopefully will call the log4perl methods
# 'debug' with "fish" and 'warn' with something that contains 'bar'
some_code();
And finally you tell Test::Log::Log4perl that you're done and it should do the comparisons. the moment of truth π
# start the test
Test::Log::Log4perl->end("test name");
π Methods π
get_logger($category)β hands you a brand new instance ofTest::Log::Log4perlthat you can use to log expected messages in the category you passed in. slay.Test::Log::Log4perl->expect(%start_args, ['dotted.path', 'warn' => qr(this), 'warn' => qr(that)], ..)β class convenience method. you use it like this:
{ # start local scope
my $foo = Test::Log::Log4perl->expect(['foo.bar.quux', warn => qr/hello/ ]);
# ... do something that should log 'hello'
} # $foo goes out of scope; this triggers the test.
startβ class method. Start logging. When you call it, it temporarily redirects all logging from the standard logging locations over to the internal logging routine, and it stays like that untilendgets called. It takes parameters that change the behavior of this test (and only this test). more on that below πdebug(@what)info(@what)warn(@what)error(@what)fatal(@what)β instance methods. String of things that you're expecting to log, at the level you're expecting them, in what class.end()end($name)β Ends the test and compares what we've got against what we expected. Also switches logging back from being captured to going wherever it was originally directed in the config. bye capture mode, hello normalcy π
π Ignoring All Logging Messages π
Sometimes you're testing something that generates a whole load of spurious log messages that you just want to ignore without testing their contents β and you do not want to have to go reconfigure your log file. The simplest way to do it is:
use Test::Log::Log4perl;
Test::Log::Log4perl->suppress_logging;
All logging functions stop working. and this is important bestie, listen: Do not alter the Logging classes (for example, by changing the config file and using Log4perl's init_and_watch functionality) after this call has been made. just... don't. π
This function will be effectively a no-op if the environmental variable NO_SUPPRESS_LOGGING is set to a true value (so if your code is behaving weirdly you can turn all the logging back on from the command line without changing any of the code). lowkey the emergency exit door πͺ
π Selectively Ignoring Logging Messages By Priority π
Completely ignoring all messages is a bad idea, ngl. What you probably want is to ignore some of the trivial messages you don't care about, and then just test that there aren't any unexpected messages of a set priority.
You can temporarily ignore any logging messages that are made by passing parameters to the start routine:
# for this test, just ignore DEBUG, INFO, and WARN
Test::Log::Log4perl->start( ignore_priority => "warn" );
# you can use the levels constants to do the same thing
use Log::Log4perl qw(:levels);
Test::Log::Log4perl->start( ignore_priority => $WARN );
You might want to ignore all logging events at all (this can be used as a quick way to not test the actual log messages, but just ignore the output):
# for this test, ignore everything
Test::Log::Log4perl->start( ignore_priority => "everything" );
# contary to readability, the same thing (try not to write this)
use Log::Log4perl qw(:levels);
Test::Log::Log4perl->start( ignore_priority => $OFF );
Or you might want to not ignore anything (which is the default, unless you've played with the method calls mentioned below):
# for this test, ignore nothing
Test::Log::Log4perl->start( ignore_priority => "nothing" );
# contary to readability, the same thing (try not to write this)
use Log::Log4perl qw(:levels);
Test::Log::Log4perl->start( ignore_priority => $ALL );
You can also permanently affect what things are ignored with the ignore_priority method call. This persists between tests and isn't automatically reset after each call to start. so it's the main character, it stays β¨
# ignore DEBUG, INFO and WARN for all future tests
Test::Log::Log4perl->ignore_priority("warn");
# you can use the levels constants to do the same thing
use Log::Log4perl qw(:levels);
Test::Log::Log4perl->ignore_priority($WARN);
# ignore everything (no log messages will be logged)
Test::Log::Log4perl->ignore_priority("everything");
# ignore nothing (messages will be logged reguardless of priority)
Test::Log::Log4perl->ignore_priority("nothing");
Obviously, you may temporarily override whatever permanent. π
π BUGS π
- Logging methods don't return the number of appenders they've written to (or rather, they do, as it's always zero.) yeah. it's always zero. π
- Changing the config file (if you're watching it) while this is testing / suppressing everything will probably break everything. As will creating new appenders, etc... proceed with caution bestie, this one's giving chaos π
π AUTHOR π
Chia-liang Kao <clkao@clkao.org>
Mark Fowler <mark@twoshortplanks.com>
π COPYRIGHT π
Copyright 2010 Chia-liang Kao all rights reserved.
Copyright 2005 Fotango Ltd all rights reserved.
Licensed under the same terms as Perl itself.
and that's the whole page, bestie π«Ά this was Test::...og4perl(3pm), rendered for you by perl v5.36.0 on 2022-12-08 β go forth and test your logs, you absolute icon πβ¨π
π¦ where this page comes from
- debian bookworm: libtest-log-log4perl-perl 0.32-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtest-log-log4perl-perl 0.32-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtest-log-log4perl-perl 0.32-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtest-log-log4perl-perl 0.32-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtest-log-log4perl-perl 0.32-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms