| 1 | package Catmandu::Logger; | ||
| 2 | |||
| 3 | 145 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 145 | our $VERSION = '1.0201_02'; | |
| 6 | |||
| 7 | 145 | ▶ | use Moo::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 145 | ▶ | use MooX::Aliases; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 145 | ▶ | use namespace::clean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 145 | with 'MooX::Role::Logger'; | |
| 12 | |||
| 13 | 145 | alias log => '_logger'; | |
| 14 | |||
| 15 | 145 | 1; | |
| 16 | |||
| 17 | __END__ | ||
| 18 | |||
| 19 | =pod | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | Catmandu::Logger - A role for classes that need logging capabilities | ||
| 24 | |||
| 25 | =head1 SYNOPSIS | ||
| 26 | |||
| 27 | package MyApp::View; | ||
| 28 | use Moo; | ||
| 29 | |||
| 30 | with 'Catmandu::Logger'; | ||
| 31 | |||
| 32 | sub something { | ||
| 33 | my ($self) = @_; | ||
| 34 | $self->log->debug("started bar"); # logs with default class catergory "MyApp::View" | ||
| 35 | $self->log->error("started bar"); | ||
| 36 | } | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | A logging role building a very lightweight wrapper to L<Log::Any>. Connecting | ||
| 41 | a Log::Any::Adapter should be performed prior to logging the first log message, | ||
| 42 | otherwise nothing will happen, just like with Log::Any. | ||
| 43 | |||
| 44 | The logger needs to be setup before using the logger, which could happen in the main application: | ||
| 45 | |||
| 46 | package main; | ||
| 47 | use Log::Any::Adapter; | ||
| 48 | use Log::Log4perl; | ||
| 49 | |||
| 50 | Log::Any::Adapter->set('Log4perl'); | ||
| 51 | Log::Log4perl::init('./log4perl.conf'); | ||
| 52 | |||
| 53 | my $app = MyApp::View->new; | ||
| 54 | $app->something(); # will print debug and error messages | ||
| 55 | |||
| 56 | with log4perl.conf as: | ||
| 57 | |||
| 58 | log4perl.rootLogger=DEBUG,STDOUT | ||
| 59 | log4perl.appender.STDOUT=Log::Log4perl::Appender::Screen | ||
| 60 | log4perl.appender.STDOUT.stderr=1 | ||
| 61 | log4perl.appender.STDOUT.utf8=1 | ||
| 62 | |||
| 63 | log4perl.appender.STDOUT.layout=PatternLayout | ||
| 64 | log4perl.appender.STDOUT.layout.ConversionPattern=%d [%P] - %p %l time=%r : %m%n | ||
| 65 | |||
| 66 | =head1 ACCESSORS | ||
| 67 | |||
| 68 | =head2 log | ||
| 69 | |||
| 70 | The C<log> attribute holds the L<Log::Any::Adapter> object that implements all logging methods for the | ||
| 71 | defined log levels, such as C<debug> or C<error>. | ||
| 72 | |||
| 73 | package MyApp::View::JSON; | ||
| 74 | |||
| 75 | extends 'MyApp::View'; | ||
| 76 | with 'Catmandu::Logger'; | ||
| 77 | |||
| 78 | sub bar { | ||
| 79 | $self->log->info("Everything fine so far"); # logs a info message | ||
| 80 | $self->log->debug("Something is fishy here"); # logs a debug message | ||
| 81 | } | ||
| 82 | |||
| 83 | Your package automatically has a logging category of MyApp::View::JSON. Use lines like: | ||
| 84 | |||
| 85 | log4perl.logger.MyApp::View::JSON=DEBUG,STDOUT | ||
| 86 | |||
| 87 | or | ||
| 88 | |||
| 89 | log4perl.logger.MyApp::View=DEBUG,STDOUT | ||
| 90 | |||
| 91 | or | ||
| 92 | |||
| 93 | log4perl.logger.MyApp=DEBUG,STDOUT | ||
| 94 | |||
| 95 | for specialized logging for your application. | ||
| 96 | |||
| 97 | =head1 SEE ALSO | ||
| 98 | |||
| 99 | L<Log::Any> | ||
| 100 | |||
| 101 | =head1 ACKNOWLEDGMENTS | ||
| 102 | |||
| 103 | Code and documentation blatantly stolen from C<MooX::Log::Any>. | ||
| 104 | |||
| 105 | =cut | ||