Business::Shipping::Logging - Log4perl wrapper for easy, non-OO usage.


Business-Shipping documentation Contained in the Business-Shipping distribution.

Index


Code Index:

NAME

Top

Business::Shipping::Logging - Log4perl wrapper for easy, non-OO usage.

NOTES

Top

The Log4perl category is Package::subroutine::line. This gives a lot of information for debugging. (Technically, category is whatever the fourth return value of caller(1) is.)

METHODS

Top

init

Build wrapper on top of Log4perl, increasing caller_depth to one:

 Business::Shipping::UPS_Offline::RateRequest::debug()
  |
  |
 Business::Shipping::Logging::debug()
  |
  |
 Log::Log4perl->logger->DEBUG()

Exported functions

Top

Please see Log4perl for more about these wrapped functions.

logdie

logwarn

fatal

error

warn

info

debug

trace

is_fatal

is_error

is_warn

is_info

is_debug

is_trace

log_level()

Does the heavy lifting for Business::Shipping->log_level().

AUTHOR

Top

Daniel Browning, db@kavod.com, http://www.kavod.com/

COPYRIGHT AND LICENCE

Top


Business-Shipping documentation Contained in the Business-Shipping distribution.
package Business::Shipping::Logging;

use strict;
use warnings;
use base qw(Exporter);
use vars qw(@EXPORT $Current_Level);
use Carp;
use Log::Log4perl;
use Business::Shipping::Config;
use version; our $VERSION = qv('400');

Log::Log4perl->wrapper_register(__PACKAGE__);
$Current_Level = 'WARN';
@EXPORT        = qw(
    fatal    is_fatal    logdie
    error    is_error
    warn     is_warn     logwarn
    info     is_info
    debug    is_debug
    trace    is_trace
);

init();

1;

# TODO: Should assume some basic configuration when the file isn't available.

sub init {
    my $config_dir = Business::Shipping::Config::config_dir();
    return carp "Could not find config directory." unless defined $config_dir;

    my $file = "$config_dir/log4perl.conf";
    return croak "Could not get log4perl config file: $file" unless -f $file;

    Log::Log4perl::init($file);

    return;
}

# (caller(1))[3] is shorthand for my (undef, undef, undef, $sub) = caller(1);
# Using call frame depth of 1

sub logdie   { Log::Log4perl->get_logger((caller(1))[3])->logdie(@_); }
sub logwarn  { Log::Log4perl->get_logger((caller(1))[3])->logwarn(@_); }
sub fatal    { Log::Log4perl->get_logger((caller(1))[3])->fatal(@_); }
sub error    { Log::Log4perl->get_logger((caller(1))[3])->error(@_); }
sub warn     { Log::Log4perl->get_logger((caller(1))[3])->warn(@_); }
sub info     { Log::Log4perl->get_logger((caller(1))[3])->info(@_); }
sub debug    { Log::Log4perl->get_logger((caller(1))[3])->debug(@_); }
sub trace    { Log::Log4perl->get_logger((caller(1))[3])->trace(@_); }
sub is_fatal { Log::Log4perl->get_logger((caller(1))[3])->is_fatal(); }
sub is_error { Log::Log4perl->get_logger((caller(1))[3])->is_error(); }
sub is_warn  { Log::Log4perl->get_logger((caller(1))[3])->is_warn(); }
sub is_info  { Log::Log4perl->get_logger((caller(1))[3])->is_info(); }
sub is_debug { Log::Log4perl->get_logger((caller(1))[3])->is_debug(); }
sub is_trace { Log::Log4perl->get_logger((caller(1))[3])->is_trace(); }

sub log_level {
    my ($class, $log_level) = @_;
    return unless $log_level;

    $log_level = lc $log_level;
    my @levels = qw(fatal error warn info debug trace);
    if (grep { $_ eq $log_level } @levels) {
        $Current_Level = uc $log_level;
    }
    Business::Shipping::Logging::init();

    return $log_level;
}

__END__