File Coverage

blib/lib/POE/Component/MessageQueue/Logger.pm
Criterion Covered Total %
statement 12 17 70.5
branch 6 10 60.0
condition n/a
subroutine 4 4 100.0
pod 0 2 0.0
total 22 33 66.6


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 12     12   8294 use Moose;
  12         31  
  12         89  
20 12     12   76374 use POE::Kernel;
  12         27  
  12         127  
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 8732     8732 0 29140 my ($self, $type, $msg) = @_;
53              
54 8732 50       23060 if ( not defined $msg )
55             {
56 0         0 $msg = $type;
57 0         0 $type = 'info';
58             }
59              
60 8732 100       302702 if ( $self->has_log_function )
    50          
    50          
61             {
62 162         4933 $self->log_function->( $type, $msg );
63             }
64             elsif ( $self->has_logger_alias )
65             {
66 0         0 $poe_kernel->post($self->logger_alias, $type, "$msg\n" );
67             }
68             elsif ($LEVELS->{$type} >= $self->level )
69             {
70 0         0 print STDERR "$msg\n";
71             }
72             }
73              
74             sub shutdown
75             {
76 4     4 0 14 my $self = shift;
77              
78 4 50       135 if ($self->has_logger_alias)
79             {
80 0           $poe_kernel->signal( $self->logger_alias, 'TERM' );
81             }
82             }
83              
84             1;
85