File Coverage

blib/lib/POE/Component/MessageQueue/Logger.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             #
2             # Copyright 2007-2010 David Snopek <dsnopek@gmail.com>
3             #
4             # This program is free software: you can redistribute it and/or modify
5             # it under the terms of the GNU General Public License as published by
6             # the Free Software Foundation, either version 2 of the License, or
7             # (at your option) any later version.
8             #
9             # This program is distributed in the hope that it will be useful,
10             # but WITHOUT ANY WARRANTY; without even the implied warranty of
11             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12             # GNU General Public License for more details.
13             #
14             # You should have received a copy of the GNU General Public License
15             # along with this program. If not, see <http://www.gnu.org/licenses/>.
16             #
17              
18             package POE::Component::MessageQueue::Logger;
19 1     1   3169 use Moose;
  0            
  0            
20             use POE::Kernel;
21              
22             my $LEVELS = {
23             debug => 0,
24             info => 1,
25             notice => 2,
26             warning => 3,
27             error => 4,
28             critical => 5,
29             alert => 6,
30             emergency => 7
31             };
32              
33             has 'level' => (
34             is => 'rw',
35             default => 3,
36             );
37              
38             has 'logger_alias' => (
39             is => 'rw',
40             writer => 'set_logger_alias',
41             predicate => 'has_logger_alias',
42             );
43              
44             has 'log_function' => (
45             is => 'rw',
46             writer => 'set_log_function',
47             predicate => 'has_log_function',
48             );
49              
50             sub log
51             {
52             my ($self, $type, $msg) = @_;
53              
54             if ( not defined $msg )
55             {
56             $msg = $type;
57             $type = 'info';
58             }
59              
60             if ( $self->has_log_function )
61             {
62             $self->log_function->( $type, $msg );
63             }
64             elsif ( $self->has_logger_alias )
65             {
66             $poe_kernel->post($self->logger_alias, $type, "$msg\n" );
67             }
68             elsif ($LEVELS->{$type} >= $self->level )
69             {
70             print STDERR "$msg\n";
71             }
72             }
73              
74             sub shutdown
75             {
76             my $self = shift;
77              
78             if ($self->has_logger_alias)
79             {
80             $poe_kernel->signal( $self->logger_alias, 'TERM' );
81             }
82             }
83              
84             1;
85