1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
package Log::Dispatch::Output;
use strict;
use warnings;
our $VERSION = '2.71';
use Carp ();
use Try::Tiny;
use Log::Dispatch;
use Log::Dispatch::Types;
use Log::Dispatch::Vars qw( @OrderedLevels );
use Params::ValidationCompiler qw( validation_for );
use base qw( Log::Dispatch::Base );
sub new {
my $proto = shift;
my $class = ref $proto || $proto;
die "The new method must be overridden in the $class subclass";
}
{
my $validator = validation_for(
params => {
level => { type => t('LogLevel') },
# Pre-PVC we accepted empty strings, which is weird, but we don't
# want to break back-compat. See
# https://github.com/houseabsolute/Log-Dispatch/issues/38.
message => { type => t('Str') },
},
slurpy => 1,
);
## no critic (Subroutines::ProhibitBuiltinHomonyms)
sub log {
my $self = shift;
my %p = $validator->(@_);
my $level_num = $self->_level_as_number( $p{level} );
return unless $self->_should_log($level_num);
local $! = undef;
$p{message} = $self->_apply_callbacks(%p)
if $self->{callbacks};
$self->log_message(%p);
}
## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
sub _log_with_num {
my $self = shift;
my $level_num = shift;
my %p = @_;
return unless $self->_should_log($level_num);
local $! = undef;
$p{message} = $self->_apply_callbacks(%p)
if $self->{callbacks};
$self->log_message(%p);
}
## use critic
}
{
my $validator = validation_for(
params => {
name => {
type => t('NonEmptyStr'),
optional => 1,
},
min_level => { type => t('LogLevel') },
max_level => {
type => t('LogLevel'),
optional => 1,
},
callbacks => {
type => t('Callbacks'),
optional => 1,
},
newline => {
type => t('Bool'),
default => 0,
},
},
# This is primarily here for the benefit of outputs outside of this
# distro which may be passing who-knows-what to this method.
slurpy => 1,
);
## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
sub _basic_init {
my $self = shift;
my %p = $validator->(@_);
$self->{level_names} = \@OrderedLevels;
$self->{name} = $p{name} || $self->_unique_name();
$self->{min_level} = $self->_level_as_number( $p{min_level} );
# Either use the parameter supplied or just the highest possible level.
$self->{max_level} = (
exists $p{max_level}
? $self->_level_as_number( $p{max_level} )
: $#{ $self->{level_names} }
);
$self->{callbacks} = $p{callbacks} if $p{callbacks};
if ( $p{newline} ) {
push @{ $self->{callbacks} }, \&_add_newline_callback;
}
}
}
sub name {
my $self = shift;
return $self->{name};
}
sub min_level {
my $self = shift;
return $self->{level_names}[ $self->{min_level} ];
}
sub max_level {
my $self = shift;
return $self->{level_names}[ $self->{max_level} ];
}
sub accepted_levels {
my $self = shift;
return @{ $self->{level_names} }
[ $self->{min_level} .. $self->{max_level} ];
}
sub _should_log {
my $self = shift;
my $level_num = shift;
return ( ( $level_num >= $self->{min_level} )
&& ( $level_num <= $self->{max_level} ) );
}
## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
sub _level_as_name {
my $self = shift;
my $level = shift;
unless ( defined $level ) {
Carp::croak 'undefined value provided for log level';
}
my $canonical_level;
unless ( $canonical_level = Log::Dispatch->level_is_valid($level) ) {
Carp::croak "$level is not a valid Log::Dispatch log level";
}
return $canonical_level unless $level =~ /\A[0-7]+\z/;
return $self->{level_names}[$level];
}
## use critic
my $_unique_name_counter = 0;
sub _unique_name {
my $self = shift;
return '_anon_' . $_unique_name_counter++;
}
sub _add_newline_callback {
# This weird construct is an optimization since this might be called a lot
# - see https://github.com/autarch/Log-Dispatch/pull/7
+{@_}->{message} . "\n";
}
1;
# ABSTRACT: Base class for all Log::Dispatch::* objects
__END__
=pod
=encoding UTF-8
=head1 NAME
Log::Dispatch::Output - Base class for all Log::Dispatch::* objects
=head1 VERSION
version 2.71
=head1 SYNOPSIS
package Log::Dispatch::MySubclass;
use Log::Dispatch::Output;
use base qw( Log::Dispatch::Output );
sub new {
my $proto = shift;
my $class = ref $proto || $proto;
my %p = @_;
my $self = bless {}, $class;
$self->_basic_init(%p);
# Do more if you like
return $self;
}
sub log_message {
my $self = shift;
my %p = @_;
# Do something with message in $p{message}
}
1;
=head1 DESCRIPTION
This module is the base class from which all Log::Dispatch::* objects should be
derived.
=head1 CONSTRUCTOR
The constructor, C<new>, must be overridden in a subclass. See L<Output
Classes|Log::Dispatch/OUTPUT CLASSES> for a description of the common
parameters accepted by this constructor.
=head1 METHODS
This class provides the following methods:
=head2 $output->_basic_init(%p)
This should be called from a subclass's constructor. Make sure to pass the
arguments in @_ to it. It sets the object's name and minimum level from the
passed parameters It also sets up two other attributes which are used by other
Log::Dispatch::Output methods, level_names and level_numbers. Subclasses will
perform parameter validation in this method, and must also call the
superclass's method.
=head2 $output->name
Returns the object's name.
=head2 $output->min_level
Returns the object's minimum log level.
=head2 $output->max_level
Returns the object's maximum log level.
=head2 $output->accepted_levels
Returns a list of the object's accepted levels (by name) from minimum to
maximum.
=head2 $output->log( level => $, message => $ )
Sends a message if the level is greater than or equal to the object's minimum
level. This method applies any message formatting callbacks that the object may
have.
=head2 $output->_should_log ($)
This method is called from the C<log()> method with the log level of the
message to be logged as an argument. It returns a boolean value indicating
whether or not the message should be logged by this particular object. The
C<log()> method will not process the message if the return value is false.
=head2 $output->_level_as_number ($)
This method will take a log level as a string (or a number) and return the
number of that log level. If not given an argument, it returns the calling
object's log level instead. If it cannot determine the level then it will
croak.
=head2 $output->add_callback( $code )
Adds a callback (like those given during construction). It is added to the end
of the list of callbacks.
=head2 $dispatch->remove_callback( $code )
Remove the given callback from the list of callbacks.
=head1 SUBCLASSING
This class should be used as the base class for all logging objects you create
that you would like to work under the Log::Dispatch architecture. Subclassing
is fairly trivial. For most subclasses, if you simply copy the code in the
SYNOPSIS and then put some functionality into the C<log_message> method then
you should be all set. Please make sure to use the C<_basic_init> method as
described above.
The actual logging implementation should be done in a C<log_message> method
that you write. B<Do not override C<log>!>.
=head1 SUPPORT
Bugs may be submitted at L<https://github.com/houseabsolute/Log-Dispatch/issues>.
=head1 SOURCE
The source code repository for Log-Dispatch can be found at L<https://github.com/houseabsolute/Log-Dispatch>.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2023 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
The full text of the license can be found in the
F<LICENSE> file included with this distribution.
=cut
|