root/lang/perl/MouseX-Log-Dispatch-Config/trunk/lib/MouseX/Log/Dispatch/Config.pm @ 28277

Revision 28277, 2.4 kB (checked in by masaki, 6 years ago)

POD

Line 
1package MouseX::Log::Dispatch::Config;
2
3use 5.8.1;
4use Mouse::Role;
5use MouseX::Types::Log::Dispatch::Configurator;
6use Log::Dispatch::Config;
7use namespace::clean -except => ['meta'];
8
9our $VERSION = '0.01';
10
11has '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
20has 'config' => (
21    is        => 'rw',
22    isa       => 'Log::Dispatch::Configurator',
23    coerce    => 1,
24    predicate => 'has_config',
25);
26
27sub _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
37no Mouse::Role; 1;
38
39=head1 NAME
40
41MouseX::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
85This 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
111Returns a L<Log::Dispatch::Config> object.
112
113=head2 config
114
115=head1 AUTHOR
116
117NAKAGAWA Masaki E<lt>masaki@cpan.orgE<gt>
118
119=head1 THANKS TO
120
121Ash Berlin, and L<MooseX::LogDispatch/AUTHOR>
122
123=head1 LICENSE
124
125This library is free software; you can redistribute it and/or modify
126it under the same terms as Perl itself.
127
128=head1 SEE ALSO
129
130L<Mouse>, L<Mouse::Role>, L<MouseX::Types::Log::Dispatch::Configurator>,
131
132L<Log::Dispatch::Config>, L<Log::Dispatch::Configurator>, L<Log::Dispatch>,
133
134L<MooseX::LogDispatch>
135
136=cut
Note: See TracBrowser for help on using the browser.