| 1 | package MouseX::Log::Dispatch::Config; |
|---|
| 2 | |
|---|
| 3 | use 5.8.1; |
|---|
| 4 | use Mouse::Role; |
|---|
| 5 | use MouseX::Types::Log::Dispatch::Configurator; |
|---|
| 6 | use Log::Dispatch::Config; |
|---|
| 7 | use namespace::clean -except => ['meta']; |
|---|
| 8 | |
|---|
| 9 | our $VERSION = '0.01'; |
|---|
| 10 | |
|---|
| 11 | has 'logger' => ( |
|---|
| 12 | is => 'rw', |
|---|
| 13 | isa => 'Log::Dispatch::Config', |
|---|
| 14 | lazy_build => 1, |
|---|
| 15 | handles => [qw( |
|---|
| 16 | log debug info notice warning error critical alert emergency |
|---|
| 17 | )], |
|---|
| 18 | ); |
|---|
| 19 | |
|---|
| 20 | has 'config' => ( |
|---|
| 21 | is => 'rw', |
|---|
| 22 | isa => 'Log::Dispatch::Configurator', |
|---|
| 23 | coerce => 1, |
|---|
| 24 | predicate => 'has_config', |
|---|
| 25 | ); |
|---|
| 26 | |
|---|
| 27 | sub _build_logger { |
|---|
| 28 | my $self = shift; |
|---|
| 29 | |
|---|
| 30 | if ($self->has_config) { |
|---|
| 31 | Log::Dispatch::Config->configure($self->config); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | return Log::Dispatch::Config->instance; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | no Mouse::Role; 1; |
|---|
| 38 | |
|---|
| 39 | =head1 NAME |
|---|
| 40 | |
|---|
| 41 | MouseX::Log::Dispatch::Config |
|---|
| 42 | |
|---|
| 43 | =head1 SYNOPSIS |
|---|
| 44 | |
|---|
| 45 | package MyLogger; |
|---|
| 46 | use MouseX::Log::Dispatch::Config; |
|---|
| 47 | |
|---|
| 48 | # file-based (AppConfig style) |
|---|
| 49 | has '+config' => (default => '/path/to/log.cfg'); |
|---|
| 50 | |
|---|
| 51 | package HashLogger; |
|---|
| 52 | use MouseX::Log::Dispatch::Config; |
|---|
| 53 | |
|---|
| 54 | # hash-based |
|---|
| 55 | has '+config' => (default => sub { |
|---|
| 56 | { |
|---|
| 57 | class => 'Log::Dispatch::Screen', |
|---|
| 58 | min_level => 'debug', |
|---|
| 59 | stderr => 1, |
|---|
| 60 | format => '[%p] %m at %F line %L%n', |
|---|
| 61 | } |
|---|
| 62 | }); |
|---|
| 63 | |
|---|
| 64 | package CustomLogger; |
|---|
| 65 | use MouseX::Log::Dispatch::Config; |
|---|
| 66 | use Log::Dispatch::Configurator::YAML; |
|---|
| 67 | |
|---|
| 68 | # custom configurator |
|---|
| 69 | has '+config' => (default => sub { |
|---|
| 70 | Log::Dispatch::Configurator::YAML->new('/path/to/log.yml'); |
|---|
| 71 | }); |
|---|
| 72 | |
|---|
| 73 | package main; |
|---|
| 74 | |
|---|
| 75 | my $log = MyLogger->new; |
|---|
| 76 | |
|---|
| 77 | $log->debug('foo'); |
|---|
| 78 | $log->logger->debug('bar'); # also works |
|---|
| 79 | |
|---|
| 80 | $log->info('baz'); |
|---|
| 81 | $log->error('error'); |
|---|
| 82 | |
|---|
| 83 | =head1 DESCRIPTION |
|---|
| 84 | |
|---|
| 85 | This is a role which provides a L<Log::Dispatch::Config> logger. |
|---|
| 86 | |
|---|
| 87 | =head1 METHODS |
|---|
| 88 | |
|---|
| 89 | =head2 log |
|---|
| 90 | |
|---|
| 91 | =head2 debug |
|---|
| 92 | |
|---|
| 93 | =head2 info |
|---|
| 94 | |
|---|
| 95 | =head2 notice |
|---|
| 96 | |
|---|
| 97 | =head2 warning |
|---|
| 98 | |
|---|
| 99 | =head2 error |
|---|
| 100 | |
|---|
| 101 | =head2 critical |
|---|
| 102 | |
|---|
| 103 | =head2 alert |
|---|
| 104 | |
|---|
| 105 | =head2 emergency |
|---|
| 106 | |
|---|
| 107 | =head1 PROPERTIES |
|---|
| 108 | |
|---|
| 109 | =head2 logger |
|---|
| 110 | |
|---|
| 111 | Returns a L<Log::Dispatch::Config> object. |
|---|
| 112 | |
|---|
| 113 | =head2 config |
|---|
| 114 | |
|---|
| 115 | =head1 AUTHOR |
|---|
| 116 | |
|---|
| 117 | NAKAGAWA Masaki E<lt>masaki@cpan.orgE<gt> |
|---|
| 118 | |
|---|
| 119 | =head1 THANKS TO |
|---|
| 120 | |
|---|
| 121 | Ash Berlin, and L<MooseX::LogDispatch/AUTHOR> |
|---|
| 122 | |
|---|
| 123 | =head1 LICENSE |
|---|
| 124 | |
|---|
| 125 | This library is free software; you can redistribute it and/or modify |
|---|
| 126 | it under the same terms as Perl itself. |
|---|
| 127 | |
|---|
| 128 | =head1 SEE ALSO |
|---|
| 129 | |
|---|
| 130 | L<Mouse>, L<Mouse::Role>, L<MouseX::Types::Log::Dispatch::Configurator>, |
|---|
| 131 | |
|---|
| 132 | L<Log::Dispatch::Config>, L<Log::Dispatch::Configurator>, L<Log::Dispatch>, |
|---|
| 133 | |
|---|
| 134 | L<MooseX::LogDispatch> |
|---|
| 135 | |
|---|
| 136 | =cut |
|---|