| FuguLib::Log(3p) | Perl Library Manual | FuguLib::Log(3p) |
NAME
FuguLib::Log —
logging to syslog, standard error, or nowhere
SYNOPSIS
use FuguLib::Log;
my $log = FuguLib::Log->new(
mode => 'syslog',
ident => 'mydaemon',
level => 'info',
facility => 'daemon',
);
$log->info('listening on port %d', $port);
$log->err('cannot open %s: %s', $path, $!);
DESCRIPTION
FuguLib::Log gives a program one logging
interface whether it is running as a daemon or in the foreground. The
destination is chosen once, when the logger is created, and every call site
is written the same way regardless.
Messages below the configured level are discarded. The levels,
from lowest to highest, are debug,
info, notice,
warning, error and
crit.
new
new(%args)
creates a logger. The arguments are as follows:
- mode
- Where messages go. One of:
syslog- Through
syslog(3).
openlog(3)
is called immediately with the
ndelayandpidoptions. stderr- To standard error, one line per message, prefixed with a local-time stamp and the upper-cased level.
quiet- Nowhere. Messages are discarded before the level is even considered.
The default is
stderr. - level
- Lowest level to emit. The default is
info. - ident
- Program name passed to
openlog(3). The
default is ‘
fugulib’. Ignored unless mode issyslog. - facility
- Syslog facility, either a
Sys::Syslog(3p)
constant or one of the names
daemon,userorlocal0throughlocal7. The default isLOG_DAEMON.
debug, info, notice, warning, warn, error, err, crit
$log->info($fmt, @args);
Log one message at the level named by the method. When @args is non-empty the message is $fmt formatted through sprintf(3); otherwise $fmt is used literally.
warn()
is a synonym for
warning()
and
err()
for
error(),
so that either the syslog or the English spelling reads naturally at the
call site.
set_level
set_level($level)
changes the lowest level to emit on an existing logger. Anything not
recognised as a level name is treated as info.
RETURN VALUES
new() returns a logger object. The logging
methods and set_level() have no useful return
value.
EXAMPLES
Log to standard error in the foreground and to syslog otherwise, without changing any call site:
my $log = FuguLib::Log->new(
mode => $foreground ? 'stderr' : 'syslog',
ident => 'mydaemon',
level => $verbose ? 'debug' : 'info',
);
ERRORS
new() dies if mode
is not one of the three names above. An unrecognised
level or facility is not an
error: the level falls back to info and the facility
to LOG_DAEMON.
SEE ALSO
syslog(3), FuguLib::Daemon(3p), Sys::Syslog(3p), syslog.conf(5)
AUTHORS
Dick Olsson <hi@senzilla.io>
CAVEATS
The format string is passed to
sprintf(3) only
when arguments follow it. A message that happens to contain a percent sign
is therefore safe on its own but not once an argument is added, so log
variable text as ‘%s’ rather than
interpolating it into the format.
A single process should create one syslog-mode logger. openlog(3) and closelog(3) act on process-wide state, so a second logger going out of scope closes the connection the first one is still using.
| July 27, 2026 | OpenBSD |