/usr/share/perl5/Logger/Syslog.pm is in liblogger-syslog-perl 1.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package Logger::Syslog;
use strict;
use warnings;
use Carp;
use Sys::Syslog qw(:DEFAULT setlogsock);
use File::Basename;
=head1 NAME
Logger::Syslog -- an intuitive wrapper over Syslog for Perl
=head1 DESCRIPTION
You want to deal with syslog, but you don't want to bother with Sys::Syslog,
that module is for you.
Logger::Syslog takes care of everything regarding the Syslog communication, all
you have to do is to use the function you need to send a message to syslog.
Logger::Syslog provides one function per Syslog message level: debug, info,
warning, error, notice, critic, alert.
=head1 NOTES
Logger::Syslog is compliant with mod_perl, all you have to do when using it
in such an environement is to call logger_init() at the beginning of your CGI,
that will garantee that everything will run smoothly (otherwise, issues with
the syslog socket can happen in mod_perl env).
=head1 SYNOPSIS
use Logger::Syslog;
info("Starting at ".localtime());
...
if ($error) {
error("An error occured!");
exit 1;
}
...
notice("There something to notify");
=cut
BEGIN {
use Exporter ;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS %SIG);
$VERSION = "1.1";
@ISA = ( 'Exporter' ) ;
@EXPORT = qw (
&debug
&info
¬ice
&warning
&error
&critic
&alert
&logger_prefix
&logger_close
&logger_init
&logger_set_default_facility
);
@EXPORT_OK=@EXPORT;
%EXPORT_TAGS = (":all"=>[],);
}
sub __get_script_name();
my $DEFAULT_FACILITY = "user";
our $fullname = __get_script_name();
our $basename = basename($fullname);
=head1 FUNCTIONS
=head2 logger_init
Call this to explicitly open a Syslog socket. You can optionaly specify
a Syslog facility.
That function is called when you use the module, if you're not in a mod_perl
environement.
Examples:
# open a syslog socket with default facility (user)
logger_init();
# open a syslog socket on the 'local' facility
logger_init('local');
=cut
sub logger_init(;$)
{
my ($facility) = @_;
$facility = $DEFAULT_FACILITY unless defined $facility;
eval {
setlogsock('unix');
$fullname = __get_script_name();
openlog($fullname, 'pid', $facility);
logger_prefix("");
};
}
# If we're not under mod_perl, let's open the Syslog socket.
if (! defined $ENV{'MOD_PERL'}) {
logger_init();
}
=head2 logger_close
Call this to close the Syslog socket.
That function is called automatically when the calling program exits.
=cut
sub logger_close()
{
eval {
closelog();
};
}
END {
eval {
logger_close();
};
}
=head2 logger_prefix
That function lets you set a string that will be prefixed to every
messages sent to syslog.
Example:
logger_prefix("my program");
info("starting");
...
info("stopping");
=cut
our $g_rh_prefix = {};
sub logger_prefix(;$)
{
my ($prefix) = @_;
$prefix = "" unless defined $prefix;
$fullname = __get_script_name();
$g_rh_prefix->{$fullname} = $prefix;
}
my %g_rh_label = (
info => 'info ',
notice => 'note ',
err => 'error',
warning => 'warn ',
debug => 'debug',
crit => 'crit ',
alert => 'alert'
);
=head2 logger_set_default_facility(facility)
You can choose which facility to use, the default one is "user". Use that
function if you want to switch smoothly from a facility to another.
That function will close the existing socket and will open a new one with the
appropriate facility.
Example:
logger_set_default_facility("cron");
=cut
sub logger_set_default_facility($)
{
my ($facility) = @_;
if ($facility ne $DEFAULT_FACILITY) {
logger_close();
logger_init($facility);
}
}
=head1 LOGGING
Logger::Syslog provides one function per Syslog level to let you send messages.
If you want to send a debug message, just use debug(), for a warning, use
warning() and so on...
All those function have the same signature : thay take a string as their only
argument, which is the message to send to syslog.
Examples:
debug("my program starts at ".localtime());
...
warning("some strange stuff occured");
...
error("should not go there !");
...
notice("Here is my notice");
=cut
sub AUTOLOAD
{
my ($message) = @_;
our $AUTOLOAD;
$AUTOLOAD =~ s/^.*:://;
return if ($AUTOLOAD eq 'DESTROY');
return 0 unless defined $message and length $message;
my @supported = qw(debug info warning err error notice alert crit critic);
if (grep /^$AUTOLOAD$/, @supported) {
my $level = $AUTOLOAD;
$level = 'err' if ($level eq 'error');
$level = 'crit' if ($level eq 'critic');
log_with_syslog($level, $message);
}
else {
croak "Unsupported function : $AUTOLOAD";
}
}
sub log_with_syslog ($$)
{
my ($level, $message) = @_;
return 0 unless defined $level and defined $message;
my $caller = 2;
if ($ENV{MOD_PERL}) {
$caller = 1;
}
my ($package, $filename, $line, $fonction) = caller ($caller);
$package = "" unless defined $package;
$filename = "" unless defined $filename;
$line = 0 unless defined $line;
$fonction = $basename unless defined $fonction;
$level = lc($level);
$level = 'info' unless defined $level and length $level;
unless (defined $message and length $message) {
$message = "[void]";
}
my $level_str = $g_rh_label{$level};
$message = $level_str . " * $message";
$message .= " - $fonction ($filename l. $line)" if $line;
$message =~ s/%/%%/g; # we have to escape % to avoid a bug related to sprintf()
$message = $g_rh_prefix->{$fullname} . " > " . $message if
(defined $g_rh_prefix->{$fullname} and length $g_rh_prefix->{$fullname});
my $sig = $SIG{__WARN__};
$SIG{__WARN__} = sub {};
eval {
syslog($level, $message);
};
$SIG{__WARN__} = $sig;
}
# returns the appropriate filename
sub __get_script_name()
{
# si on est en mod perl, il faut utiliser $ENV{'SCRIPT_FILENAME'}
return $ENV{'SCRIPT_FILENAME'} if $ENV{'MOD_PERL'} and $ENV{'SCRIPT_FILENAME'};
return $0;
}
=head1 LICENSE
This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=head1 COPYRIGHT
This program is copyright © 2004-2006 Alexis Sukrieh
=head1 AUTHOR
Alexis Sukrieh <sukria@sukria.net>
Very first versions were made at Cegetel (2004-2005) ; Thomas Parmelan gave a
hand for the mod_perl support.
=cut
1;
|