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
|
###########################################################################
#
# Priorities.pm
#
# Copyright (C) 1999 Raphael Manfredi.
# Copyright (C) 2002-2017 Mark Rogaski, mrogaski@cpan.org;
# all rights reserved.
#
# See the README file included with the
# distribution for license information.
#
##########################################################################
use strict;
########################################################################
package Log::Agent::Priorities;
require Exporter;
use AutoLoader 'AUTOLOAD';
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS @LEVELS);
@ISA = qw(Exporter);
@LEVELS = qw(NONE EMERG ALERT CRIT ERROR WARN NOTICE INFO DEBUG);
@EXPORT = qw(priority_level);
@EXPORT_OK = qw(prio_from_level level_from_prio);
push(@EXPORT_OK, @LEVELS);
%EXPORT_TAGS = (LEVELS => \@LEVELS);
BEGIN {
sub NONE () {-1}
sub EMERG () {0}
sub ALERT () {1}
sub CRIT () {2}
sub ERROR () {3}
sub WARN () {4}
sub NOTICE () {6}
sub INFO () {8}
sub DEBUG () {10}
}
use vars qw(@basic_prio %basic_level);
@basic_prio = qw(
emergency
alert
critical
error
warning warning
notice notice
info info);
%basic_level = (
'em' => EMERG, # emergency
'al' => ALERT, # alert
'cr' => CRIT, # critical
'er' => ERROR, # error
'wa' => WARN, # warning
'no' => NOTICE, # notice
'in' => INFO, # info
'de' => DEBUG, # debug
);
1;
__END__
#
# prio_from_level
#
# Given a level, compute suitable priority.
#
sub prio_from_level {
my ($level) = @_;
return 'none' if $level < 0;
return 'debug' if $level >= @basic_prio;
return $basic_prio[$level];
}
#
# level_from_prio
#
# Given a syslog priority, compute suitable level.
#
sub level_from_prio {
my ($prio) = @_;
return -1 if lc($prio) eq 'none'; # none & notice would look alike
my $canonical = lc(substr($prio, 0, 2));
return 10 unless exists $basic_level{$canonical};
return $basic_level{$canonical} || -1;
}
#
# priority_level
#
# Decompiles priority which can be either a single digit, a "priority" string
# or a "priority:digit" string. Returns the priority (computed if none) and
# the level (computed if none).
#
sub priority_level {
my ($id) = @_;
return (prio_from_level($id), $id) if $id =~ /^\d+$/;
return ($1, $2) if $id =~ /^([^:]+):(\d+)$/;
return ($id, level_from_prio($id));
}
=head1 NAME
Log::Agent::Priorities - conversion between syslog priorities and levels
=head1 SYNOPSIS
Not intended to be used directly
=head1 DESCRIPTION
This package contains routines to convert between syslog priorities
and logging levels: level_from_prio("crit") yields 2, and
prio_from_level(4) yields "warning", as does prio_from_level(5).
Here are the known priorities (which may be abbreviated to the first
2 letters, in a case-insensitive manner) and their corresponding
logging level:
Name Level Traditional Export
--------- ----- -------------- ------
none -1 NONE (special, see text)
emergency 0 (emerg, panic) EMERG
alert 1 ALERT
critical 2 (crit) CRIT
error 3 (err) ERROR
warning 4 WARN
notice 6 NOTICE
info 8 INFO
debug 10 DEBUG
The values between parenthesis show the traditional syslog priority tokens.
The missing levels (5, 7, 9) are there for possible extension.
They currently map to the level immediately below.
The Export column lists the symbolic constants defined by this package.
They can be imported selectively, or altogether via the C<:LEVELS>
tag, as in:
use Log::Agent::Priorities qw(:LEVELS);
The special token "none" may be used (and spelled out fully) on special
occasions: it maps to -1, and is convenient when specifying a logging
level, for instance: specifying "none" ensures that B<no logging> will
take place, even for emergency situations.
Anywhere where a I<priority> is expected, one may specify a number taken
as a logging level or a string taken as a priority. If the default
mapping outlined above is not satisfactory, it can be redefined by
specifying, for instance C<"notice:9">. It will be taken as being of
level 9, but with a C<notice> priority nonetheless, not C<info> as
it would have been implicitly determined otherwise.
The routine priority_level() decompiles C<"notice:9"> into ("notice", 9),
and otherwise uses prio_from_level() or level_from_prio() to compute the
missing informatin. For instance, given "critical", priority_level()
routine will return the tuple ("critical", 2).
=head1 AUTHOR
Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>>
=head1 SEE ALSO
Log::Agent(3), Log::Agent::Logger(3).
=cut
|