| 1 |
NAME |
| 2 |
Test::Log4perl - test log4perl |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use Test::More tests => 1; |
| 6 |
|
| 7 |
# setup l4p |
| 8 |
use Log::Log4Perl; |
| 9 |
# do your normal Log::Log4Perl setup here |
| 10 |
use Test::Log4perl; |
| 11 |
|
| 12 |
# get the loggers |
| 13 |
my $logger = Log::Log4perl->get_logger("Foo::Bar"); |
| 14 |
my $tlogger = Test::Log4perl->get_logger("Foo::Bar"); |
| 15 |
|
| 16 |
# test l4p |
| 17 |
Test::Log4perl->start(); |
| 18 |
|
| 19 |
# declare we're going to log something |
| 20 |
$tlogger->error("This is a test"); |
| 21 |
|
| 22 |
# log that something |
| 23 |
$logger->error("This is a test"); |
| 24 |
|
| 25 |
# test that those things matched |
| 26 |
Test::Log4perl->end("Test that that logs okay"); |
| 27 |
|
| 28 |
# we also have a simplified version: |
| 29 |
{ |
| 30 |
my $foo = Test::Logger->expect(['foo.bar.quux', warn => qr/hello/ ]); |
| 31 |
# ... do something that should log 'hello' |
| 32 |
} |
| 33 |
# $foo goes out of scope; this triggers the test. |
| 34 |
|
| 35 |
DESCRIPTION |
| 36 |
This module can be used to test that you're logging the right thing with |
| 37 |
Log::Log4perl. It checks that we get what, and only what, we expect |
| 38 |
logged by your code. |
| 39 |
|
| 40 |
The basic process is very simple. Within your test script you get one or |
| 41 |
more loggers from Test::Log4perl with the "get_logger" method just like |
| 42 |
you would with Log::Log4perl. You're going to use these loggers to |
| 43 |
declare what you think the code you're going to test should be logging. |
| 44 |
|
| 45 |
# declare a bunch of test loggers |
| 46 |
my $tlogger = Test::Log4perl->get_logger("Foo::Bar"); |
| 47 |
|
| 48 |
Then, for each test you want to do you need to start up the module. |
| 49 |
|
| 50 |
# start the test |
| 51 |
Test::Log4perl->start(); |
| 52 |
|
| 53 |
This diverts all subsequent attempts Log::Log4perl makes to log stuff |
| 54 |
and records them internally rather than passing them though to the |
| 55 |
Log4perl appenders as normal. |
| 56 |
|
| 57 |
You then need to declare with the loggers we created earlier what we |
| 58 |
hope Log4perl will be asked to log. This is the same syntax as |
| 59 |
Test::Log4perl uses, except if you want you can use regular expressions: |
| 60 |
|
| 61 |
$tlogger->debug("fish"); |
| 62 |
$tlogger->warn(qr/bar/); |
| 63 |
|
| 64 |
You then need to run your code that you're testing. |
| 65 |
|
| 66 |
# call some code that hopefully will call the log4perl methods |
| 67 |
# 'debug' with "fish" and 'warn' with something that contains 'bar' |
| 68 |
some_code(); |
| 69 |
|
| 70 |
We finally need to tell Test::Log4Perl that we're done and it should do |
| 71 |
the comparisons. |
| 72 |
|
| 73 |
# start the test |
| 74 |
Test::Log4perl->end("test name"); |
| 75 |
|
| 76 |
Methods |
| 77 |
get_logger($category) |
| 78 |
Returns a new instance of Test::Logger that can be used to log |
| 79 |
expected messages in the category passed. |
| 80 |
|
| 81 |
Test::Logger->expect(['dotted.path', 'warn' => qr'this', 'warn' => |
| 82 |
qr'that'], ..) |
| 83 |
Class convenience method. Used like this: |
| 84 |
|
| 85 |
{ # start local scope |
| 86 |
my $foo = Test::Logger->expect(['foo.bar.quux', warn => qr/hello/ ]); |
| 87 |
# ... do something that should log 'hello' |
| 88 |
} # $foo goes out of scope; this triggers the test. |
| 89 |
|
| 90 |
start |
| 91 |
Class method. Start logging. When you call this method it temporarly |
| 92 |
redirects all logging from the standard logging locations to the |
| 93 |
internal logging routine until end is called. Takes parameters to |
| 94 |
change the behavior of this (and only this) test. See below. |
| 95 |
|
| 96 |
debug(@what) |
| 97 |
info(@what) |
| 98 |
warn(@what) |
| 99 |
error(@what) |
| 100 |
fatal(@what) |
| 101 |
Instance methods. String of things that you're expecting to log, at |
| 102 |
the level you're expecting them, in what class. |
| 103 |
|
| 104 |
end() |
| 105 |
end($name) |
| 106 |
Ends the test and compares what we've got with what we expected. |
| 107 |
Switches logging back from being captured to going to wherever it |
| 108 |
was originally directed in the config. |
| 109 |
|
| 110 |
Ignoring All Logging Messages |
| 111 |
Sometimes you're going to be testing something that generates a load of |
| 112 |
spurious log messages that you simply want to ignore without testing |
| 113 |
their contents, but you don't want to have to reconfigure your log file. |
| 114 |
The simpliest way to do this is to do: |
| 115 |
|
| 116 |
use Test::Log4perl; |
| 117 |
Test::Log4perl->suppress_logging; |
| 118 |
|
| 119 |
All logging functions stop working. Do not alter the Logging classes |
| 120 |
(for example, by changing the config file and use Log4perl's |
| 121 |
"init_and_watch" functionality) after this call has been made. |
| 122 |
|
| 123 |
This function will be effectivly a no-op if the enviromental variable |
| 124 |
"NO_SUPRESS_LOGGING" is set to a true value (so if your code is behaving |
| 125 |
weirdly you can turn all the logging back on from the command line |
| 126 |
without changing any of the code) |
| 127 |
|
| 128 |
Selectivly Ignoring Logging Messages By Priority |
| 129 |
It's a bad idea to completely ignore all messages. What you probably |
| 130 |
want to do is ignore some of the trivial messages that you don't care |
| 131 |
about, and just test that there aren't any unexpected messages of a set |
| 132 |
priority. |
| 133 |
|
| 134 |
You can temporarly ignore any logging messages that are made by passing |
| 135 |
parameters to the "start" routine |
| 136 |
|
| 137 |
# for this test, just ignore DEBUG, INFO, and WARN |
| 138 |
Test::Log4perl->start( ignore_priority => "warn" ); |
| 139 |
|
| 140 |
# you can use the levels constants to do the same thing |
| 141 |
use Log::Log4perl qw(:levels); |
| 142 |
Test::Log4perl->start( ignore_priority => $WARN ); |
| 143 |
|
| 144 |
You might want to ignore all logging events at all (this can be used as |
| 145 |
quick way to not test the actual log messages, but just ignore the |
| 146 |
output. |
| 147 |
|
| 148 |
# for this test, ignore everything |
| 149 |
Test::Log4perl->start( ignore_priority => "everything" ); |
| 150 |
|
| 151 |
# contary to readability, the same thing (try not to write this) |
| 152 |
use Log::Log4perl qw(:levels); |
| 153 |
Test::Log4perl->start( ignore_priority => $OFF ); |
| 154 |
|
| 155 |
Or you might want to not ignore anything (which is the default, unless |
| 156 |
you've played with the method calls mentioned below:) |
| 157 |
|
| 158 |
# for this test, ignore nothing |
| 159 |
Test::Log4perl->start( ignore_priority => "nothing" ); |
| 160 |
|
| 161 |
# contary to readability, the same thing (try not to write this) |
| 162 |
use Log::Log4perl qw(:levels); |
| 163 |
Test::Log4perl->start( ignore_priority => $ALL ); |
| 164 |
|
| 165 |
You can also perminatly effect what things are ignored with the |
| 166 |
"ignore_priority" method call. This persists between tests and isn't |
| 167 |
autoically reset after each call to "start". |
| 168 |
|
| 169 |
# ignore DEBUG, INFO and WARN for all future tests |
| 170 |
Test::Log4perl->ignore_priority("warn"); |
| 171 |
|
| 172 |
# you can use the levels constants to do the same thing |
| 173 |
use Log::Log4perl qw(:levels); |
| 174 |
Test::Log4perl->ignore_priority($WARN); |
| 175 |
|
| 176 |
# ignore everything (no log messages will be logged) |
| 177 |
Test::Log4perl->ignore_priority("everything"); |
| 178 |
|
| 179 |
# ignore nothing (messages will be logged reguardless of priority) |
| 180 |
Test::Log4perl->ignore_priority("nothing"); |
| 181 |
|
| 182 |
Obviously, you may temporarly override whatever perminant |
| 183 |
|
| 184 |
BUGS |
| 185 |
Logging methods don't return the number of appenders they've written to |
| 186 |
(or rather, they do, as it's always zero.) |
| 187 |
|
| 188 |
Changing the config file (if you're watching it) while this is testing / |
| 189 |
supressing everything will probably break everything. As will creating |
| 190 |
new appenders, etc... |
| 191 |
|
| 192 |
AUTHOR |
| 193 |
Mark Fowler <[email protected]> |
| 194 |
|
| 195 |
COPYRIGHT |
| 196 |
Copyright 2005 Fotango Ltd all rights reserved. |
| 197 |
Licensed under the same terms as Perl itself. |
| 198 |
|