dsto.dfc.logging
Class Log

java.lang.Object
  extended by dsto.dfc.logging.Log

public final class Log
extends Object

A basic application log. Log events by default go to console, but can be redirected via listeners and adapters.

Filtering

Events sent to the log are automatically printed to the console. This can be controlled by changing the filter obained via getFilter() or using the shortcuts such as enableAll().

The initial set of enabled console events can also be controlled via the system properties dfc.log.console.show and dfc.log.console.hide. For example, to show only WARNING and ALARM events, you could use "-Ddfc.log.console.show=WARNING,ALARM" on the command line.

Example usage:


      Log.info ("Starting application", this);

      try
      {
        ...
      } catch (Exception ex)
      {
        Log.alarm ("Unexpected exception", this, ex);
      }

 

Version:
$Revision: 1.49 $
See Also:
FileLogger, LogListener, LogMessageBuffer

Field Summary
static int ALARM
           
static int DIAGNOSTIC
           
static int INFO
           
static int INTERNAL_ERROR
           
static int PERFORMANCE
           
static int SYSTEM_EVENT
           
static int TRACE
           
static int WARNING
           
 
Method Summary
static void add(int type, String messageStr, Object source)
          Add a message to the log.
static void add(int type, String messageStr, Object source, Throwable exception)
          Add a message to the log.
static void add(LogEvent event)
          Log an existing log event.
static void addLogListener(LogListener l)
           
static void alarm(String messageStr, Object source)
          An alarm is for serious errors that indicate the program has failed in some non-recoverable way.
static void alarm(String messageStr, Object source, Throwable ex)
          An alarm is for serious errors that indicate the program has failed in some non-recoverable way.
static String applicationName()
          Get the application name, or null of none set.
static void diagnostic(String messageStr, Object source)
          A diagnostic is something that is likely to be useful for diagnosing problems, and thus is more interesting than a trace, but si not an actual warning of anything going wrong.
static void diagnostic(String messageStr, Object source, Throwable ex)
          A diagnostic is something that is likely to be useful for diagnosing problems, and thus is more interesting than a trace, but si not an actual warning of anything going wrong.
static void disableAll()
          Shortcut for disabling printing all log events from the console.
static void enableAll()
          Shortcut for enabling printing all log events to the console.
static void enableAwtErrorLogging()
          Enable redirection of unhandled exceptions in the AWT event thread to the log.
protected static void fireMessageReceived(LogEvent e)
           
static String getDateString(Date time)
          Generate a string from a date formatted in the same way as the logger.
static PrintWriter getErrorOutput()
           
static LogFilter getFilter()
          Get the filter for sending log events to the console.
static PrintWriter getInfoOutput()
           
static String getTypeString(int type)
          Get a string for a given log message type (INFO, WARNING, etc).
static void info(String messageStr, Object source)
          An info message is for the information that may be of interest to the user, rather than a trace for the developer.
static void internalError(String messageStr, Object source)
          Indicates some extremely serious error has occurred, usually requiring immediate shutdown of the system.
static void internalError(String messageStr, Object source, Throwable ex)
          Indicates some extremely serious error has occurred, usually requiring immediate shutdown of the system.
static boolean isEnabled(int type)
           
static void printException(PrintWriter str, Throwable exception)
           
static boolean printExceptionMessage(PrintWriter str, Throwable exception, int maxLength)
           
static void printExceptionTrace(PrintWriter str, Throwable exception, int maxLength)
          Prints the exception stack trace.
static boolean printMessage(PrintWriter str, Date time, DateFormat dateFormat, int type, String messageStr)
           
static boolean printMessage(PrintWriter logWriter, Date time, DateFormat dateFormat, int type, String messageStr, int maxLength)
          Prints a message to the supplied PrintWriter instance.
static boolean printMessage(PrintWriter str, Date time, int type, String messageStr)
           
static void readLogFilterSystemProperty()
          Read the dfc.log.console.show and dfc.log.console.hide system properties and update the filter settings.
static void removeLogListener(LogListener l)
           
static void setApplicationName(String newValue)
          Set the user-readable application name.
static void setEnabled(int type, boolean enabled)
          Shortcut to set filtering for sending log events to the console.
static void setErrorOutput(PrintWriter newValue)
          Set the stream that error messages are echoed to.
static void setInfoOutput(PrintWriter newValue)
          Set the stream that info (non-error) messages are echoed to.
static void setOutput(PrintWriter newOutput)
          Sets both the info and error output streams that log messages are echoed to.
static int stringToType(String typeName)
          Get the type code for a log entry type.
static String toLogString(LogEvent event)
          Generate a string from a log event, minus the application and date info.
static void trace(String messageStr, Object source)
          A trace is used to show the current state of the program for debugging purposes.
static void trace(String messageStr, Object source, Throwable ex)
          A trace is used to show the current state of the program for debugging purposes.
static String typeToString(int type)
           
static void warn(String messageStr, Object source)
          A warning is an alert that something anomalous has happened that *may* indicate something has gone wrong.
static void warn(String messageStr, Object source, Throwable ex)
          A warning is an alert that something anomalous has happened that *may* indicate something has gone wrong.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TRACE

public static final int TRACE
See Also:
Constant Field Values

INFO

public static final int INFO
See Also:
Constant Field Values

WARNING

public static final int WARNING
See Also:
Constant Field Values

ALARM

public static final int ALARM
See Also:
Constant Field Values

PERFORMANCE

public static final int PERFORMANCE
See Also:
Constant Field Values

DIAGNOSTIC

public static final int DIAGNOSTIC
See Also:
Constant Field Values

SYSTEM_EVENT

public static final int SYSTEM_EVENT
See Also:
Constant Field Values

INTERNAL_ERROR

public static final int INTERNAL_ERROR
See Also:
Constant Field Values
Method Detail

readLogFilterSystemProperty

public static void readLogFilterSystemProperty()
Read the dfc.log.console.show and dfc.log.console.hide system properties and update the filter settings. This is called on class load, but clients may call this if the system properties have changed.


setApplicationName

public static void setApplicationName(String newValue)
Set the user-readable application name. If set, this is prefixed to all log events sent to the console to identify the application's output.


applicationName

public static String applicationName()
Get the application name, or null of none set.

See Also:
setApplicationName(String)

enableAwtErrorLogging

public static void enableAwtErrorLogging()
Enable redirection of unhandled exceptions in the AWT event thread to the log.


setOutput

public static void setOutput(PrintWriter newOutput)
Sets both the info and error output streams that log messages are echoed to. If null, no echo is done.

See Also:
setInfoOutput(PrintWriter), setErrorOutput(PrintWriter)

setInfoOutput

public static void setInfoOutput(PrintWriter newValue)
Set the stream that info (non-error) messages are echoed to.

See Also:
setErrorOutput(PrintWriter)

getInfoOutput

public static PrintWriter getInfoOutput()

setErrorOutput

public static void setErrorOutput(PrintWriter newValue)
Set the stream that error messages are echoed to.

See Also:
setInfoOutput(PrintWriter)

getErrorOutput

public static PrintWriter getErrorOutput()

getFilter

public static LogFilter getFilter()
Get the filter for sending log events to the console.


setEnabled

public static void setEnabled(int type,
                              boolean enabled)
Shortcut to set filtering for sending log events to the console.


isEnabled

public static boolean isEnabled(int type)

disableAll

public static void disableAll()
Shortcut for disabling printing all log events from the console.


enableAll

public static void enableAll()
Shortcut for enabling printing all log events to the console.


getDateString

public static String getDateString(Date time)
Generate a string from a date formatted in the same way as the logger.


getTypeString

public static String getTypeString(int type)
Get a string for a given log message type (INFO, WARNING, etc). This is a human-readable string, se also typeToString(int).


stringToType

public static int stringToType(String typeName)
                        throws IllegalArgumentException
Get the type code for a log entry type.

Parameters:
typeName - One of "TRACE", "INFO", "WARNING", "ALARM", "PERFORMANCE", "DIAGNOSTIC", "SYSTEM_EVENT", "INTERNAL_ERROR"
Returns:
The log entry type "WARN" yields Log.WARN.
Throws:
IllegalArgumentException - if typeName is not valid.
See Also:
typeToString(int)

typeToString

public static String typeToString(int type)

add

public static void add(LogEvent event)
Log an existing log event. Note: this currently rewrites the event's time to the current time (this is likely to change at some point, do not rely on it).


add

public static void add(int type,
                       String messageStr,
                       Object source)
Add a message to the log.

Parameters:
type - The type of message: TRACE, INFO, WARNING, ALARM, etc.
messageStr - The message to add to the log.
source - The source of the message.

add

public static void add(int type,
                       String messageStr,
                       Object source,
                       Throwable exception)
Add a message to the log. The message is printed out to the console. If the message is too long and the truncate flag is turned on, the message is truncated before being be printed.

Parameters:
type - The type of message: TRACE, INFO, WARNING, ALARM, etc.
messageStr - The message to add to the log.
source - The source of the message.
exception - The exception that relates to the message.

printMessage

public static boolean printMessage(PrintWriter str,
                                   Date time,
                                   int type,
                                   String messageStr)

printMessage

public static boolean printMessage(PrintWriter str,
                                   Date time,
                                   DateFormat dateFormat,
                                   int type,
                                   String messageStr)

printMessage

public static boolean printMessage(PrintWriter logWriter,
                                   Date time,
                                   DateFormat dateFormat,
                                   int type,
                                   String messageStr,
                                   int maxLength)
Prints a message to the supplied PrintWriter instance. The message is truncated if it exceeds maxLength

Parameters:
logWriter - The writer to print the message to.
time - The timestamp of the message.
dateFormat - The date format to use.
type - The type of message (log level).
messageStr - The content of the message.
maxLength - The maximum length the message can be.
Returns:
False if the message was truncated, true otherwise.

toLogString

public static String toLogString(LogEvent event)
Generate a string from a log event, minus the application and date info.


printException

public static void printException(PrintWriter str,
                                  Throwable exception)

printExceptionMessage

public static boolean printExceptionMessage(PrintWriter str,
                                            Throwable exception,
                                            int maxLength)

printExceptionTrace

public static void printExceptionTrace(PrintWriter str,
                                       Throwable exception,
                                       int maxLength)
Prints the exception stack trace. Note the stack trace is not truncated to preserve diagnostic information.

Parameters:
str - The output character stream.
exception -
maxLength - Max length of nested exception message, if any.

trace

public static void trace(String messageStr,
                         Object source)
A trace is used to show the current state of the program for debugging purposes.


trace

public static void trace(String messageStr,
                         Object source,
                         Throwable ex)
A trace is used to show the current state of the program for debugging purposes.


warn

public static void warn(String messageStr,
                        Object source)
A warning is an alert that something anomalous has happened that *may* indicate something has gone wrong.


warn

public static void warn(String messageStr,
                        Object source,
                        Throwable ex)
A warning is an alert that something anomalous has happened that *may* indicate something has gone wrong.


alarm

public static void alarm(String messageStr,
                         Object source)
An alarm is for serious errors that indicate the program has failed in some non-recoverable way.


alarm

public static void alarm(String messageStr,
                         Object source,
                         Throwable ex)
An alarm is for serious errors that indicate the program has failed in some non-recoverable way.


info

public static void info(String messageStr,
                        Object source)
An info message is for the information that may be of interest to the user, rather than a trace for the developer.


internalError

public static void internalError(String messageStr,
                                 Object source)
Indicates some extremely serious error has occurred, usually requiring immediate shutdown of the system.


internalError

public static void internalError(String messageStr,
                                 Object source,
                                 Throwable ex)
Indicates some extremely serious error has occurred, usually requiring immediate shutdown of the system.


diagnostic

public static void diagnostic(String messageStr,
                              Object source)
A diagnostic is something that is likely to be useful for diagnosing problems, and thus is more interesting than a trace, but si not an actual warning of anything going wrong. An example would be a messsage like "About to expire old messages", "Completed system initialization", etc.


diagnostic

public static void diagnostic(String messageStr,
                              Object source,
                              Throwable ex)
A diagnostic is something that is likely to be useful for diagnosing problems, and thus is more interesting than a trace, but si not an actual warning of anything going wrong. An example would be a messsage like "About to expire old messages", "Completed system initialization", etc.


removeLogListener

public static void removeLogListener(LogListener l)

addLogListener

public static void addLogListener(LogListener l)

fireMessageReceived

protected static void fireMessageReceived(LogEvent e)


Copyright © 2008 Commonwealth of Australia