| Log-Dispatch-Config-TestLog documentation | Contained in the Log-Dispatch-Config-TestLog distribution. |
Log::Dispatch::Config::TestLog - Set up Log::Dispatch::Config for a test run
use Log::Dispatch::Config::TestLog;
This module will load Log::Dispatch::Config and set things up so that:
file, a Log::Dispatch::File
instance, whose output is the name of the test appended with log.
TEST_LOG_DIR is set or the log_dir parameter
is given to import, then log files will be created in that directory
instead. info level by default. If the tap_level
parameter is given to import then that level will be used instead. undef
can be passed to disable TAP output.
Make the test logging use different levels for certain things (fails increase the level, for instance), and consider scrubbing multi line output since we provide a one line format by default.
Yuval Kogman <nothingmuch@woobling.org>
Copyright (c) 2008, 2010 Yuval Kogman. All rights reserved This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Log-Dispatch-Config-TestLog documentation | Contained in the Log-Dispatch-Config-TestLog distribution. |
#!/usr/bin/perl package Log::Dispatch::Config::TestLog; use strict; use warnings; our $VERSION = "0.02"; use Sub::Override; use Test::Builder; use Log::Dispatch::Config; use Path::Class; use base qw(Log::Dispatch::Configurator); sub new { my ( $class, %args ) = @_; bless { %args, global => { dispatchers => [qw(file)], %{ $args{global} || {} } }, file => { class => 'Log::Dispatch::File', min_level => 'debug', %{ $args{file} || {} } }, }, $class; } sub get_attrs { my ( $self, $name ) = @_; $self->{$name}; } sub get_attrs_global { shift->get_attrs("global") } sub needs_reload { return } sub caller_file_to_log_file { my ( $self, $file, %args ) = @_; my $log_dir = dir( $ENV{TEST_LOG_DIR} || $args{log_dir} || $file->parent ); unless ( -d $log_dir ) { $log_dir->mkpath or die "Couldn't create test log directory $log_dir"; } unless ( -w $log_dir ) { die "Log directory $log_dir is not writable"; } return $log_dir->file( $file->basename . ".log" )->stringify; } my @overrides; sub import { my ( $self, %args ) = @_; require Test::Builder; my $file = file($0)->absolute; Log::Dispatch::Config->configure( $self->new( %args, file => { mode => "write", filename => $self->caller_file_to_log_file( $file, %args ), format => "[%d] [%p] %m\n", %{ $args{file} || {} } }, ), ); my $logger = Log::Dispatch::Config->instance; $logger->info("Starting test $0, pid = $$"); my $tap_level = exists($args{tap_log_level}) ? $args{tap_log_level} : "info"; if ( defined( $tap_level ) ) { unless ( @overrides ) { foreach my $print ( qw(_diag _print_to_fh) ) { no strict 'refs'; my $fq = "Test::Builder::$print"; my $orig = \&$fq; push @overrides, Sub::Override->new( $fq, sub { my ( $builder, @output ) = @_; shift @output if $print eq '_print_to_fh'; # first arg is output handle chomp( my $out = "@output" ); $logger->$tap_level("TAP: $out") if length $out; goto $orig; }); } } } } END { Log::Dispatch::Config->__instance && Log::Dispatch::Config->instance->info("Finishing test $0"); } __PACKAGE__ __END__