MozRepl::Log - MozRepl logging class


MozRepl documentation Contained in the MozRepl distribution.

Index


Code Index:

NAME

Top

MozRepl::Log - MozRepl logging class

VERSION

Top

version 0.01

SYNOPSIS

Top

    use MozRepl;

    my $repl = MozRepl->new;
    $repl->setup;

    $repl->log->debug("Look! someone on that wall!");

METHODS

Top

new(@levels)

Create instance. If you want to limit log levels, then specify only levels to want to use.

    my $log = MozRepl::Log->new(qw/info error/);

enable($level)

Return whether the specified level is enabled or not.

log($level, $messages)

Logging messege as specified level.

debug($messeage)

Logging message as debug level.

info($messeage)

Logging message as info level.

warn($messeage)

Logging message as warn level.

error($messeage)

Logging message as error level.

fatal($messeage)

Logging message as fatl level.

is_debug()

Return whether the debug level is enabled or not.

is_info()

Return whether the info level is enabled or not.

is_warn()

Return whether the warn level is enabled or not.

is_error()

Return whether the error level is enabled or not.

is_fatal()

Return whether the fatl level is enabled or not.

AUTHOR

Top

Toru Yamaguchi, <zigorou@cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-mozrepl-log@rt.cpan.org, or through the web interface at http://rt.cpan.org. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

COPYRIGHT & LICENSE

Top


MozRepl documentation Contained in the MozRepl distribution.
package MozRepl::Log;

use strict;
use warnings;

use base qw(Class::Accessor::Fast);

__PACKAGE__->mk_accessors(qw/levels/);

our %LEVELS = ();

{
    my @levels = qw/debug info warn error fatal/;

    for ( my $i = 0; $i < @levels; $i++ ) {
        my $name  = $levels[$i];
        my $level += $i;

        $LEVELS{$name} = $level;

        no strict 'refs';

        *{$name} = sub {
            my $self = shift;

            if ( $self->enable($level) ) {
                $self->log( uc($name), @_ );
            }
        };

        *{"is_$name"} = sub {
            my $self = shift;

            return ( $self->enable($level) ) ? 1 : 0;
        };
    }
}

our $VERSION = '0.01';

sub new {
    my ($class, @levels) = @_;
    my $self  = $class->SUPER::new;

    $self->levels(
        [   @levels > 0
            ? grep { exists $LEVELS{$_} } map { lc($_) } @levels
            : keys %LEVELS
        ]
    );

    return $self;
}

sub enable {
    my ( $self, $level ) = @_;
    ( ( grep { $LEVELS{$_} == $level } @{ $self->levels } ) == 1 ) ? 1 : 0;
}

sub log {
    my $self = shift;
    my $level = shift;
    my @messages = map { split(/\n/, $_) } @_;

    my $message = @messages > 1 ? join("\n", "", @messages) : shift @messages;

    warn( sprintf( "[%s] %s\n", $level, $message ) );
}

1;    # End of MozRepl::Log