| 1 | #=============================================================================== | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # FILE: Logger.pm | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | # DESCRIPTION: Syslog wrapper for Net SDS | ||||||||||||||||||||
| 6 | # | ||||||||||||||||||||
| 7 | # AUTHOR: Michael Bochkaryov (Rattler), <misha@rattler.kiev.ua> | ||||||||||||||||||||
| 8 | # COMPANY: Net.Style | ||||||||||||||||||||
| 9 | # CREATED: 25.04.2008 17:32:37 EEST | ||||||||||||||||||||
| 10 | #=============================================================================== | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | NetSDS::Logger - syslog wrapper for applications and classes | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | use NetSDS::Logger; | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | my $logger = NetSDS::Logger->new( | ||||||||||||||||||||
| 21 | name => 'NetSDS-SuperDaemon', | ||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||
| 23 | $logger->log("info", "Syslog message here"); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | This module contains implementation of logging functionality for NetSDS components. | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | By default, messages are logged with C<local0> facility and C<pid,ndelay,nowait> options. | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | B<NOTE>: C<NetSDS::Logger> module is for internal use mostly from application | ||||||||||||||||||||
| 32 | frameworks like C<NetSDS::App>, C<NetSDS::App::FCGI>, etc. | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | package NetSDS::Logger; | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 2 | ▶ | use 5.8.0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 2 | ▶ | use Unix::Syslog qw(:macros :subs); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 2 | ▶ | use version; our $VERSION = '1.301'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | #=============================================================================== | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =head1 CLASS API | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | =over | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =item B<new(%parameters)> - constructor | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | Constructor B<new()> creates new logger object and opens socket with default | ||||||||||||||||||||
| 54 | NetSDS logging parameters. | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | Arguments allowed (as hash): | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | B<name> - application name for identification | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | Use only ASCII characters in "name" to avoid possible errors. | ||||||||||||||||||||
| 61 | Default value is "NetSDS". | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | B<facility> - logging facility | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | Available facility values: | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | * local0..local7 | ||||||||||||||||||||
| 68 | * user | ||||||||||||||||||||
| 69 | * daemon | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | If not set 'local0' is used as default value | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | #----------------------------------------------------------------------- | ||||||||||||||||||||
| 76 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | my ( $class, %params ) = @_; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | my $self = {}; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | # Set application identification name | ||||||||||||||||||||
| 83 | 0 | my $name = 'NetSDS'; | |||||||||||||||||||
| 84 | 0 | ▶ | if ( $params{name} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 0 | $name = $params{name}; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # Set logging facility | ||||||||||||||||||||
| 89 | 0 | my %facility_map = ( | |||||||||||||||||||
| 90 | 'local0' => LOG_LOCAL0, | ||||||||||||||||||||
| 91 | 'local1' => LOG_LOCAL1, | ||||||||||||||||||||
| 92 | 'local2' => LOG_LOCAL2, | ||||||||||||||||||||
| 93 | 'local3' => LOG_LOCAL3, | ||||||||||||||||||||
| 94 | 'local4' => LOG_LOCAL4, | ||||||||||||||||||||
| 95 | 'local5' => LOG_LOCAL5, | ||||||||||||||||||||
| 96 | 'local6' => LOG_LOCAL6, | ||||||||||||||||||||
| 97 | 'local7' => LOG_LOCAL7, | ||||||||||||||||||||
| 98 | 'user' => LOG_USER, | ||||||||||||||||||||
| 99 | 'daemon' => LOG_DAEMON, | ||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 0 | my $facility = LOG_LOCAL0; # default is local0 | |||||||||||||||||||
| 103 | 0 | ▶ | if ( $params{facility} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 0 | ▶ | $facility = $facility_map{ $params{facility} } || LOG_LOCAL0; | ||||||||||||||||||
|
Condition0%0/3
$facility_map{$params{'facility'}} || LOG_LOCAL0()
MC/DC0%0/2
$facility_map{$params{'facility'}} || LOG_LOCAL0()
$facility_map{$params{'facility'}}
LOG_LOCAL0()
Truth table0%0/3
A || B
A= $facility_map{$params{'facility'}}B= LOG_LOCAL0()
| |||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 0 | openlog( $name, LOG_PID | LOG_CONS | LOG_NDELAY, $facility ); | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 0 | return bless $self, $class; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | } ## end sub new | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | #*********************************************************************** | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =item B<log($level, $message)> - write record to log | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | Wrapper to C<syslog()> method of L<Unix::Syslog> module. | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | Level is passed as string and may be one of the following: | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | alert - LOG_ALERT | ||||||||||||||||||||
| 122 | crit - LOG_CRIT | ||||||||||||||||||||
| 123 | debug - LOG_DEBUG | ||||||||||||||||||||
| 124 | emerg - LOG_EMERG | ||||||||||||||||||||
| 125 | error - LOG_ERR | ||||||||||||||||||||
| 126 | info - LOG_INFO | ||||||||||||||||||||
| 127 | notice - LOG_NOTICE | ||||||||||||||||||||
| 128 | warning - LOG_WARNING | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | #----------------------------------------------------------------------- | ||||||||||||||||||||
| 133 | 0 | ▶ | sub log { | ||||||||||||||||||
|
Subroutine
log: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 0 | my ( $self, $level, $message ) = @_; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | # Level aliases | ||||||||||||||||||||
| 138 | 0 | my %LEVFIX = ( | |||||||||||||||||||
| 139 | alert => LOG_ALERT, | ||||||||||||||||||||
| 140 | crit => LOG_CRIT, | ||||||||||||||||||||
| 141 | critical => LOG_CRIT, | ||||||||||||||||||||
| 142 | deb => LOG_DEBUG, | ||||||||||||||||||||
| 143 | debug => LOG_DEBUG, | ||||||||||||||||||||
| 144 | emerg => LOG_EMERG, | ||||||||||||||||||||
| 145 | emergency => LOG_EMERG, | ||||||||||||||||||||
| 146 | panic => LOG_EMERG, | ||||||||||||||||||||
| 147 | err => LOG_ERR, | ||||||||||||||||||||
| 148 | error => LOG_ERR, | ||||||||||||||||||||
| 149 | inf => LOG_INFO, | ||||||||||||||||||||
| 150 | info => LOG_INFO, | ||||||||||||||||||||
| 151 | inform => LOG_INFO, | ||||||||||||||||||||
| 152 | note => LOG_NOTICE, | ||||||||||||||||||||
| 153 | notice => LOG_NOTICE, | ||||||||||||||||||||
| 154 | warning => LOG_WARNING, | ||||||||||||||||||||
| 155 | warn => LOG_WARNING, | ||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 0 | my $LEV = $LEVFIX{$level}; | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 0 | ▶ | if ( !$LEV ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | 0 | $LEV = LOG_INFO; | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 0 | ▶ | if ( !$message ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 165 | 0 | $message = ""; | |||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 0 | syslog( $LEV, "[$level] $message" ); | |||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | } ## end sub log | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | #*********************************************************************** | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =item B<DESTROY> - class destructor | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | Destructor (DESTROY method) calls C<closelog()> function. That's all. | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =cut | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | #----------------------------------------------------------------------- | ||||||||||||||||||||
| 181 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 0 | closelog(); | |||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 2 | 1; | |||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | __END__ | ||||||||||||||||||||