| 1 | package Logger::Syslog; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | use Sys::Syslog qw(:DEFAULT setlogsock); | ||
| 7 | use File::Basename; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Logger::Syslog -- an intuitive wrapper over Syslog for Perl | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | You want to deal with syslog, but you don't want to bother with Sys::Syslog, | ||
| 16 | that module is for you. | ||
| 17 | |||
| 18 | Logger::Syslog takes care of everything regarding the Syslog communication, all | ||
| 19 | you have to do is to use the function you need to send a message to syslog. | ||
| 20 | |||
| 21 | Logger::Syslog provides one function per Syslog message level: debug, info, | ||
| 22 | warning, error, notice, critic, alert. | ||
| 23 | |||
| 24 | =head1 NOTES | ||
| 25 | |||
| 26 | Logger::Syslog is compliant with mod_perl, all you have to do when using it | ||
| 27 | in such an environement is to call logger_init() at the beginning of your CGI, | ||
| 28 | that will garantee that everything will run smoothly (otherwise, issues with | ||
| 29 | the syslog socket can happen in mod_perl env). | ||
| 30 | |||
| 31 | =head1 SYNOPSIS | ||
| 32 | |||
| 33 | use Logger::Syslog; | ||
| 34 | |||
| 35 | info("Starting at ".localtime()); | ||
| 36 | ... | ||
| 37 | if ($error) { | ||
| 38 | error("An error occured!"); | ||
| 39 | exit 1; | ||
| 40 | } | ||
| 41 | ... | ||
| 42 | notice("There something to notify"); | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | BEGIN { | ||
| 47 | use Exporter ; | ||
| 48 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS %SIG); | ||
| 49 | $VERSION = "1.1"; | ||
| 50 | @ISA = ( 'Exporter' ) ; | ||
| 51 | @EXPORT = qw ( | ||
| 52 | &debug | ||
| 53 | &info | ||
| 54 | ¬ice | ||
| 55 | &warning | ||
| 56 | &error | ||
| 57 | &critic | ||
| 58 | &alert | ||
| 59 | &logger_prefix | ||
| 60 | &logger_close | ||
| 61 | &logger_init | ||
| 62 | &logger_set_default_facility | ||
| 63 | ); | ||
| 64 | @EXPORT_OK=@EXPORT; | ||
| 65 | %EXPORT_TAGS = (":all"=>[],); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub __get_script_name(); | ||
| 69 | my $DEFAULT_FACILITY = "user"; | ||
| 70 | our $fullname = __get_script_name(); | ||
| 71 | our $basename = basename($fullname); | ||
| 72 | |||
| 73 | =head1 FUNCTIONS | ||
| 74 | |||
| 75 | =head2 logger_init | ||
| 76 | |||
| 77 | Call this to explicitly open a Syslog socket. You can optionaly specify | ||
| 78 | a Syslog facility. | ||
| 79 | |||
| 80 | That function is called when you use the module, if you're not in a mod_perl | ||
| 81 | environement. | ||
| 82 | |||
| 83 | Examples: | ||
| 84 | |||
| 85 | # open a syslog socket with default facility (user) | ||
| 86 | logger_init(); | ||
| 87 | |||
| 88 | # open a syslog socket on the 'local' facility | ||
| 89 | logger_init('local'); | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub logger_init(;$) | ||
| 94 | { | ||
| 95 | my ($facility) = @_; | ||
| 96 | $facility = $DEFAULT_FACILITY unless defined $facility; | ||
| 97 | |||
| 98 | eval { | ||
| 99 | setlogsock('unix'); | ||
| 100 | $fullname = __get_script_name(); | ||
| 101 | openlog($fullname, 'pid', $facility); | ||
| 102 | logger_prefix(""); | ||
| 103 | }; | ||
| 104 | } | ||
| 105 | |||
| 106 | # If we're not under mod_perl, let's open the Syslog socket. | ||
| 107 | if (! defined $ENV{'MOD_PERL'}) { | ||
| 108 | logger_init(); | ||
| 109 | } | ||
| 110 | |||
| 111 | =head2 logger_close | ||
| 112 | |||
| 113 | Call this to close the Syslog socket. | ||
| 114 | |||
| 115 | That function is called automatically when the calling program exits. | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub logger_close() | ||
| 120 | { | ||
| 121 | eval { | ||
| 122 | closelog(); | ||
| 123 | }; | ||
| 124 | } | ||
| 125 | |||
| 126 | END { | ||
| 127 | eval { | ||
| 128 | logger_close(); | ||
| 129 | }; | ||
| 130 | } | ||
| 131 | |||
| 132 | =head2 logger_prefix | ||
| 133 | |||
| 134 | That function lets you set a string that will be prefixed to every | ||
| 135 | messages sent to syslog. | ||
| 136 | |||
| 137 | Example: | ||
| 138 | |||
| 139 | logger_prefix("my program"); | ||
| 140 | info("starting"); | ||
| 141 | ... | ||
| 142 | info("stopping"); | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | our $g_rh_prefix = {}; | ||
| 147 | sub logger_prefix(;$) | ||
| 148 | { | ||
| 149 | my ($prefix) = @_; | ||
| 150 | $prefix = "" unless defined $prefix; | ||
| 151 | $fullname = __get_script_name(); | ||
| 152 | $g_rh_prefix->{$fullname} = $prefix; | ||
| 153 | } | ||
| 154 | |||
| 155 | my %g_rh_label = ( | ||
| 156 | info => 'info ', | ||
| 157 | notice => 'note ', | ||
| 158 | err => 'error', | ||
| 159 | warning => 'warn ', | ||
| 160 | debug => 'debug', | ||
| 161 | crit => 'crit ', | ||
| 162 | alert => 'alert' | ||
| 163 | ); | ||
| 164 | |||
| 165 | |||
| 166 | =head2 logger_set_default_facility(facility) | ||
| 167 | |||
| 168 | You can choose which facility to use, the default one is "user". Use that | ||
| 169 | function if you want to switch smoothly from a facility to another. | ||
| 170 | |||
| 171 | That function will close the existing socket and will open a new one with the | ||
| 172 | appropriate facility. | ||
| 173 | |||
| 174 | Example: | ||
| 175 | |||
| 176 | logger_set_default_facility("cron"); | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | sub logger_set_default_facility($) | ||
| 181 | { | ||
| 182 | my ($facility) = @_; | ||
| 183 | if ($facility ne $DEFAULT_FACILITY) { | ||
| 184 | logger_close(); | ||
| 185 | logger_init($facility); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | =head1 LOGGING | ||
| 190 | |||
| 191 | Logger::Syslog provides one function per Syslog level to let you send messages. | ||
| 192 | If you want to send a debug message, just use debug(), for a warning, use | ||
| 193 | warning() and so on... | ||
| 194 | |||
| 195 | All those function have the same signature : thay take a string as their only | ||
| 196 | argument, which is the message to send to syslog. | ||
| 197 | |||
| 198 | Examples: | ||
| 199 | |||
| 200 | debug("my program starts at ".localtime()); | ||
| 201 | ... | ||
| 202 | warning("some strange stuff occured"); | ||
| 203 | ... | ||
| 204 | error("should not go there !"); | ||
| 205 | ... | ||
| 206 | notice("Here is my notice"); | ||
| 207 | |||
| 208 | =cut | ||
| 209 | |||
| 210 | sub AUTOLOAD | ||
| 211 | { | ||
| 212 | my ($message) = @_; | ||
| 213 | our $AUTOLOAD; | ||
| 214 | $AUTOLOAD =~ s/^.*:://; | ||
| 215 | return if ($AUTOLOAD eq 'DESTROY'); | ||
| 216 | |||
| 217 | return 0 unless defined $message and length $message; | ||
| 218 | my @supported = qw(debug info warning err error notice alert crit critic); | ||
| 219 | |||
| 220 | if (grep /^$AUTOLOAD$/, @supported) { | ||
| 221 | my $level = $AUTOLOAD; | ||
| 222 | |||
| 223 | $level = 'err' if ($level eq 'error'); | ||
| 224 | $level = 'crit' if ($level eq 'critic'); | ||
| 225 | |||
| 226 | log_with_syslog($level, $message); | ||
| 227 | } | ||
| 228 | else { | ||
| 229 | croak "Unsupported function : $AUTOLOAD"; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | sub log_with_syslog ($$) | ||
| 234 | { | ||
| 235 | my ($level, $message) = @_; | ||
| 236 | return 0 unless defined $level and defined $message; | ||
| 237 | |||
| 238 | my $caller = 2; | ||
| 239 | if ($ENV{MOD_PERL}) { | ||
| 240 | $caller = 1; | ||
| 241 | } | ||
| 242 | my ($package, $filename, $line, $fonction) = caller ($caller); | ||
| 243 | |||
| 244 | $package = "" unless defined $package; | ||
| 245 | $filename = "" unless defined $filename; | ||
| 246 | $line = 0 unless defined $line; | ||
| 247 | $fonction = $basename unless defined $fonction; | ||
| 248 | $level = lc($level); | ||
| 249 | $level = 'info' unless defined $level and length $level; | ||
| 250 | |||
| 251 | unless (defined $message and length $message) { | ||
| 252 | $message = "[void]"; | ||
| 253 | } | ||
| 254 | |||
| 255 | my $level_str = $g_rh_label{$level}; | ||
| 256 | $message = $level_str . " * $message"; | ||
| 257 | $message .= " - $fonction ($filename l. $line)" if $line; | ||
| 258 | |||
| 259 | $message =~ s/%/%%/g; # we have to escape % to avoid a bug related to sprintf() | ||
| 260 | $message = $g_rh_prefix->{$fullname} . " > " . $message if | ||
| 261 | (defined $g_rh_prefix->{$fullname} and length $g_rh_prefix->{$fullname}); | ||
| 262 | |||
| 263 | my $sig = $SIG{__WARN__}; | ||
| 264 | $SIG{__WARN__} = sub {}; | ||
| 265 | eval { | ||
| 266 | syslog($level, $message); | ||
| 267 | }; | ||
| 268 | $SIG{__WARN__} = $sig; | ||
| 269 | } | ||
| 270 | |||
| 271 | # returns the appropriate filename | ||
| 272 | sub __get_script_name() | ||
| 273 | { | ||
| 274 | # si on est en mod perl, il faut utiliser $ENV{'SCRIPT_FILENAME'} | ||
| 275 | return $ENV{'SCRIPT_FILENAME'} if $ENV{'MOD_PERL'} and $ENV{'SCRIPT_FILENAME'}; | ||
| 276 | return $0; | ||
| 277 | } | ||
| 278 | |||
| 279 | =head1 LICENSE | ||
| 280 | |||
| 281 | This program is free software; you can redistribute it and/or modify it under | ||
| 282 | the same terms as Perl itself. | ||
| 283 | |||
| 284 | =head1 COPYRIGHT | ||
| 285 | |||
| 286 | This program is copyright © 2004-2006 Alexis Sukrieh | ||
| 287 | |||
| 288 | =head1 AUTHOR | ||
| 289 | |||
| 290 | Alexis Sukrieh <sukria@sukria.net> | ||
| 291 | |||
| 292 | Very first versions were made at Cegetel (2004-2005) ; Thomas Parmelan gave a | ||
| 293 | hand for the mod_perl support. | ||
| 294 | |||
| 295 | =cut | ||
| 296 | |||
| 297 | 1; |