MooX::Role::Logger(3) Provide logging via Log::Any

VERSION

version 0.005

SYNOPSIS

In your modules:


package MyModule;
use Moose;
with 'MooX::Role::Logger';
sub run {
my ($self) = @_;
$self->cry;
}
sub cry {
my ($self) = @_;
$self->_logger->info("I'm sad");
}

In your application:

    use MyModule;
    use Log::Any::Adapter ('File', '/path/to/file.log');
    MyModule->run;

DESCRIPTION

This role provides universal logging via Log::Any. The class using this role doesn't need to know or care about the details of log configuration, implementation or destination.

Use it when you want your module to offer logging capabilities, but don't know who is going to use your module or what kind of logging they will implement. This role lets you do your part and leaves actual log setup and routing to someone else.

The application that ultimately uses your module can then choose to direct log messages somewhere based on its own needs and configuration with Log::Any::Adapter.

This role is based on Moo so it should work with either Moo or Moose based classes.

USAGE

Testing

Testing with Log::Any is pretty easy, thanks to Log::Any::Test. Just load that before Log::Any loads and your log messages get sent to a test adapter that includes testing methods:

    use Test::More 0.96;
    use Log::Any::Test;
    use Log::Any qw/$log/;
    use lib 't/lib';
    use MyModule;
    MyModule->new->cry;
    $log->contains_ok( qr/I'm sad/, "got log message" );
    done_testing;

Customizing

If you have a whole set of classes that should log with a single category, create your own role and set the "_build__logger_category" there:

    package MyLibrary::Role::Logger;
    use Moo::Role;
    with 'MooX::Role::Logger';
    sub _build__logger_category { "MyLibrary" }

Then in your other classes, use your custom role:

    package MyLibrary::Foo;
    use Moo;
    with 'MyLibrary::Role::Logger'

METHODS

_logger

Returns a logging object. See Log::Any for a list of logging methods it accepts.

_build__logger_category

Override to set the category used for logging. Defaults to the class name of the object (which could be a subclass). You can override to lock it to a particular name:

    sub _build__logger_category { __PACKAGE__ }

SUPPORT

Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker at <https://github.com/dagolden/MooX-Role-Logger/issues>. You will be notified automatically of any progress on your issue.

Source Code

This is open source software. The code repository is available for public review and contribution under the terms of the license.

<https://github.com/dagolden/MooX-Role-Logger>

  git clone https://github.com/dagolden/MooX-Role-Logger.git

AUTHOR

David Golden <[email protected]>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2013 by David Golden.

This is free software, licensed under:

  The Apache License, Version 2.0, January 2004