snaq.util.logging
Class LogUtil

java.lang.Object
  extended by snaq.util.logging.LogUtil

public class LogUtil
extends Object

Class providing simple logging and debug functionality, which can be easily instantiated and used as a logging object. This class is not related to other external logging libraries, and is used to provide a standalone solution for writing basic log files.

Author:
Giles Winstanley

Constructor Summary
LogUtil()
          Creates a new Logger with logging disabled.
LogUtil(File file)
          Creates a new Logger which writes to the specified file.
 
Method Summary
 void close()
          Closes the log.
 void debug(String logEntry)
          If debug is enabled, writes a message to the log.
 void debug(String prefix, String logEntry)
          If debug is enabled, writes a message to the log with an optional prefix.
 void debug(String logEntry, Throwable throwable)
          If debug is enabled, writes a message with a Throwable to the log file.
 PrintWriter getLogWriter()
          Returns the current PrintWriter used to write to the log.
 boolean isDebug()
          Returns whether debug logging is enabled.
 boolean isLogging()
          Returns whether calls to the logging methods actually write to the log.
 void log(String logEntry)
          Writes a message to the log.
 void log(String prefix, String logEntry)
          Writes a message to the log with an optional prefix.
 void log(String prefix, String logEntry, Throwable throwable)
          Writes a message with a Throwable to the log file.
 void log(String logEntry, Throwable throwable)
          Writes a message with a Throwable to the log file.
 void log(Throwable throwable)
          Writes a Throwable to the log file.
 void setDateFormat(DateFormat df)
          Sets the date formatter for the logging.
 void setDebug(boolean b)
          Determines whether to perform debug logging.
 void setLog(PrintWriter writer)
          Sets the log stream and enables logging.
 void setLog(PrintWriter writer, boolean closeOnExit)
          Sets the log stream and enables logging.
 void setLogging(boolean b)
          Determines whether calls to the logging methods actually write to the log.
 void setSeparator(String sep)
          Sets the separator string between the date and log message (default ": ").
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LogUtil

public LogUtil()
Creates a new Logger with logging disabled.


LogUtil

public LogUtil(File file)
        throws IOException
Creates a new Logger which writes to the specified file.

Parameters:
file - file to which to write log entries
Throws:
FileNotFoundException - if specified file is a directory, or cannot be opened for some reason.
IOException
Method Detail

setDateFormat

public void setDateFormat(DateFormat df)
Sets the date formatter for the logging. The default format is obtained using the method call: DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG)

Parameters:
df - DateFormat instance to use for formatting log messages
See Also:
DateFormat

setSeparator

public void setSeparator(String sep)
Sets the separator string between the date and log message (default ": "). To set the default separator, call with a null argument.

Parameters:
sep - string to use as separator

setLog

public void setLog(PrintWriter writer)
Sets the log stream and enables logging. By default the PrintWriter is closed when the close() method is called.

Parameters:
writer - PrintWriter to which to write log entries

setLog

public void setLog(PrintWriter writer,
                   boolean closeOnExit)
Sets the log stream and enables logging.

Parameters:
writer - PrintWriter to which to write log entries
closeOnExit - whether to close the PrintWriter when close() is called

getLogWriter

public PrintWriter getLogWriter()
Returns the current PrintWriter used to write to the log.


debug

public void debug(String logEntry)
If debug is enabled, writes a message to the log.

Parameters:
logEntry - message to write as a log entry

debug

public void debug(String prefix,
                  String logEntry)
If debug is enabled, writes a message to the log with an optional prefix.

Parameters:
prefix - prefix string for the log entry
logEntry - message to write as a log entry

debug

public void debug(String logEntry,
                  Throwable throwable)
If debug is enabled, writes a message with a Throwable to the log file.

Parameters:
logEntry - message to write as a log entry
throwable - Throwable instance to log with this entry

log

public void log(String logEntry)
Writes a message to the log.

Parameters:
logEntry - message to write as a log entry

log

public void log(String prefix,
                String logEntry)
Writes a message to the log with an optional prefix.

Parameters:
prefix - prefix string for the log entry
logEntry - message to write as a log entry

log

public void log(String prefix,
                String logEntry,
                Throwable throwable)
Writes a message with a Throwable to the log file.

Parameters:
prefix - prefix string for the log entry
logEntry - message to write as a log entry
throwable - Throwable instance to log with this entry

log

public void log(String logEntry,
                Throwable throwable)
Writes a message with a Throwable to the log file.

Parameters:
throwable - Throwable instance to log with this entry
logEntry - message to write as a log entry

log

public void log(Throwable throwable)
Writes a Throwable to the log file.

Parameters:
throwable - Throwable instance to log with this entry

close

public void close()
Closes the log.


setLogging

public void setLogging(boolean b)
Determines whether calls to the logging methods actually write to the log.

Parameters:
b - flag indicating whether to write to the log

isLogging

public boolean isLogging()
Returns whether calls to the logging methods actually write to the log.

Returns:
true if logging is enabled, false otherwise.

setDebug

public void setDebug(boolean b)
Determines whether to perform debug logging.

Parameters:
b - flag indicating whether to perform debug logging

isDebug

public boolean isDebug()
Returns whether debug logging is enabled.

Returns:
true if debug logging is enabled, false otherwise.