You are viewing the version of this documentation from Perl 5.26.1. View the latest version

CONTENTS

NAME

Sys::Syslog - Perl interface to the UNIX syslog(3) calls

VERSION

This is the documentation of version 0.35

SYNOPSIS

use Sys::Syslog;                        # all except setlogsock()
use Sys::Syslog qw(:standard :macros);  # standard functions & macros

openlog($ident, $logopt, $facility);    # don't forget this
syslog($priority, $format, @args);
$oldmask = setlogmask($mask_priority);
closelog();

DESCRIPTION

Sys::Syslog is an interface to the UNIX syslog(3) program. Call syslog() with a string priority and a list of printf() args just like syslog(3).

EXPORTS

Sys::Syslog exports the following Exporter tags:

By default, Sys::Syslog exports the symbols from the :standard tag.

FUNCTIONS

openlog($ident, $logopt, $facility)

Opens the syslog. $ident is prepended to every message. $logopt contains zero or more of the options detailed below. $facility specifies the part of the system to report about, for example LOG_USER or LOG_LOCAL0: see "Facilities" for a list of well-known facilities, and your syslog(3) documentation for the facilities available in your system. Check "SEE ALSO" for useful links. Facility can be given as a string or a numeric macro.

This function will croak if it can't connect to the syslog daemon.

Note that openlog() now takes three arguments, just like openlog(3).

You should use openlog() before calling syslog().

Options

  • cons - This option is ignored, since the failover mechanism will drop down to the console automatically if all other media fail.

  • ndelay - Open the connection immediately (normally, the connection is opened when the first message is logged).

  • noeol - When set to true, no end of line character (\n) will be appended to the message. This can be useful for some syslog daemons. Added in Sys::Syslog 0.29.

  • nofatal - When set to true, openlog() and syslog() will only emit warnings instead of dying if the connection to the syslog can't be established. Added in Sys::Syslog 0.15.

  • nonul - When set to true, no NUL character (\0) will be appended to the message. This can be useful for some syslog daemons. Added in Sys::Syslog 0.29.

  • nowait - Don't wait for child processes that may have been created while logging the message. (The GNU C library does not create a child process, so this option has no effect on Linux.)

  • perror - Write the message to standard error output as well to the system log. Added in Sys::Syslog 0.22.

  • pid - Include PID with each message.

Examples

Open the syslog with options ndelay and pid, and with facility LOCAL0:

openlog($name, "ndelay,pid", "local0");

Same thing, but this time using the macro corresponding to LOCAL0:

openlog($name, "ndelay,pid", LOG_LOCAL0);
syslog($priority, $message)
syslog($priority, $format, @args)

If $priority permits, logs $message or sprintf($format, @args) with the addition that %m in $message or $format is replaced with "$!" (the latest error message).

$priority can specify a level, or a level and a facility. Levels and facilities can be given as strings or as macros. When using the eventlog mechanism, priorities DEBUG and INFO are mapped to event type informational, NOTICE and WARNING to warning and ERR to EMERG to error.

If you didn't use openlog() before using syslog(), syslog() will try to guess the $ident by extracting the shortest prefix of $format that ends in a ":".

Examples

# informational level
syslog("info", $message);
syslog(LOG_INFO, $message);

# information level, Local0 facility
syslog("info|local0", $message);
syslog(LOG_INFO|LOG_LOCAL0, $message);
Note

Sys::Syslog version v0.07 and older passed the $message as the formatting string to sprintf() even when no formatting arguments were provided. If the code calling syslog() might execute with older versions of this module, make sure to call the function as syslog($priority, "%s", $message) instead of syslog($priority, $message). This protects against hostile formatting sequences that might show up if $message contains tainted data.

setlogmask($mask_priority)

Sets the log mask for the current process to $mask_priority and returns the old mask. If the mask argument is 0, the current log mask is not modified. See "Levels" for the list of available levels. You can use the LOG_UPTO() function to allow all levels up to a given priority (but it only accept the numeric macros as arguments).

Examples

Only log errors:

setlogmask( LOG_MASK(LOG_ERR) );

Log everything except informational messages:

setlogmask( ~(LOG_MASK(LOG_INFO)) );

Log critical messages, errors and warnings:

setlogmask( LOG_MASK(LOG_CRIT)
          | LOG_MASK(LOG_ERR)
          | LOG_MASK(LOG_WARNING) );

Log all messages up to debug:

setlogmask( LOG_UPTO(LOG_DEBUG) );
setlogsock()

Sets the socket type and options to be used for the next call to openlog() or syslog(). Returns true on success, undef on failure.

Being Perl-specific, this function has evolved along time. It can currently be called as follow:

  • setlogsock($sock_type)

  • setlogsock($sock_type, $stream_location) (added in Perl 5.004_02)

  • setlogsock($sock_type, $stream_location, $sock_timeout) (added in Sys::Syslog 0.25)

  • setlogsock(\%options) (added in Sys::Syslog 0.28)

The available options are:

  • type - equivalent to $sock_type, selects the socket type (or "mechanism"). An array reference can be passed to specify several mechanisms to try, in the given order.

  • path - equivalent to $stream_location, sets the stream location. Defaults to standard Unix location, or _PATH_LOG.

  • timeout - equivalent to $sock_timeout, sets the socket timeout in seconds. Defaults to 0 on all systems except Mac OS X where it is set to 0.25 sec.

  • host - sets the hostname to send the messages to. Defaults to the local host.

  • port - sets the TCP or UDP port to connect to. Defaults to the first standard syslog port available on the system.

The available mechanisms are:

  • "native" - use the native C functions from your syslog(3) library (added in Sys::Syslog 0.15).

  • "eventlog" - send messages to the Win32 events logger (Win32 only; added in Sys::Syslog 0.19).

  • "tcp" - connect to a TCP socket, on the syslog/tcp or syslogng/tcp service. See also the host, port and timeout options.

  • "udp" - connect to a UDP socket, on the syslog/udp service. See also the host, port and timeout options.

  • "inet" - connect to an INET socket, either TCP or UDP, tried in that order. See also the host, port and timeout options.

  • "unix" - connect to a UNIX domain socket (in some systems a character special device). The name of that socket is given by the path option or, if omitted, the value returned by the _PATH_LOG macro (if your system defines it), /dev/log or /dev/conslog, whichever is writable.

  • "stream" - connect to the stream indicated by the path option, or, if omitted, the value returned by the _PATH_LOG macro (if your system defines it), /dev/log or /dev/conslog, whichever is writable. For example Solaris and IRIX system may prefer "stream" instead of "unix".

  • "pipe" - connect to the named pipe indicated by the path option, or, if omitted, to the value returned by the _PATH_LOG macro (if your system defines it), or /dev/log (added in Sys::Syslog 0.21). HP-UX is a system which uses such a named pipe.

  • "console" - send messages directly to the console, as for the "cons" option of openlog().

The default is to try native, tcp, udp, unix, pipe, stream, console. Under systems with the Win32 API, eventlog will be added as the first mechanism to try if Win32::EventLog is available.

Giving an invalid value for $sock_type will croak.

Examples

Select the UDP socket mechanism:

setlogsock("udp");

Send messages using the TCP socket mechanism on a custom port:

setlogsock({ type => "tcp", port => 2486 });

Send messages to a remote host using the TCP socket mechanism:

setlogsock({ type => "tcp", host => $loghost });

Try the native, UDP socket then UNIX domain socket mechanisms:

setlogsock(["native", "udp", "unix"]);
Note

Now that the "native" mechanism is supported by Sys::Syslog and selected by default, the use of the setlogsock() function is discouraged because other mechanisms are less portable across operating systems. Authors of modules and programs that use this function, especially its cargo-cult form setlogsock("unix"), are advised to remove any occurrence of it unless they specifically want to use a given mechanism (like TCP or UDP to connect to a remote host).