From: Tony B. <log...@tm...> - 2004-01-09 13:37:22
|
On Tue, Jan 06, 2004 at 09:12:17AM -0800, Mike Schilli wrote: > Absolutely, that's the way to go -- changing where logged message of a > system are going is just a matter of configuring l4p. > Just use a Log::Log4perl::Appender::File appender or > Log::Log4perl::Appender::TestBuffer at initialization time. I think I'm missing something quite fundamental here because I can't get this to work. With some fiddling around it seems that the problem is that my Log4perl::init() in the test gets clobbered by the one in the code that I'm testing and so doesn't actually log to the TestBuffer at all. Take the below example, which doesn't work. If I create a $foo before calling the Log4perl::init, everything works fine. But in the app I'm testing, there's no sensible place to hook in like this that I can find. Suggestions? Thanks, Tony ------ BEGIN EXAMPLE ------- package My::Foo; use Log::Log4perl ':easy'; sub new { Log::Log4perl->easy_init($INFO); bless {} => shift } sub do_it { my ($self, $info) = @_; get_logger()->info("do_it needs args") unless $info; return; } package main; use Test::More tests => 1; Log::Log4perl::init(\q{ screen = Log::Log4perl::Appender::TestBuffer log4perl.category = INFO, BufferApp log4perl.appender.BufferApp = ${screen} log4perl.appender.BufferApp.layout = \ Log::Log4perl::Layout::PatternLayout log4perl.appender.BufferApp.layout.ConversionPattern = %d %F{1} %L> %m %n }); { my $log = Log::Log4perl::Appender::TestBuffer->by_name("BufferApp"); My::Foo->new->do_it(1); is $log->buffer, "", "No log if args"; } { my $log = Log::Log4perl::Appender::TestBuffer->by_name("BufferApp"); My::Foo->new->do_it(); like $log->buffer, qr/needs args/, "Logged if no args"; } ------ END EXAMPLE ------- |