ch.javasoft.util.logging
Class LogWriter

java.lang.Object
  extended by java.io.Writer
      extended by ch.javasoft.util.logging.LogWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable

public class LogWriter
extends Writer

The LogWriter is a special writer to support the log formatting functionality provided by this package.

If a program wants to write some output, it might be desired to write the output to the log file, formatted just like standard log messages. Since the program possibly writes multiple lines, this log writer uses a cache and flushes complete lines to the log file, using the formatter configured with the used logger.

A sample usage of the log writer involves logging of exception traces. The following code traces an exception to the log file, sample output is given below. Note that a LogPrintWriter is used here, which is based on the LogWriter:

        ...
        catch (Exception e) {
                Logger logger = Logger.getLogger("mylogger");
                e.printStackTrace(new LogPrintWriter(logger, Level.WARNING));
        }
 
Sample output looks like this
2008-09-13  10:47:45.990  main    mylogger         WARNING  | java.lang.ArithmeticException: / by zero
2008-09-13  10:47:45.992  main    mylogger         WARNING  |   at ch.javasoft.util.logging.LogWriter.divideByZero(LogWriter.java:37)
2008-09-13  10:47:45.992  main    mylogger         WARNING  |   at ch.javasoft.util.logging.LogWriter.main(LogWriter.java:42)
 


Field Summary
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
LogWriter(Logger logger, Level level)
          Constructor with logger and level on which the messages are logged
LogWriter(String loggerName, Level level)
          Constructor with logger name and level on which the messages are logged
 
Method Summary
 void close()
          Calls flush()
 void flush()
          Flushes the current content in the buffer to the log file, and clears the line buffer.
protected  String newLine()
          The newline which is used for, default is the platform specific newline encoding as defined by the line.separator system property.
 void write(char[] cbuf, int off, int len)
          Caches the characters, until a newline is detected.
 
Methods inherited from class java.io.Writer
append, append, append, write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LogWriter

public LogWriter(String loggerName,
                 Level level)
Constructor with logger name and level on which the messages are logged

Parameters:
loggerName - the logger's name
level - the log level to use for tracing

LogWriter

public LogWriter(Logger logger,
                 Level level)
Constructor with logger and level on which the messages are logged

Parameters:
logger - the logger
level - the log level to use for tracing
Method Detail

write

public void write(char[] cbuf,
                  int off,
                  int len)
Caches the characters, until a newline is detected. If a newline encoding is found, the buffered line is flushed to the logger, the remaining characters are kept in the buffer.

Specified by:
write in class Writer

newLine

protected String newLine()
The newline which is used for, default is the platform specific newline encoding as defined by the line.separator system property. However, subclasses might want to override this method, for instance to return an operating system independent newline encoding.


flush

public void flush()
Flushes the current content in the buffer to the log file, and clears the line buffer.

Specified by:
flush in interface Flushable
Specified by:
flush in class Writer

close

public void close()
Calls flush()

Specified by:
close in interface Closeable
Specified by:
close in class Writer