SK.gnome.dwarf.log
Class SyslogLogger

java.lang.Object
  extended bySK.gnome.dwarf.GenericService
      extended bySK.gnome.dwarf.thread.ThreadService
          extended bySK.gnome.dwarf.log.GenericLogger
              extended bySK.gnome.dwarf.log.SyslogLogger
All Implemented Interfaces:
Logger, java.lang.Runnable, Service

public class SyslogLogger
extends GenericLogger

The syslog logger.

This logger forwards the log messages to a syslog server. The syslog server is a standard TCP/IP network service described by the RFC 3164. It listens on UDP port 514 for incoming event notification messages, collects them and either writes to a physical medium or forwards to another syslog server.

The address of the remote syslog server can be specified via the setAddress(InetAddress) method. The setFacility(int) method specifies the facility of the generated syslog messages. The default facility is local0, but you may use another facility, if desired.

See the write(long, String, LogLevel, String, Throwable) method for detailed information about the logging process.


Field Summary
protected  java.net.InetAddress address
          The address of the remote syslog server.
protected  int facility
          The facility of generated syslog messages.
protected  boolean hostname
          Whether to send the HOSTNAME header field.
protected  int port
          The UDP port on the remote syslog server.
protected  java.net.DatagramSocket socket
          The datagram socket for sending the messages.
 
Fields inherited from class SK.gnome.dwarf.log.GenericLogger
facilities, levels, lock, LOG_FACILITY_ALL
 
Fields inherited from class SK.gnome.dwarf.thread.ThreadService
daemon, thread
 
Fields inherited from class SK.gnome.dwarf.GenericService
initParameters, parent
 
Fields inherited from interface SK.gnome.dwarf.Service
INITIALIZED, LOG_DEBUG, LOG_ERROR, LOG_FATAL, LOG_INFO, LOG_TRACE, LOG_WARN, LOG_XFER, SHUTDOWN, STARTED, STOPPED
 
Constructor Summary
SyslogLogger(java.lang.String name)
          Creates a new SyslogLogger.
 
Method Summary
 void init(Server parent)
          Inititalizes the service.
 void setAddress(java.net.InetAddress address)
          Sets the address of the remote syslog server.
 void setFacility(int facility)
          Sets the facility of the generated syslog messages.
 void setPort(int port)
          Sets the UDP port of the remote syslog server.
 void setSendHostname(boolean enable)
          Enables or disables sending of HOSTNAME field in the header.
 void shutdown()
          Shuts down the service and closes the datagram socket.
protected  boolean wouldLog(java.lang.String facility, LogLevel level)
          Decides whether the logger would log a message with the given loggging level and facility.
protected  void write(long time, java.lang.String facility, LogLevel level, java.lang.String message, java.lang.Throwable error)
          Sends the message to the syslog server.
 
Methods inherited from class SK.gnome.dwarf.log.GenericLogger
enableThread, getLastAccessed, log, log, loop, setFacilities, setLevels, setMarkInterval, setRepeatedInterval, start
 
Methods inherited from class SK.gnome.dwarf.thread.ThreadService
finish, run, setDaemon, stop
 
Methods inherited from class SK.gnome.dwarf.GenericService
getAuthenticator, getAuthFacility, getFullName, getInitParameter, getInitParameterNames, getLogFacility, getLogger, getName, getParameters, getPrincipal, getShutdownTimeout, getState, log, log, login, logout, setAuthFacility, setInitParameters, setLogFacility, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

socket

protected java.net.DatagramSocket socket
The datagram socket for sending the messages.

It is initialized in the init(Server) method. The access to the socket must be synchronized.


address

protected java.net.InetAddress address
The address of the remote syslog server.

See Also:
setAddress(InetAddress)

port

protected int port
The UDP port on the remote syslog server.

Default: 514

See Also:
setPort(int)

facility

protected int facility
The facility of generated syslog messages.

Default: 16

See Also:
setFacility(int)

hostname

protected boolean hostname
Whether to send the HOSTNAME header field.

Default: true

See Also:
setSendHostname(boolean)
Constructor Detail

SyslogLogger

public SyslogLogger(java.lang.String name)
Creates a new SyslogLogger.

Method Detail

setAddress

public void setAddress(java.net.InetAddress address)
Sets the address of the remote syslog server.

This is the address of the syslog server the messages will be routed to.

Parameters:
address - the inet address
See Also:
address

setPort

public void setPort(int port)
Sets the UDP port of the remote syslog server.

Normally, the default UDP port number 514 is used for the syslog service. You should never change this value.

Parameters:
port - the port number
See Also:
port

setFacility

public void setFacility(int facility)
Sets the facility of the generated syslog messages.

Typically, one of the local use facilities or the user level facility should be used by the services which have not been assigned a concrete facility number. The following facilities are defined byt the RFC 3164:

         Numerical             Facility
           Code
 
            0             kernel messages
            1             user-level messages
            2             mail system
            3             system daemons
            4             security/authorization messages (note 1)
            5             messages generated internally by syslogd
            6             line printer subsystem
            7             network news subsystem
            8             UUCP subsystem
            9             clock daemon (note 2)
           10             security/authorization messages (note 1)
           11             FTP daemon
           12             NTP subsystem
           13             log audit (note 1)
           14             log alert (note 1)
           15             clock daemon (note 2)
           16             local use 0  (local0)
           17             local use 1  (local1)
           18             local use 2  (local2)
           19             local use 3  (local3)
           20             local use 4  (local4)
           21             local use 5  (local5)
           22             local use 6  (local6)
           23             local use 7  (local7)
 

The SyslogLogger uses local0 facility by default.

Parameters:
facility - the facility code
See Also:
facility

setSendHostname

public void setSendHostname(boolean enable)
Enables or disables sending of HOSTNAME field in the header.

The syslog protocol description states that the HOSTNAME field should be send along with the TIMESTAMP info in the message packet header. However, some syslog servers would log this hostname information twice, so this method may be used to prevent such information redundancy. The sending of hostname is enabled by default for conformance with the RFC document.

Parameters:
enable - true to enable sending of hostname
See Also:
hostname

init

public void init(Server parent)
          throws ServiceException
Inititalizes the service.

The new DatagramSocket is created and connected to the address and port. The socket reference is then assigned to the socket field.

Specified by:
init in interface Service
Overrides:
init in class GenericService
Throws:
ServiceException

shutdown

public void shutdown()
Shuts down the service and closes the datagram socket.

Specified by:
shutdown in interface Service
Overrides:
shutdown in class GenericLogger

wouldLog

protected boolean wouldLog(java.lang.String facility,
                           LogLevel level)
Decides whether the logger would log a message with the given loggging level and facility.

It first checks whether the UDP socket has been initialized and then calls the overriden wouldLog method and returns its result.

Overrides:
wouldLog in class GenericLogger
Parameters:
facility - the logging facility
level - the logging level
Returns:
true if the logger would log the message, false otherwise

write

protected void write(long time,
                     java.lang.String facility,
                     LogLevel level,
                     java.lang.String message,
                     java.lang.Throwable error)
Sends the message to the syslog server.

This method formats the message and sends it to the remote syslog server via the socket.

The Dwarf logging levels are mapped to severity levels defined by the protocol specification as follows:

         Dwarf         Syslog           Severity
         Level          Code 
 
       LOG_FATAL  =<     0       Emergency: system is unusable
                         1       Alert: action must be taken immediately
                         2       Critical: critical conditions
       LOG_ERROR  =<     3       Error: error conditions
       LOG_WARN   =<     4       Warning: warning conditions
                         5       Notice: normal but significant condition
       LOG_INFO   =<     6       Informational: informational messages
       LOG_DEBUG  =<     7       Debug: debug-level messages
 

Since the length of a syslog datagram packet is limited to 1024 bytes, any messages longer than this limit will be truncated to 1kB automatically.

Specified by:
write in class GenericLogger
Parameters:
time - the current time of the logged event
facility - the log facility
level - the log level
message - the message
error - the error (optional)
See Also:
GenericLogger.log(String, LogLevel, String), GenericLogger.log(String, LogLevel, String, Throwable)


Copyright (c) 1999-2005, Gnome Ltd. All rights reserved.