| 1 | package Net::CLI::Interact::Logger; | ||||||||||||||||||||||||||||
| 2 | 3 | $Net::CLI::Interact::Logger::VERSION = '2.400002'; | |||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Sub::Quote; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use MooX::Types::MooseLike::Base qw(HashRef Bool ArrayRef Any); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Class::Mix qw(genpkg); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Time::HiRes qw(gettimeofday tv_interval); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Log::Dispatch::Config; # loads Log::Dispatch | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Log::Dispatch::Configurator::Any; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 2 | my ($class, @args) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # accept single hash ref or naked hash | ||||||||||||||||||||||||||||
| 16 | 2 | ▶ | my $params = (ref {} eq ref $args[0] ? $args[0] : {@args}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # back-compat for old attr name | ||||||||||||||||||||||||||||
| 19 | 2 | ▶ | $params->{log_stamp} = $params->{log_stamps} if exists $params->{log_stamps}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | return $params; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 3 | has log_config => ( | |||||||||||||||||||||||||||
| 25 | is => 'rw', | ||||||||||||||||||||||||||||
| 26 | isa => HashRef, | ||||||||||||||||||||||||||||
| 27 | builder => 1, | ||||||||||||||||||||||||||||
| 28 | trigger => quote_sub(q{ $_[0]->_clear_logger }), | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | sub _build_log_config { | ||||||||||||||||||||||||||
|
Subroutine
_build_log_config: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | return { | ||||||||||||||||||||||||||||
| 33 | 2 | dispatchers => ['screen'], | |||||||||||||||||||||||||||
| 34 | screen => { | ||||||||||||||||||||||||||||
| 35 | class => 'Log::Dispatch::Screen', | ||||||||||||||||||||||||||||
| 36 | min_level => 'debug', | ||||||||||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 3 | has _logger => ( | |||||||||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||||||||
| 43 | isa => quote_sub(q{ $_[0]->isa('Log::Dispatch::Config') }), | ||||||||||||||||||||||||||||
| 44 | builder => 1, | ||||||||||||||||||||||||||||
| 45 | lazy => 1, | ||||||||||||||||||||||||||||
| 46 | clearer => 1, | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # this allows each instance of this module to have its own | ||||||||||||||||||||||||||||
| 50 | # wrapped logger with different configuration. | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub _build__logger { | ||||||||||||||||||||||||||
|
Subroutine
_build__logger: not called CC=1 SCAR=3.3
| |||||||||||||||||||||||||||||
| 52 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | my $anon_logger = genpkg(); | |||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||
| 56 | ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 58 | 0 | @{"$anon_logger\::ISA"} = 'Log::Dispatch::Config'; | |||||||||||||||||||||||||||
| 59 | ## use critic | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | my $config = Log::Dispatch::Configurator::Any->new($self->log_config); | |||||||||||||||||||||||||||
| 63 | 0 | $anon_logger->configure($config); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | return $anon_logger->instance; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 3 | has 'log_stamp' => ( | |||||||||||||||||||||||||||
| 69 | is => 'rw', | ||||||||||||||||||||||||||||
| 70 | isa => Bool, | ||||||||||||||||||||||||||||
| 71 | default => quote_sub('1'), | ||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 3 | has 'log_category' => ( | |||||||||||||||||||||||||||
| 75 | is => 'rw', | ||||||||||||||||||||||||||||
| 76 | isa => Bool, | ||||||||||||||||||||||||||||
| 77 | default => quote_sub('1'), | ||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | has 'log_start' => ( | ||||||||||||||||||||||||||||
| 81 | is => 'ro', | ||||||||||||||||||||||||||||
| 82 | isa => ArrayRef, | ||||||||||||||||||||||||||||
| 83 | 2 | ▶ | default => sub{ [gettimeofday] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 3 | ); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | has 'log_flags' => ( | ||||||||||||||||||||||||||||
| 87 | is => 'rw', | ||||||||||||||||||||||||||||
| 88 | isa => Any, # FIXME 'ArrayRef|HashRef[Str]', | ||||||||||||||||||||||||||||
| 89 | 2 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 3 | ); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 3 | my %code_for = ( | |||||||||||||||||||||||||||
| 93 | debug => 0, | ||||||||||||||||||||||||||||
| 94 | info => 1, | ||||||||||||||||||||||||||||
| 95 | notice => 2, | ||||||||||||||||||||||||||||
| 96 | warning => 3, | ||||||||||||||||||||||||||||
| 97 | error => 4, | ||||||||||||||||||||||||||||
| 98 | critical => 5, | ||||||||||||||||||||||||||||
| 99 | alert => 6, | ||||||||||||||||||||||||||||
| 100 | emergency => 7, | ||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | ▶ | sub would_log { | ||||||||||||||||||||||||||
|
Subroutine
would_log: called CC=6 SCAR=22.9
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 2 | my ($self, $category, $level) = @_; | |||||||||||||||||||||||||||
| 105 | 2 | ▶ | return 0 if !defined $category or !defined $level; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $category and defined $level
MC/DC0%0/2
defined $category and defined $level
defined $category
defined $level
Truth table33%1/3
A and B
A= defined $categoryB= defined $level
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | my $flags = (ref $self->log_flags eq ref [] | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | ? { map {$_ => 'error'} @{$self->log_flags} } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | : $self->log_flags | ||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | ▶ | return 0 if !exists $code_for{$level}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 2 | ▶ | return 0 if !exists $flags->{$category}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | return ($code_for{$level} >= $code_for{ $flags->{$category} }); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 2 | ▶ | sub log { | ||||||||||||||||||||||||||
|
Subroutine
log: called CC=6 SCAR=33.6
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 2 | my ($self, $category, $level, @msgs) = @_; | |||||||||||||||||||||||||||
| 119 | 2 | ▶ | return unless $self->would_log($category, $level); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | @msgs = grep {defined} @msgs; | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | return unless scalar @msgs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | my $prefix = ''; | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | $prefix .= sprintf "[%11s] ", sprintf "%.6f", (tv_interval $self->log_start, [gettimeofday]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | if $self->log_stamp; | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | $prefix .= (substr $category, 0, 2) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | if $self->log_category; | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | my $suffix = ''; | |||||||||||||||||||||||||||
| 130 | 0 | ▶ | $suffix = "\n" if $msgs[-1] !~ m/\n$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | $self->_logger->$level($prefix . (' ' x (2 - $code_for{$level})), (join ' ', @msgs) . $suffix); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 3 | 1; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =pod | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =for Pod::Coverage BUILDARGS | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =for test_synopsis | ||||||||||||||||||||||||||||
| 142 | my ($logger, $category, $level, @message); | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 NAME | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Net::CLI::Interact::Logger - Per-instance multi-target logging, with categories | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | $logger->log($category, $level, @message); | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | This module implements a generic logging service, based on L<Log::Dispatch> | ||||||||||||||||||||||||||||
| 155 | but with additional options and configuration. Log messages coming from your | ||||||||||||||||||||||||||||
| 156 | application are categorized, and each category can be enabled/disabled | ||||||||||||||||||||||||||||
| 157 | separately and have its own log level (i.e. C<emergency> .. C<debug>). High | ||||||||||||||||||||||||||||
| 158 | resolution timestamps can be added to log messages. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 DEFAULT CONFIGURATION | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Being based on L<Log::Dispatch::Config>, this logger can have multiple | ||||||||||||||||||||||||||||
| 163 | targets, each configured for independent level thresholds. The overall default | ||||||||||||||||||||||||||||
| 164 | configuration is to print log messages to the screen (console), with a minimum | ||||||||||||||||||||||||||||
| 165 | level of C<debug>. Each category (see below) has its own log level as well. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Note that categories, as discussed below, are arbitrary so if a category is | ||||||||||||||||||||||||||||
| 168 | not explicitly enabled or disabled, it is assumed to be B<disabled>. If you | ||||||||||||||||||||||||||||
| 169 | wish to invent a new category for your application, simply think of the name | ||||||||||||||||||||||||||||
| 170 | and begin to use it, with a C<$level> and C<@message> as above in the | ||||||||||||||||||||||||||||
| 171 | SYNOPSIS. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 INTERFACE | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 log( $category, $level, @message ) | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | The combination of category and level determine whether the the log messages | ||||||||||||||||||||||||||||
| 178 | are emitted to any of the log destinations. Destinations are set using the | ||||||||||||||||||||||||||||
| 179 | C<log_config> method, and categories are configured using the C<log_flags> | ||||||||||||||||||||||||||||
| 180 | method. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | The C<@message> list will be joined by a space character, and a newline | ||||||||||||||||||||||||||||
| 183 | appended if the last message doesn't contain one itself. Messages are | ||||||||||||||||||||||||||||
| 184 | prepended with the first character of their C<$category>, and then indented | ||||||||||||||||||||||||||||
| 185 | proportionally to their C<$level>. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head2 log_config( \%config ) | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | A C<Log::Dispatch::Config> configuration (hash ref), meaning multiple log | ||||||||||||||||||||||||||||
| 190 | targets may be specified with different minimum level thresholds. There is a | ||||||||||||||||||||||||||||
| 191 | default configuration which emits messages to your screen (console) with no | ||||||||||||||||||||||||||||
| 192 | minimum threshold: | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | { | ||||||||||||||||||||||||||||
| 195 | dispatchers => ['screen'], | ||||||||||||||||||||||||||||
| 196 | screen => { | ||||||||||||||||||||||||||||
| 197 | class => 'Log::Dispatch::Screen', | ||||||||||||||||||||||||||||
| 198 | min_level => 'debug', | ||||||||||||||||||||||||||||
| 199 | }, | ||||||||||||||||||||||||||||
| 200 | }; | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head2 log_flags( \@categories | \%category_level_map ) | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | The user is expected to specify which log categories they are interested in, | ||||||||||||||||||||||||||||
| 205 | and at what levels. If a category is used in the application for logging but | ||||||||||||||||||||||||||||
| 206 | not specified, then it is deemed B<disabled>. Hence, even though the default | ||||||||||||||||||||||||||||
| 207 | destination log level is C<debug>, no messages are emitted until a category is | ||||||||||||||||||||||||||||
| 208 | enabled. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | In the array reference form, the list should contain category names, and they | ||||||||||||||||||||||||||||
| 211 | will all be mapped to the C<error> level: | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | $logger->log_flags([qw/ | ||||||||||||||||||||||||||||
| 214 | network | ||||||||||||||||||||||||||||
| 215 | disk | ||||||||||||||||||||||||||||
| 216 | io | ||||||||||||||||||||||||||||
| 217 | cpu | ||||||||||||||||||||||||||||
| 218 | /]); | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | In the hash reference form, the keys should be category names and the values | ||||||||||||||||||||||||||||
| 221 | log levels from the list below (ordered such that each level "includes" the | ||||||||||||||||||||||||||||
| 222 | levels I<above>): | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | emergency | ||||||||||||||||||||||||||||
| 225 | alert | ||||||||||||||||||||||||||||
| 226 | critical | ||||||||||||||||||||||||||||
| 227 | error | ||||||||||||||||||||||||||||
| 228 | warning | ||||||||||||||||||||||||||||
| 229 | notice | ||||||||||||||||||||||||||||
| 230 | info | ||||||||||||||||||||||||||||
| 231 | debug | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | For example: | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | $logger->log_flags({ | ||||||||||||||||||||||||||||
| 236 | network => 'info', | ||||||||||||||||||||||||||||
| 237 | disk => 'debug', | ||||||||||||||||||||||||||||
| 238 | io => 'critical', | ||||||||||||||||||||||||||||
| 239 | cpu => 'debug', | ||||||||||||||||||||||||||||
| 240 | }); | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | Messages at or above the specified level will be passed on to the | ||||||||||||||||||||||||||||
| 243 | C<Log::Dispatch> target, which may then specify an overriding threshold. | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head2 C< Net::CLI::Interact->default_log_categories() >> | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | Not a part of this class, but the only way to retrieve a list of the current | ||||||||||||||||||||||||||||
| 248 | log categories used in the L<Net::CLI::Interact> distribution source. Does not | ||||||||||||||||||||||||||||
| 249 | take into account any log categories added by the user. | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =head2 log_stamp( $boolean ) | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | Enable (the default) or disable the display of high resolution interval | ||||||||||||||||||||||||||||
| 254 | timestamps with each log message. | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | =head2 log_category( $boolean ) | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | Enable (the default) or disable the display of the first letters of the | ||||||||||||||||||||||||||||
| 259 | category name with each log message. | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =head2 log_start( [$seconds, $microseconds] ) | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | Time of the start for generating a time interval when logging stamps. Defaults | ||||||||||||||||||||||||||||
| 264 | to the result of C<Time::HiRes::gettimeofday> at the point the module is | ||||||||||||||||||||||||||||
| 265 | loaded, in list context. | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =head2 would_log( $category, $level ) | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | Returns True if, according to the current C<log_flags>, the given C<$category> | ||||||||||||||||||||||||||||
| 270 | is enabled at or above the threshold of C<$level>, otherwise returns False. | ||||||||||||||||||||||||||||
| 271 | Note that the C<Log::Dispatch> targets maintain their own thresholds as well. | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | =cut | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||