| 1 | package Bot::Cobalt::Logger; | ||
| 2 | $Bot::Cobalt::Logger::VERSION = '0.021002'; | ||
| 3 | 1 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Bot::Cobalt::Common ':types'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Bot::Cobalt::Logger::Output; | ||
| 8 | |||
| 9 | |||
| 10 | use Moo; | ||
| 11 | |||
| 12 | 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 | my ($self, $val) = @_; | ||
| 27 | $self->output->time_format($val) if $self->has_output | ||
| 28 | }, | ||
| 29 | ); | ||
| 30 | |||
| 31 | has log_format => ( | ||
| 32 | lazy => 1, | ||
| 33 | is => 'rw', | ||
| 34 | isa => Str, | ||
| 35 | predicate => 'has_log_format', | ||
| 36 | trigger => sub { | ||
| 37 | my ($self, $val) = @_; | ||
| 38 | $self->output->log_format($val) if $self->has_output | ||
| 39 | }, | ||
| 40 | ); | ||
| 41 | |||
| 42 | |||
| 43 | 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 | error => 1, | ||
| 57 | warn => 2, | ||
| 58 | info => 3, | ||
| 59 | debug => 4, | ||
| 60 | } | ||
| 61 | }, | ||
| 62 | ); | ||
| 63 | |||
| 64 | sub _build_output { | ||
| 65 | my ($self) = @_; | ||
| 66 | |||
| 67 | my %opts; | ||
| 68 | |||
| 69 | $opts{log_format} = $self->log_format if $self->has_log_format; | ||
| 70 | $opts{time_format} = $self->time_format if $self->has_time_format; | ||
| 71 | |||
| 72 | Bot::Cobalt::Logger::Output->new( | ||
| 73 | %opts | ||
| 74 | ) | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _should_log { | ||
| 78 | my ($self, $level) = @_; | ||
| 79 | |||
| 80 | my $num_lev = $self->_levmap->{$level} | ||
| 81 | || confess "unknown level $level"; | ||
| 82 | |||
| 83 | my $accept = $self->_levmap->{ $self->level }; | ||
| 84 | |||
| 85 | $accept >= $num_lev ? 1 : 0 | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _log_to_level { | ||
| 89 | my ($self, $level) = splice @_, 0, 2; | ||
| 90 | |||
| 91 | $self->output->_write( | ||
| 92 | $level, | ||
| 93 | [ caller(1) ], | ||
| 94 | @_ | ||
| 95 | ) if $self->_should_log($level); | ||
| 96 | |||
| 97 | 1 | ||
| 98 | } | ||
| 99 | |||
| 100 | sub debug { shift->_log_to_level( 'debug', @_ ) } | ||
| 101 | sub info { shift->_log_to_level( 'info', @_ ) } | ||
| 102 | sub warn { shift->_log_to_level( 'warn', @_ ) } | ||
| 103 | sub error { shift->_log_to_level( 'error', @_ ) } | ||
| 104 | |||
| 105 | 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 | ||