3 use warnings::register;
5 use Fcntl qw(O_WRONLY);
7 use POSIX qw(strftime setlocale LC_TIME);
17 standard => [qw(openlog syslog closelog setlogmask)],
18 extended => [qw(setlogsock)],
22 LOG_ALERT LOG_CRIT LOG_DEBUG LOG_EMERG LOG_ERR
23 LOG_INFO LOG_NOTICE LOG_WARNING
28 LOG_AUTH LOG_AUTHPRIV LOG_CRON LOG_DAEMON LOG_FTP LOG_KERN
29 LOG_LOCAL0 LOG_LOCAL1 LOG_LOCAL2 LOG_LOCAL3 LOG_LOCAL4
30 LOG_LOCAL5 LOG_LOCAL6 LOG_LOCAL7 LOG_LPR LOG_MAIL LOG_NEWS
31 LOG_SYSLOG LOG_USER LOG_UUCP
33 # Mac OS X specific facilities
34 qw( LOG_INSTALL LOG_LAUNCHD LOG_NETINFO LOG_RAS LOG_REMOTEAUTH ),
35 # modern BSD specific facilities
36 qw( LOG_CONSOLE LOG_NTP LOG_SECURITY ),
37 # IRIX specific facilities
38 qw( LOG_AUDIT LOG_LFMT ),
42 LOG_CONS LOG_PID LOG_NDELAY LOG_NOWAIT LOG_ODELAY LOG_PERROR
47 LOG_FACMASK LOG_NFACILITIES LOG_PRIMASK
54 @{$EXPORT_TAGS{standard}},
58 @{$EXPORT_TAGS{extended}},
59 @{$EXPORT_TAGS{macros}},
64 XSLoader::load('Sys::Syslog', $VERSION);
68 push @ISA, 'DynaLoader';
69 bootstrap Sys::Syslog $VERSION;
77 use vars qw($host); # host to send syslog messages to (see notes at end)
82 use vars qw($facility);
83 my $connected = 0; # flag to indicate if we're connected or not
84 my $syslog_send; # coderef of the function used to send messages
85 my $syslog_path = undef; # syslog path for "stream" and "unix" mechanisms
86 my $syslog_xobj = undef; # if defined, holds the external object used to send messages
87 my $transmit_ok = 0; # flag to indicate if the last message was transmited
88 my $current_proto = undef; # current mechanism used to transmit messages
89 my $ident = ''; # identifiant prepended to each message
90 $facility = ''; # current facility
91 my $maskpri = LOG_UPTO(&LOG_DEBUG); # current log mask
100 # Default is now to first use the native mechanism, so Perl programs
101 # behave like other normal C programs, then try other mechanisms.
102 my @connectMethods = qw(native tcp udp unix stream console);
103 if ($^O =~ /^(freebsd|linux)$/) {
104 @connectMethods = grep { $_ ne 'udp' } @connectMethods;
107 # use EventLog on Win32
108 my $is_Win32 = $^O =~ /Win32/i;
109 eval "use Sys::Syslog::Win32";
112 unshift @connectMethods, 'eventlog';
113 } elsif ($is_Win32) {
117 my @defaultMethods = @connectMethods;
118 my @fallbackMethods = ();
120 # coderef for a nicer handling of errors
121 my $err_sub = $options{nofatal} ? \&warnings::warnif : \&croak;
125 # This AUTOLOAD is used to 'autoload' constants from the constant()
129 ($constname = $AUTOLOAD) =~ s/.*:://;
130 croak "Sys::Syslog::constant() not defined" if $constname eq 'constant';
131 my ($error, $val) = constant($constname);
132 croak $error if $error;
134 *$AUTOLOAD = sub { $val };
140 ($ident, my $logopt, $facility) = @_;
143 $ident ||= basename($0) || getlogin() || getpwuid($<) || 'syslog';
145 $facility ||= LOG_USER();
147 for my $opt (split /\b/, $logopt) {
148 $options{$opt} = 1 if exists $options{$opt}
151 $err_sub = $options{nofatal} ? \&warnings::warnif : \&croak;
152 return 1 unless $options{ndelay};
157 $facility = $ident = '';
162 my $oldmask = $maskpri;
163 $maskpri = shift unless $_[0] == 0;
169 $syslog_path = shift;
170 disconnect_log() if $connected;
172 @fallbackMethods = ();
173 @connectMethods = @defaultMethods;
175 if (ref $setsock eq 'ARRAY') {
176 @connectMethods = @$setsock;
178 } elsif (lc $setsock eq 'stream') {
179 if (not defined $syslog_path) {
180 my @try = qw(/dev/log /dev/conslog);
182 if (length &_PATH_LOG) { # Undefined _PATH_LOG is "".
183 unshift @try, &_PATH_LOG;
193 if (not defined $syslog_path) {
194 warnings::warnif "stream passed to setlogsock, but could not find any device";
199 if (not -w $syslog_path) {
200 warnings::warnif "stream passed to setlogsock, but $syslog_path is not writable";
203 @connectMethods = qw(stream);
206 } elsif (lc $setsock eq 'unix') {
207 if (length _PATH_LOG() || (defined $syslog_path && -w $syslog_path)) {
208 $syslog_path = _PATH_LOG() unless defined $syslog_path;
209 @connectMethods = qw(unix);
211 warnings::warnif 'unix passed to setlogsock, but path not available';
215 } elsif (lc $setsock eq 'native') {
216 @connectMethods = qw(native);
218 } elsif (lc $setsock eq 'eventlog') {
219 if (eval "use Win32::EventLog; 1") {
220 @connectMethods = qw(eventlog);
222 warnings::warnif "eventlog passed to setlogsock, but operating system isn't Win32-compatible"
225 } elsif (lc $setsock eq 'tcp') {
226 if (getservbyname('syslog', 'tcp') || getservbyname('syslogng', 'tcp')) {
227 @connectMethods = qw(tcp);
229 warnings::warnif "tcp passed to setlogsock, but tcp service unavailable";
233 } elsif (lc $setsock eq 'udp') {
234 if (getservbyname('syslog', 'udp')) {
235 @connectMethods = qw(udp);
237 warnings::warnif "udp passed to setlogsock, but udp service unavailable";
241 } elsif (lc $setsock eq 'inet') {
242 @connectMethods = ( 'tcp', 'udp' );
244 } elsif (lc $setsock eq 'console') {
245 @connectMethods = qw(console);
248 croak "Invalid argument passed to setlogsock; must be 'stream', 'unix', 'native', 'eventlog', 'tcp', 'udp' or 'inet'"
255 my $priority = shift;
258 my (@words, $num, $numpri, $numfac, $sum);
260 my $fail_time = undef;
263 # if $ident is undefined, it means openlog() wasn't previously called
264 # so do it now in order to have sensible defaults
265 openlog() unless $ident;
267 local $facility = $facility; # may need to change temporarily.
269 croak "syslog: expecting argument \$priority" unless defined $priority;
270 croak "syslog: expecting argument \$format" unless defined $mask;
272 @words = split(/\W+/, $priority, 2); # Allow "level" or "level|facility".
277 $num = xlate($_); # Translate word to number.
279 croak "syslog: invalid level/facility: $_"
281 elsif ($num <= &LOG_PRIMASK) {
282 croak "syslog: too many levels given: $_" if defined $numpri;
284 return 0 unless LOG_MASK($numpri) & $maskpri;
287 croak "syslog: too many facilities given: $_" if defined $numfac;
293 croak "syslog: level must be given" unless defined $numpri;
295 if (not defined $numfac) { # Facility not specified in this call.
296 $facility = 'user' unless $facility;
297 $numfac = xlate($facility);
300 connect_log() unless $connected;
303 # escape percent signs for sprintf()
304 $error =~ s/%/%%/g if @_;
305 # replace %m with $error, if preceded by an even number of percent signs
306 $mask =~ s/(?<!%)((?:%%)*)%m/$1$error/g;
309 $mask .= "\n" unless $mask =~ /\n$/;
310 $message = @_ ? sprintf($mask, @_) : $mask;
312 if($current_proto eq 'native') {
316 elsif ($current_proto eq 'eventlog') {
321 $whoami .= "[$$]" if $options{pid};
323 $sum = $numpri + $numfac;
324 my $oldlocale = setlocale(LC_TIME);
325 setlocale(LC_TIME, 'C');
326 my $timestamp = strftime "%b %e %T", localtime;
327 setlocale(LC_TIME, $oldlocale);
328 $buf = "<$sum>$timestamp $whoami: $message\0";
331 # it's possible that we'll get an error from sending
332 # (e.g. if method is UDP and there is no UDP listener,
333 # then we'll get ECONNREFUSED on the send). So what we
334 # want to do at this point is to fallback onto a different
336 while (scalar @fallbackMethods || $syslog_send) {
337 if ($failed && (time - $fail_time) > 60) {
338 # it's been a while... maybe things have been fixed
339 @fallbackMethods = ();
341 $transmit_ok = 0; # make it look like a fresh attempt
345 if ($connected && !connection_ok()) {
346 # Something was OK, but has now broken. Remember coz we'll
347 # want to go back to what used to be OK.
348 $failed = $current_proto unless $failed;
353 connect_log() unless $connected;
354 $failed = undef if ($current_proto && $failed && $current_proto eq $failed);
357 if ($syslog_send->($buf, $numpri, $numfac)) {
361 # typically doesn't happen, since errors are rare from write().
365 # could not send, could not fallback onto a working
366 # connection method. Lose.
370 sub _syslog_send_console {
372 chop($buf); # delete the NUL from the end
373 # The console print is a method which could block
374 # so we do it in a child process and always return success
376 if (my $pid = fork) {
378 if ($options{nowait}) {
381 if (waitpid($pid, 0) >= 0) {
384 # it's possible that the caller has other
385 # plans for SIGCHLD, so let's not interfere
390 if (open(CONS, ">/dev/console")) {
391 my $ret = print CONS $buf . "\r"; # XXX: should this be \x0A ?
392 exit $ret if defined $pid;
395 exit if defined $pid;
399 sub _syslog_send_stream {
401 # XXX: this only works if the OS stream implementation makes a write
402 # look like a putmsg() with simple header. For instance it works on
403 # Solaris 8 but not Solaris 7.
404 # To be correct, it should use a STREAMS API, but perl doesn't have one.
405 return syswrite(SYSLOG, $buf, length($buf));
408 sub _syslog_send_socket {
410 return syswrite(SYSLOG, $buf, length($buf));
411 #return send(SYSLOG, $buf, 0);
414 sub _syslog_send_native {
415 my ($buf, $numpri) = @_;
416 syslog_xs($numpri, $buf);
423 # private function to translate names to numeric values
427 return $name+0 if $name =~ /^\s*\d+\s*$/;
429 $name = "LOG_$name" unless $name =~ /^LOG_/;
430 $name = "Sys::Syslog::$name";
431 # Can't have just eval { &$name } || -1 because some LOG_XXX may be zero.
432 my $value = eval { no strict 'refs'; &$name };
433 defined $value ? $value : -1;
439 # This function acts as a kind of front-end: it tries to connect to
440 # a syslog service using the selected methods, trying each one in the
444 @fallbackMethods = @connectMethods unless scalar @fallbackMethods;
446 if ($transmit_ok && $current_proto) {
447 # Retry what we were on, because it has worked in the past.
448 unshift(@fallbackMethods, $current_proto);
455 while ($proto = shift @fallbackMethods) {
457 my $fn = "connect_$proto";
458 $connected = &$fn(\@errs) if defined &$fn;
464 $current_proto = $proto;
465 my ($old) = select(SYSLOG); $| = 1; select($old);
467 @fallbackMethods = ();
468 $err_sub->(join "\n\t- ", "no connection to syslog available", @errs);
476 my $tcp = getprotobyname('tcp');
478 push @$errs, "getprotobyname failed for tcp";
482 my $syslog = getservbyname('syslog', 'tcp');
483 $syslog = getservbyname('syslogng', 'tcp') unless defined $syslog;
484 if (!defined $syslog) {
485 push @$errs, "getservbyname failed for syslog/tcp and syslogng/tcp";
491 $addr = inet_aton($host);
493 push @$errs, "can't lookup $host";
497 $addr = INADDR_LOOPBACK;
499 $addr = sockaddr_in($syslog, $addr);
501 if (!socket(SYSLOG, AF_INET, SOCK_STREAM, $tcp)) {
502 push @$errs, "tcp socket: $!";
506 setsockopt(SYSLOG, SOL_SOCKET, SO_KEEPALIVE, 1);
507 setsockopt(SYSLOG, IPPROTO_TCP, TCP_NODELAY, 1);
508 if (!connect(SYSLOG, $addr)) {
509 push @$errs, "tcp connect: $!";
513 $syslog_send = \&_syslog_send_socket;
521 my $udp = getprotobyname('udp');
523 push @$errs, "getprotobyname failed for udp";
527 my $syslog = getservbyname('syslog', 'udp');
528 if (!defined $syslog) {
529 push @$errs, "getservbyname failed for syslog/udp";
535 $addr = inet_aton($host);
537 push @$errs, "can't lookup $host";
541 $addr = INADDR_LOOPBACK;
543 $addr = sockaddr_in($syslog, $addr);
545 if (!socket(SYSLOG, AF_INET, SOCK_DGRAM, $udp)) {
546 push @$errs, "udp socket: $!";
549 if (!connect(SYSLOG, $addr)) {
550 push @$errs, "udp connect: $!";
554 # We want to check that the UDP connect worked. However the only
555 # way to do that is to send a message and see if an ICMP is returned
556 _syslog_send_socket("");
557 if (!connection_ok()) {
558 push @$errs, "udp connect: nobody listening";
562 $syslog_send = \&_syslog_send_socket;
569 # might want syslog_path to be variable based on syslog.h (if only
571 $syslog_path = '/dev/conslog' unless defined $syslog_path;
572 if (!-w $syslog_path) {
573 push @$errs, "stream $syslog_path is not writable";
576 if (!sysopen(SYSLOG, $syslog_path, 0400, O_WRONLY)) {
577 push @$errs, "stream can't open $syslog_path: $!";
580 $syslog_send = \&_syslog_send_stream;
587 $syslog_path ||= _PATH_LOG() if length _PATH_LOG();
589 if (not defined $syslog_path) {
590 push @$errs, "_PATH_LOG not available in syslog.h and no user-supplied socket path";
594 if (! -S $syslog_path) {
595 push @$errs, "$syslog_path is not a socket";
599 my $addr = sockaddr_un($syslog_path);
601 push @$errs, "can't locate $syslog_path";
604 if (!socket(SYSLOG, AF_UNIX, SOCK_STREAM, 0)) {
605 push @$errs, "unix stream socket: $!";
609 if (!connect(SYSLOG, $addr)) {
610 if (!socket(SYSLOG, AF_UNIX, SOCK_DGRAM, 0)) {
611 push @$errs, "unix dgram socket: $!";
614 if (!connect(SYSLOG, $addr)) {
615 push @$errs, "unix dgram connect: $!";
620 $syslog_send = \&_syslog_send_socket;
629 # reconstruct the numeric equivalent of the options
630 for my $opt (keys %options) {
631 $logopt += xlate($opt) if $options{$opt}
634 eval { openlog_xs($ident, $logopt, xlate($facility)) };
640 $syslog_send = \&_syslog_send_native;
645 sub connect_eventlog {
648 $syslog_xobj = Sys::Syslog::Win32::_install();
649 $syslog_send = \&Sys::Syslog::Win32::_syslog_send;
654 sub connect_console {
656 if (!-w '/dev/console') {
657 push @$errs, "console is not writable";
660 $syslog_send = \&_syslog_send_console;
664 # To test if the connection is still good, we need to check if any
665 # errors are present on the connection. The errors will not be raised
666 # by a write. Instead, sockets are made readable and the next read
667 # would cause the error to be returned. Unfortunately the syslog
668 # 'protocol' never provides anything for us to read. But with
669 # judicious use of select(), we can see if it would be readable...
671 return 1 if defined $current_proto and (
672 $current_proto eq 'native' or $current_proto eq 'console'
673 or $current_proto eq 'eventlog'
677 vec($rin, fileno(SYSLOG), 1) = 1;
678 my $ret = select $rin, undef, $rin, 0.25;
679 return ($ret ? 0 : 1);
684 $syslog_send = undef;
686 if (defined $current_proto and $current_proto eq 'native') {
690 elsif (defined $current_proto and $current_proto eq 'eventlog') {
691 $syslog_xobj->Close();
704 Sys::Syslog - Perl interface to the UNIX syslog(3) calls
712 use Sys::Syslog; # all except setlogsock(), or:
713 use Sys::Syslog qw(:DEFAULT setlogsock); # default set, plus setlogsock()
714 use Sys::Syslog qw(:standard :macros); # standard functions, plus macros
716 openlog $ident, $logopt, $facility; # don't forget this
717 syslog $priority, $format, @args;
718 $oldmask = setlogmask $mask_priority;
724 C<Sys::Syslog> is an interface to the UNIX C<syslog(3)> program.
725 Call C<syslog()> with a string priority and a list of C<printf()> args
726 just like C<syslog(3)>.
728 You can find a kind of FAQ in L<"THE RULES OF SYS::SYSLOG">. Please read
729 it before coding, and again before asking questions.
734 C<Sys::Syslog> exports the following C<Exporter> tags:
740 C<:standard> exports the standard C<syslog(3)> functions:
742 openlog closelog setlogmask syslog
746 C<:extended> exports the Perl specific functions for C<syslog(3)>:
752 C<:macros> exports the symbols corresponding to most of your C<syslog(3)>
753 macros and the C<LOG_UPTO()> and C<LOG_MASK()> functions.
754 See L<"CONSTANTS"> for the supported constants and their meaning.
758 By default, C<Sys::Syslog> exports the symbols from the C<:standard> tag.
765 =item B<openlog($ident, $logopt, $facility)>
768 C<$ident> is prepended to every message. C<$logopt> contains zero or
769 more of the options detailed below. C<$facility> specifies the part
770 of the system to report about, for example C<LOG_USER> or C<LOG_LOCAL0>:
771 see L<"Facilities"> for a list of well-known facilities, and your
772 C<syslog(3)> documentation for the facilities available in your system.
773 Check L<"SEE ALSO"> for useful links. Facility can be given as a string
776 This function will croak if it can't connect to the syslog daemon.
778 Note that C<openlog()> now takes three arguments, just like C<openlog(3)>.
780 B<You should use C<openlog()> before calling C<syslog()>.>
788 C<cons> - This option is ignored, since the failover mechanism will drop
789 down to the console automatically if all other media fail.
793 C<ndelay> - Open the connection immediately (normally, the connection is
794 opened when the first message is logged).
798 C<nofatal> - When set to true, C<openlog()> and C<syslog()> will only
799 emit warnings instead of dying if the connection to the syslog can't
804 C<nowait> - Don't wait for child processes that may have been created
805 while logging the message. (The GNU C library does not create a child
806 process, so this option has no effect on Linux.)
810 C<pid> - Include PID with each message.
816 Open the syslog with options C<ndelay> and C<pid>, and with facility C<LOCAL0>:
818 openlog($name, "ndelay,pid", "local0");
820 Same thing, but this time using the macro corresponding to C<LOCAL0>:
822 openlog($name, "ndelay,pid", LOG_LOCAL0);
825 =item B<syslog($priority, $message)>
827 =item B<syslog($priority, $format, @args)>
829 If C<$priority> permits, logs C<$message> or C<sprintf($format, @args)>
830 with the addition that C<%m> in $message or C<$format> is replaced with
831 C<"$!"> (the latest error message).
833 C<$priority> can specify a level, or a level and a facility. Levels and
834 facilities can be given as strings or as macros. When using the C<eventlog>
835 mechanism, priorities C<DEBUG> and C<INFO> are mapped to event type
836 C<informational>, C<NOTICE> and C<WARNIN> to C<warning> and C<ERR> to
837 C<EMERG> to C<error>.
839 If you didn't use C<openlog()> before using C<syslog()>, C<syslog()> will
840 try to guess the C<$ident> by extracting the shortest prefix of
841 C<$format> that ends in a C<":">.
845 syslog("info", $message); # informational level
846 syslog(LOG_INFO, $message); # informational level
848 syslog("info|local0", $message); # information level, Local0 facility
849 syslog(LOG_INFO|LOG_LOCAL0, $message); # information level, Local0 facility
855 C<Sys::Syslog> version v0.07 and older passed the C<$message> as the
856 formatting string to C<sprintf()> even when no formatting arguments
857 were provided. If the code calling C<syslog()> might execute with
858 older versions of this module, make sure to call the function as
859 C<syslog($priority, "%s", $message)> instead of C<syslog($priority,
860 $message)>. This protects against hostile formatting sequences that
861 might show up if $message contains tainted data.
866 =item B<setlogmask($mask_priority)>
868 Sets the log mask for the current process to C<$mask_priority> and
869 returns the old mask. If the mask argument is 0, the current log mask
870 is not modified. See L<"Levels"> for the list of available levels.
871 You can use the C<LOG_UPTO()> function to allow all levels up to a
872 given priority (but it only accept the numeric macros as arguments).
878 setlogmask( LOG_MASK(LOG_ERR) );
880 Log everything except informational messages:
882 setlogmask( ~(LOG_MASK(LOG_INFO)) );
884 Log critical messages, errors and warnings:
886 setlogmask( LOG_MASK(LOG_CRIT) | LOG_MASK(LOG_ERR) | LOG_MASK(LOG_WARNING) );
888 Log all messages up to debug:
890 setlogmask( LOG_UPTO(LOG_DEBUG) );
893 =item B<setlogsock($sock_type)>
895 =item B<setlogsock($sock_type, $stream_location)> (added in Perl 5.004_02)
897 Sets the socket type to be used for the next call to
898 C<openlog()> or C<syslog()> and returns true on success,
899 C<undef> on failure. The available mechanisms are:
905 C<"native"> - use the native C functions from your C<syslog(3)> library
906 (added in C<Sys::Syslog> 0.15).
910 C<"tcp"> - connect to a TCP socket, on the C<syslog/tcp> or C<syslogng/tcp>
915 C<"udp"> - connect to a UDP socket, on the C<syslog/udp> service.
919 C<"inet"> - connect to an INET socket, either TCP or UDP, tried in that order.
923 C<"unix"> - connect to a UNIX domain socket (in some systems a character
924 special device). The name of that socket is the second parameter or, if
925 you omit the second parameter, the value returned by the C<_PATH_LOG> macro
926 (if your system defines it), or F</dev/log> or F</dev/conslog>, whatever is
931 C<"stream"> - connect to the stream indicated by the pathname provided as
932 the optional second parameter, or, if omitted, to F</dev/conslog>.
933 For example Solaris and IRIX system may prefer C<"stream"> instead of C<"unix">.
937 C<"console"> - send messages directly to the console, as for the C<"cons">
938 option of C<openlog()>.
942 C<"eventlog"> - send messages to the Win32 events logger (Win32 only;
943 added in C<Sys::Syslog> 0.19).
947 A reference to an array can also be passed as the first parameter.
948 When this calling method is used, the array should contain a list of
949 mechanisms which are attempted in order.
951 The default is to try C<native>, C<tcp>, C<udp>, C<unix>, C<stream>, C<console>.
953 Giving an invalid value for C<$sock_type> will C<croak>.
957 Select the UDP socket mechanism:
961 Select the native, UDP socket then UNIX domain socket mechanisms:
963 setlogsock(["native", "udp", "unix"]);
969 Now that the "native" mechanism is supported by C<Sys::Syslog> and selected
970 by default, the use of the C<setlogsock()> function is discouraged because
971 other mechanisms are less portable across operating systems. Authors of
972 modules and programs that use this function, especially its cargo-cult form
973 C<setlogsock("unix")>, are advised to remove any occurence of it unless they
974 specifically want to use a given mechanism (like TCP or UDP to connect to
981 Closes the log file and returns true on success.
986 =head1 THE RULES OF SYS::SYSLOG
988 I<The First Rule of Sys::Syslog is:>
989 You do not call C<setlogsock>.
991 I<The Second Rule of Sys::Syslog is:>
992 You B<do not> call C<setlogsock>.
994 I<The Third Rule of Sys::Syslog is:>
995 The program crashes, C<die>s, calls C<closelog>, the log is over.
997 I<The Fourth Rule of Sys::Syslog is:>
998 One facility, one priority.
1000 I<The Fifth Rule of Sys::Syslog is:>
1003 I<The Sixth Rule of Sys::Syslog is:>
1004 No C<syslog> before C<openlog>.
1006 I<The Seventh Rule of Sys::Syslog is:>
1007 Logs will go on as long as they have to.
1009 I<The Eighth, and Final Rule of Sys::Syslog is:>
1010 If this is your first use of Sys::Syslog, you must read the doc.
1017 openlog($program, 'cons,pid', 'user');
1018 syslog('info', '%s', 'this is another test');
1019 syslog('mail|warning', 'this is a better test: %d', time);
1022 syslog('debug', 'this is the last test');
1026 openlog("$program $$", 'ndelay', 'user');
1027 syslog('notice', 'fooprogram: this is really done');
1029 Example of use of C<%m>:
1032 syslog('info', 'problem was %m'); # %m == $! in syslog(3)
1034 Log to UDP port on C<$remotehost> instead of logging locally:
1037 $Sys::Syslog::host = $remotehost;
1038 openlog($program, 'ndelay', 'user');
1039 syslog('info', 'something happened over here');
1050 C<LOG_AUDIT> - audit daemon (IRIX); falls back to C<LOG_AUTH>
1054 C<LOG_AUTH> - security/authorization messages
1058 C<LOG_AUTHPRIV> - security/authorization messages (private)
1062 C<LOG_CONSOLE> - C</dev/console> output (FreeBSD); falls back to C<LOG_USER>
1066 C<LOG_CRON> - clock daemons (B<cron> and B<at>)
1070 C<LOG_DAEMON> - system daemons without separate facility value
1074 C<LOG_FTP> - FTP daemon
1078 C<LOG_KERN> - kernel messages
1082 C<LOG_INSTALL> - installer subsystem (Mac OS X); falls back to C<LOG_USER>
1086 C<LOG_LAUNCHD> - launchd - general bootstrap daemon (Mac OS X);
1087 falls back to C<LOG_DAEMON>
1091 C<LOG_LFMT> - logalert facility; falls back to C<LOG_USER>
1095 C<LOG_LOCAL0> through C<LOG_LOCAL7> - reserved for local use
1099 C<LOG_LPR> - line printer subsystem
1103 C<LOG_MAIL> - mail subsystem
1107 C<LOG_NETINFO> - NetInfo subsystem (Mac OS X); falls back to C<LOG_DAEMON>
1111 C<LOG_NEWS> - USENET news subsystem
1115 C<LOG_NTP> - NTP subsystem (FreeBSD, NetBSD); falls back to C<LOG_DAEMON>
1119 C<LOG_RAS> - Remote Access Service (VPN / PPP) (Mac OS X);
1120 falls back to C<LOG_AUTH>
1124 C<LOG_REMOTEAUTH> - remote authentication/authorization (Mac OS X);
1125 falls back to C<LOG_AUTH>
1129 C<LOG_SECURITY> - security subsystems (firewalling, etc.) (FreeBSD);
1130 falls back to C<LOG_AUTH>
1134 C<LOG_SYSLOG> - messages generated internally by B<syslogd>
1138 C<LOG_USER> (default) - generic user-level messages
1142 C<LOG_UUCP> - UUCP subsystem
1153 C<LOG_EMERG> - system is unusable
1157 C<LOG_ALERT> - action must be taken immediately
1161 C<LOG_CRIT> - critical conditions
1165 C<LOG_ERR> - error conditions
1169 C<LOG_WARNING> - warning conditions
1173 C<LOG_NOTICE> - normal, but significant, condition
1177 C<LOG_INFO> - informational message
1181 C<LOG_DEBUG> - debug-level message
1190 =item C<Invalid argument passed to setlogsock>
1192 B<(F)> You gave C<setlogsock()> an invalid value for C<$sock_type>.
1194 =item C<eventlog passed to setlogsock, but operating system isn't Win32-compatible>
1196 B<(W)> You asked C<setlogsock()> to use the Win32 event logger but the
1197 operating system running the program isn't Win32 or does not provides Win32
1200 =item C<no connection to syslog available>
1202 B<(F)> C<syslog()> failed to connect to the specified socket.
1204 =item C<stream passed to setlogsock, but %s is not writable>
1206 B<(W)> You asked C<setlogsock()> to use a stream socket, but the given
1207 path is not writable.
1209 =item C<stream passed to setlogsock, but could not find any device>
1211 B<(W)> You asked C<setlogsock()> to use a stream socket, but didn't
1212 provide a path, and C<Sys::Syslog> was unable to find an appropriate one.
1214 =item C<tcp passed to setlogsock, but tcp service unavailable>
1216 B<(W)> You asked C<setlogsock()> to use a TCP socket, but the service
1217 is not available on the system.
1219 =item C<syslog: expecting argument %s>
1221 B<(F)> You forgot to give C<syslog()> the indicated argument.
1223 =item C<syslog: invalid level/facility: %s>
1225 B<(F)> You specified an invalid level or facility.
1227 =item C<syslog: too many levels given: %s>
1229 B<(F)> You specified too many levels.
1231 =item C<syslog: too many facilities given: %s>
1233 B<(F)> You specified too many facilities.
1235 =item C<syslog: level must be given>
1237 B<(F)> You forgot to specify a level.
1239 =item C<udp passed to setlogsock, but udp service unavailable>
1241 B<(W)> You asked C<setlogsock()> to use a UDP socket, but the service
1242 is not available on the system.
1244 =item C<unix passed to setlogsock, but path not available>
1246 B<(W)> You asked C<setlogsock()> to use a UNIX socket, but C<Sys::Syslog>
1247 was unable to find an appropriate an appropriate device.
1258 SUSv3 issue 6, IEEE Std 1003.1, 2004 edition,
1259 L<http://www.opengroup.org/onlinepubs/000095399/basedefs/syslog.h.html>
1261 GNU C Library documentation on syslog,
1262 L<http://www.gnu.org/software/libc/manual/html_node/Syslog.html>
1264 Solaris 10 documentation on syslog,
1265 L<http://docs.sun.com/app/docs/doc/816-5168/6mbb3hruo?a=view>
1267 IRIX 6.4 documentation on syslog,
1268 L<http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=0640&db=man&fname=3c+syslog>
1270 AIX 5L 5.3 documentation on syslog,
1271 L<http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.doc/libs/basetrf2/syslog.htm>
1273 HP-UX 11i documentation on syslog,
1274 L<http://docs.hp.com/en/B9106-90010/syslog.3C.html>
1276 Tru64 5.1 documentation on syslog,
1277 L<http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V51_HTML/MAN/MAN3/0193____.HTM>
1280 L<http://stratadoc.stratus.com/vos/15.1.1/r502-01/wwhelp/wwhimpl/js/html/wwhelp.htm?context=r502-01&file=ch5r502-01bi.html>
1284 I<RFC 3164 - The BSD syslog Protocol>, L<http://www.faqs.org/rfcs/rfc3164.html>
1285 -- Please note that this is an informational RFC, and therefore does not
1286 specify a standard of any kind.
1288 I<RFC 3195 - Reliable Delivery for syslog>, L<http://www.faqs.org/rfcs/rfc3195.html>
1292 I<Syslogging with Perl>, L<http://lexington.pm.org/meetings/022001.html>
1297 L<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wes/wes/windows_event_log.asp>
1300 =head1 AUTHORS & ACKNOWLEDGEMENTS
1302 Tom Christiansen E<lt>F<tchrist (at) perl.com>E<gt> and Larry Wall
1303 E<lt>F<larry (at) wall.org>E<gt>.
1305 UNIX domain sockets added by Sean Robinson
1306 E<lt>F<robinson_s (at) sc.maricopa.edu>E<gt> with support from Tim Bunce
1307 E<lt>F<Tim.Bunce (at) ig.co.uk>E<gt> and the C<perl5-porters> mailing list.
1309 Dependency on F<syslog.ph> replaced with XS code by Tom Hughes
1310 E<lt>F<tom (at) compton.nu>E<gt>.
1312 Code for C<constant()>s regenerated by Nicholas Clark E<lt>F<nick (at) ccl4.org>E<gt>.
1314 Failover to different communication modes by Nick Williams
1315 E<lt>F<Nick.Williams (at) morganstanley.com>E<gt>.
1317 Extracted from core distribution for publishing on the CPAN by
1318 SE<eacute>bastien Aperghis-Tramoni E<lt>sebastien (at) aperghis.netE<gt>.
1320 XS code for using native C functions borrowed from C<L<Unix::Syslog>>,
1321 written by Marcus Harnisch E<lt>F<marcus.harnisch (at) gmx.net>E<gt>.
1323 Yves Orton suggested and helped for making C<Sys::Syslog> use the native
1324 event logger under Win32 systems.
1326 Jerry D. Hedden and Reini Urban provided greatly appreciated help to
1327 debug and polish C<Sys::Syslog> under Cygwin.
1332 Please report any bugs or feature requests to
1333 C<bug-sys-syslog (at) rt.cpan.org>, or through the web interface at
1334 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sys-Syslog>.
1335 I will be notified, and then you'll automatically be notified of progress on
1336 your bug as I make changes.
1341 You can find documentation for this module with the perldoc command.
1345 You can also look for information at:
1349 =item * AnnoCPAN: Annotated CPAN documentation
1351 L<http://annocpan.org/dist/Sys-Syslog>
1353 =item * CPAN Ratings
1355 L<http://cpanratings.perl.org/d/Sys-Syslog>
1357 =item * RT: CPAN's request tracker
1359 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Sys-Syslog>
1363 L<http://search.cpan.org/dist/Sys-Syslog/>
1365 =item * Kobes' CPAN Search
1367 L<http://cpan.uwinnipeg.ca/dist/Sys-Syslog>
1369 =item * Perl Documentation
1371 L<http://perldoc.perl.org/Sys/Syslog.html>
1378 This program is free software; you can redistribute it and/or modify it
1379 under the same terms as Perl itself.
1385 Notes for the future maintainer (even if it's still me..)
1386 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1388 Using Google Code Search, I search who on Earth was relying on $host being
1389 public. It found 5 hits:
1391 * First was inside Indigo Star Perl2exe documentation. Just an old version
1395 * One real hit was inside DalWeathDB, a weather related program. It simply
1398 $Sys::Syslog::host = '127.0.0.1';
1400 - L<http://www.gallistel.net/nparker/weather/code/>
1403 * Two hits were in TPC, a fax server thingy. It does a
1405 $Sys::Syslog::host = $TPC::LOGHOST;
1407 but also has this strange piece of code:
1409 # work around perl5.003 bug
1410 sub Sys::Syslog::hostname {}
1412 I don't know what bug the author referred to.
1414 - L<http://www.tpc.int/>
1415 - L<ftp://ftp.tpc.int/tpc/server/UNIX/>
1416 - L<ftp://ftp-usa.tpc.int/pub/tpc/server/UNIX/>
1419 * Last hit was in Filefix, which seems to be a FIDOnet mail program (!).
1420 This one does not use $host, but has the following piece of code:
1422 sub Sys::Syslog::hostname
1428 I guess this was a more elaborate form of the previous bit, maybe because
1429 of a bug in Sys::Syslog back then?
1431 - L<ftp://ftp.kiae.su/pub/unix/fido/>