Most systems have the
logger utility, which knows how to talk to
syslogd. It allows you to set log level (severity), facility name, specify the log file to write to, send to
syslogd on a remote host, write messages to
STDERR as well as to the system log.
The logging semantics are not quite the same as those provided by tools like
log4j, but by combining the
facility.level settings with message tags, you can achieve something very close.
NOTE: These examples use the FreeBSD version of
logger. Your system may have different options, so read your local documentation!
logger -p local3.info -f /var/log/messages -t MY_LOG_TAG "something interesting happened"
This will send the message to be logged in
/var/log/messages with a severity of
info, in the
local3 facility. It includes a tag (
-t MY_LOG_TAG), which is included in each line. Tags are useful for extracting log entries with
logger -h loghost -p mail.crit -s -f /var/log/mail "an unrecoverable error has occurred"
This one sends the message with severity
crit in the
mail facility to the remote machine
loghost, to be logged in
-s causes the message to be printed on the the script's
STDERR as well as sending it to be logged.