AnyEvent
view release on metacpan or search on metacpan
lib/AnyEvent/Log.pm view on Meta::CPAN
Example: create a new logging context and set both the default logging
level, some slave contexts and a logging callback.
$ctx = new AnyEvent::Log::Ctx
title => "dubious messages",
level => "error",
log_cb => sub { print STDOUT shift; 0 },
slaves => [$ctx1, $ctx, $ctx2],
;
=back
=cut
sub new {
my $class = shift;
my $ctx = AnyEvent::Log::ctx undef;
while (@_) {
my ($k, $v) = splice @_, 0, 2;
$ctx->$k (ref $v eq "ARRAY" ? @$v : $v);
}
bless $ctx, $class # do we really support subclassing, hmm?
}
=head2 CONFIGURING A LOG CONTEXT
The following methods can be used to configure the logging context.
=over 4
=item $ctx->title ([$new_title])
Returns the title of the logging context - this is the package name, for
package contexts, and a user defined string for all others.
If C<$new_title> is given, then it replaces the package name or title.
=cut
sub title {
$_[0][0] = $_[1] if @_ > 1;
$_[0][0]
}
=back
=head3 LOGGING LEVELS
The following methods deal with the logging level set associated with the
log context.
The most common method to use is probably C<< $ctx->level ($level) >>,
which configures the specified and any higher priority levels.
All functions which accept a list of levels also accept the special string
C<all> which expands to all logging levels.
=over 4
=item $ctx->levels ($level[, $level...)
Enables logging for the given levels and disables it for all others.
=item $ctx->level ($level)
Enables logging for the given level and all lower level (higher priority)
ones. In addition to normal logging levels, specifying a level of C<0> or
C<off> disables all logging for this level.
Example: log warnings, errors and higher priority messages.
$ctx->level ("warn");
$ctx->level (5); # same thing, just numeric
=item $ctx->enable ($level[, $level...])
Enables logging for the given levels, leaving all others unchanged.
=item $ctx->disable ($level[, $level...])
Disables logging for the given levels, leaving all others unchanged.
=item $ctx->cap ($level)
Caps the maximum priority to the given level, for all messages logged
to, or passing through, this context. That is, while this doesn't affect
whether a message is logged or passed on, the maximum priority of messages
will be limited to the specified level - messages with a higher priority
will be set to the specified priority.
Another way to view this is that C<< ->level >> filters out messages with
a too low priority, while C<< ->cap >> modifies messages with a too high
priority.
This is useful when different log targets have different interpretations
of priority. For example, for a specific command line program, a wrong
command line switch might well result in a C<fatal> log message, while the
same message, logged to syslog, is likely I<not> fatal to the system or
syslog facility as a whole, but more likely a mere C<error>.
This can be modeled by having a stderr logger that logs messages "as-is"
and a syslog logger that logs messages with a level cap of, say, C<error>,
or, for truly system-critical components, actually C<critical>.
=cut
sub _lvl_lst {
map {
$_ > 0 && $_ <= 9 ? $_+0
: $_ eq "all" ? (1 .. 9)
: $STR2LEVEL{$_} || Carp::croak "$_: not a valid logging level, caught"
} @_
}
sub _lvl {
$_[0] =~ /^(?:0|off|none)$/ ? 0 : (_lvl_lst $_[0])[-1]
( run in 1.361 second using v1.01-cache-2.11-cpan-5b529ec07f3 )