|
Simple syslogd Message Logging Utility Classesby Rajesh Ramchandani(June, 2002) We want to hear from you! Please send us your FEEDBACK. The following tool may contain actual software programs in source code form. This source code is made available for developers to use as needed, pursuant to the terms and conditions of this license. You can download this tool (SysLogger) and its corresponding files here.
This document describes the simplest utility classes that can be used in a distributed application environment to log debugging messages to a syslog server. The document highlights the use of severity levels and logging facilities that can be used in various error environments to assist debugging of Java[tm] platform clients. Introduction Almost all developers use trace methods to log information to local log files for debugging purposes. Every individual developer may have his/her own way of using the log files. Not only could the format of the log files vary, but also the level of information, severity of information, and origin of the log files. Log files are the best way to debug problems as debuggers may not always be available or applicable. Problems arise when the application runs on embedded devices where there is very limited local non-volatile storage space to store the log files, and also when diskless devices (for example, network applicances) or PCs are deployed in numbers ranging from hundreds to thousands. Using network storage space or disk space on the server is a solution, but writing log files across the network is bound to slow down the performance. The message logging server, syslogd, helps in situations like these. syslogd reads and forwards system messages to the appropriate log files and/or users, depending upon the priority of a message and the system facility from which it originates. A configuration file controls where messages are forwarded. The syslogd server is a standard implementation of the message logging server available on most operating environments. Logging facilities Typically, messages logged to the syslog will have a message header and a message body. The message header consists of a logging facility indicator, a severity level indicator, and a tag string. It may optionally contain timestamp, client IP address or in some cases the process id. Possible values of severity level include: LOG_EMERG
LOG_ALERT
LOG_CRIT
LOG_ERR
LOG_WARNING
LOG_NOTICE
LOG_INFO
LOG_DEBUG
The facility indicates the application or system component generating the message. Possible facility values include: LOG_USER
LOG_MAIL
LOG_AUTH
The utility classes can be modified to add any additional desired facilities and severity levels to suit the appropriate application environment. The SysLogger utility class source provided is an example of using the syslog facility. syslog SysLogger API Here's the list of the methods that syslog SysLogger provides. This is Javadoc[tm] software documentation generated from the current syslog SysLogger utility class code. Logging Mechanism Here's the sample application code which uses syslog SysLogger API to log exceptions it receives during Runtime to the syslog server. import SysLogger;
public class LogTest { public static void main(String args[]) { String msg = "";
l.openLog("LogTest", "LOG_IP"); //Try to open a non-existing file to get exception try {
try {
setSysLogger() : call setSysLogger() method to choose the syslogd server that will be used for logging. The application developer may choose to close the current logger or syslog server and choose another syslog server to log messages. This gives enough flexibility to the application developer to send different messages to different servers in distributed application environments. openLog() : The application should open the SysLogger by calling openLog() on the SysLogger object. If log is not opened, it throws LogNotOpenedException which should be handled by the application. SysLogger provides a method called isOpened() to check if the log has been opened or not. sysLog() : Messages to be logged are sent to the selected syslog server using sysLog(). It specifies the severity levels for the message along with the message. Syslog server will log these messages to the specified log files in syslogd's configuration file (typically /etc/syslog.conf). Here's an example of a log message from the above application: The syslog server was configured to send messages to /var/adm/messages on a system running the Solaris[tm] Operating Environment. Feb 21 17:07:34 frsico LogTest: LOG_ALERT: file.txt (No such file or directory) The message has LogTest tag prepended, the specified severity level LOG_ALERT, timestamp, name of the operating system, and the actual message "file not found (file.txt)". Source code for SysLogger is available here and can be modified for custom use in various environments. Different facilities and log severites can be implemented accordingly. Javadoc[tm] software documentation for the SysLogger utility class is available here. DOC ID #1133 | |||||||||||||||||||||||||||||||||||||||