| 1 | package Bot::Cobalt::Logger; | ||||||||||||||||||||
| 2 | 1 | $Bot::Cobalt::Logger::VERSION = '0.021001'; | |||||||||||||||||||
| 3 | 2 | ▶ | use strictures 2; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | ▶ | use Bot::Cobalt::Common ':types'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use Bot::Cobalt::Logger::Output; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 1 | has level => ( | |||||||||||||||||||
| 13 | required => 1, | ||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||
| 15 | writer => 'set_level', | ||||||||||||||||||||
| 16 | isa => Enum[qw/error warn info debug/], | ||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | ## time_format / log_format are passed to ::Output | ||||||||||||||||||||
| 20 | has time_format => ( | ||||||||||||||||||||
| 21 | lazy => 1, | ||||||||||||||||||||
| 22 | is => 'rw', | ||||||||||||||||||||
| 23 | isa => Str, | ||||||||||||||||||||
| 24 | predicate => 'has_time_format', | ||||||||||||||||||||
| 25 | trigger => sub { | ||||||||||||||||||||
| 26 | 0 | ▶ | my ($self, $val) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 27 | 0 | ▶ | $self->output->time_format($val) if $self->has_output | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||
| 29 | 1 | ); | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | has log_format => ( | ||||||||||||||||||||
| 32 | lazy => 1, | ||||||||||||||||||||
| 33 | is => 'rw', | ||||||||||||||||||||
| 34 | isa => Str, | ||||||||||||||||||||
| 35 | predicate => 'has_log_format', | ||||||||||||||||||||
| 36 | trigger => sub { | ||||||||||||||||||||
| 37 | 0 | ▶ | my ($self, $val) = @_; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 38 | 0 | ▶ | $self->output->log_format($val) if $self->has_output | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | }, | ||||||||||||||||||||
| 40 | 1 | ); | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 1 | has output => ( | |||||||||||||||||||
| 44 | lazy => 1, | ||||||||||||||||||||
| 45 | is => 'rwp', | ||||||||||||||||||||
| 46 | predicate => 'has_output', | ||||||||||||||||||||
| 47 | isa => InstanceOf['Bot::Cobalt::Logger::Output'], | ||||||||||||||||||||
| 48 | builder => '_build_output', | ||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | has _levmap => ( | ||||||||||||||||||||
| 52 | is => 'ro', | ||||||||||||||||||||
| 53 | isa => HashRef, | ||||||||||||||||||||
| 54 | builder => sub { | ||||||||||||||||||||
| 55 | +{ | ||||||||||||||||||||
| 56 | 0 | ▶ | error => 1, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 57 | warn => 2, | ||||||||||||||||||||
| 58 | info => 3, | ||||||||||||||||||||
| 59 | debug => 4, | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||
| 62 | 1 | ); | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | sub _build_output { | ||||||||||||||||||||
| 65 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
_build_output: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | my %opts; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | $opts{log_format} = $self->log_format if $self->has_log_format; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | ▶ | $opts{time_format} = $self->time_format if $self->has_time_format; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 0 | Bot::Cobalt::Logger::Output->new( | |||||||||||||||||||
| 73 | %opts | ||||||||||||||||||||
| 74 | ) | ||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | sub _should_log { | ||||||||||||||||||||
| 78 | 0 | ▶ | my ($self, $level) = @_; | ||||||||||||||||||
|
Subroutine
_should_log: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | ▶ | my $num_lev = $self->_levmap->{$level} | ||||||||||||||||||
|
Condition0%0/3
$self->_levmap->{$level} || confess("unknown level $level")
MC/DC0%0/2
$self->_levmap->{$level} || confess("unknown level $level")
$self->_levmap->{$level}
confess("unknown level $level")
Truth table0%0/3
A || B
A= $self->_levmap->{$level}B= confess("unknown level $level")
| |||||||||||||||||||||
| 81 | || confess "unknown level $level"; | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | my $accept = $self->_levmap->{ $self->level }; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | ▶ | $accept >= $num_lev ? 1 : 0 | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | sub _log_to_level { | ||||||||||||||||||||
| 89 | 0 | ▶ | my ($self, $level) = splice @_, 0, 2; | ||||||||||||||||||
|
Subroutine
_log_to_level: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 0 | ▶ | $self->output->_write( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | $level, | ||||||||||||||||||||
| 93 | [ caller(1) ], | ||||||||||||||||||||
| 94 | @_ | ||||||||||||||||||||
| 95 | ) if $self->_should_log($level); | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | 1 | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | ▶ | sub debug { shift->_log_to_level( 'debug', @_ ) } | ||||||||||||||||||
|
Subroutine
debug: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 101 | 0 | ▶ | sub info { shift->_log_to_level( 'info', @_ ) } | ||||||||||||||||||
|
Subroutine
info: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 102 | 0 | ▶ | sub warn { shift->_log_to_level( 'warn', @_ ) } | ||||||||||||||||||
|
Subroutine
warn: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 103 | 0 | ▶ | sub error { shift->_log_to_level( 'error', @_ ) } | ||||||||||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 1 | 1; | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =pod | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =head1 NAME | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | Bot::Cobalt::Logger - Log handler for Bot::Cobalt | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =head1 SYNOPSIS | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | my $logger = Bot::Cobalt::Logger->new( | ||||||||||||||||||||
| 116 | ## Required, one of: debug info warn error | ||||||||||||||||||||
| 117 | level => 'info', | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | ## Optional, passed to Bot::Cobalt::Logger::Output | ||||||||||||||||||||
| 120 | time_format => "%Y/%m/%d %H:%M:%S" | ||||||||||||||||||||
| 121 | log_format => "%time% %pkg% (%level%) %msg%" | ||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | ## Add outputs | ||||||||||||||||||||
| 125 | ## (See Bot::Cobalt::Logger::Output for details) | ||||||||||||||||||||
| 126 | $logger->output->add( | ||||||||||||||||||||
| 127 | 'Output::File' => | ||||||||||||||||||||
| 128 | { file => $path_to_log }, | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 'Output::Term' => | ||||||||||||||||||||
| 131 | { }, | ||||||||||||||||||||
| 132 | ); | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | ## Log messages | ||||||||||||||||||||
| 135 | $logger->debug("Debugging message", @more_info ); | ||||||||||||||||||||
| 136 | $logger->info("Informative message"); | ||||||||||||||||||||
| 137 | $logger->warn("Warning message"); | ||||||||||||||||||||
| 138 | $logger->error("Error message"); | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =head1 DESCRIPTION | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | This is the log handler for L<Bot::Cobalt>. | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | Configured outputs must be added before log messages actually go | ||||||||||||||||||||
| 145 | anywhere (see the L</SYNOPSIS>). See L<Bot::Cobalt::Logger::Output> for | ||||||||||||||||||||
| 146 | details. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =head2 Log Levels | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | A B<level> is required at construction-time; messages logged to the | ||||||||||||||||||||
| 151 | specified level or any level below it will be recorded. | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | For example, a B<level> of 'warn' will discard log messages to 'debug' | ||||||||||||||||||||
| 154 | and 'info' and report only 'warn' and 'error' messages. | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | Valid levels, from high to low: | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | debug | ||||||||||||||||||||
| 159 | info | ||||||||||||||||||||
| 160 | warn | ||||||||||||||||||||
| 161 | error | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | These should be called as methods to log to the appropriate level: | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | $logger->info("This is some information"); | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | If a list is provided, it will be concatenated with an empty space | ||||||||||||||||||||
| 168 | between items: | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | $logger->info("Some info", "more info"); | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =head2 Methods | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =head3 level | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | Returns the currently tracked log level. | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head3 set_level | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | Changes the current log level. | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =head3 time_format | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | Sets a date/time formatting string to be fed to C<strftime> -- see | ||||||||||||||||||||
| 185 | L<Bot::Cobalt::Logger::Output> | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | =head3 log_format | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | Sets a formatting template string for log messages -- see | ||||||||||||||||||||
| 190 | L<Bot::Cobalt::Logger::Output> | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =head3 output | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | Returns the L<Bot::Cobalt::Logger::Output> object. | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | =head1 AUTHOR | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | Jon Portnoy <avenj@cobaltirc.org> | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||