tuwien.auto.calimero.log
Class LogStreamWriter

java.lang.Object
  extended by tuwien.auto.calimero.log.LogWriter
      extended by tuwien.auto.calimero.log.LogStreamWriter
Direct Known Subclasses:
LogFileWriter, LogNetWriter

public class LogStreamWriter
extends LogWriter

LogWriter using a Writer for output.

An existing output stream has to be supplied on creation of this LogWriter. All output will be checked against the internal log level, on logging permitted the output is formatted (optional) and handed to the underlying writer.
Output is not automatically flushed after each write by default.
Using autoFlush = true in the constructor ensures that no data buffering will delay the output. Note that this may degrade performance.
For occasional flushing use flush() manually.


Field Summary
protected static java.util.Calendar c
          Calendar used to generate date/time of logged output message.
protected  boolean formatOutput
          Set the formatting behavior of LogStreamWriter.
protected  java.lang.String lineSep
          Line separator, retrieved from the property "line.separator" and set in the constructor.
 
Constructor Summary
protected LogStreamWriter()
          Sets line separator; also called by subtypes creating the output stream on their own.
  LogStreamWriter(LogLevel level, java.io.OutputStream os)
          Creates a LogStreamWriter with specified log level and output stream.
  LogStreamWriter(LogLevel level, java.io.OutputStream os, boolean autoFlush)
          Creates a LogStreamWriter with specified log level and output stream.
  LogStreamWriter(java.io.OutputStream os)
          Creates a LogStreamWriter with specified output stream.
 
Method Summary
 void close()
          Closes the LogWriter and all its resources.
 void flush()
          Flushes all buffered output.
protected  java.lang.String formatOutput(java.lang.String svc, LogLevel l, java.lang.String msg, java.lang.Throwable t)
          Creates a formatted output string from the input parameters.
protected  boolean logAllowed(LogLevel level)
          Checks if logging output with log level is possible and would get accepted by this LogStreamWriter.
protected  void setOutput(java.io.Writer w)
          Sets the underlying writer to use for logging output.
 void write(java.lang.String logService, LogLevel level, java.lang.String msg)
          Writes a message out to this LogWriter.
 void write(java.lang.String logService, LogLevel level, java.lang.String msg, java.lang.Throwable t)
          Like LogWriter.write(String, LogLevel, String), in addition a Throwable object is taken which will be added to the message.
 
Methods inherited from class tuwien.auto.calimero.log.LogWriter
getErrorHandler, getLogLevel, setErrorHandler, setLogLevel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

c

protected static final java.util.Calendar c
Calendar used to generate date/time of logged output message.


formatOutput

protected boolean formatOutput
Set the formatting behavior of LogStreamWriter.

Determines if LogStreamWriter should call formatOutput(LogLevel, String, Throwable) before writing out the log message. Defaults to true, but might be set to false by subtypes if message given to write is already formatted.


lineSep

protected java.lang.String lineSep
Line separator, retrieved from the property "line.separator" and set in the constructor.

Constructor Detail

LogStreamWriter

protected LogStreamWriter()
Sets line separator; also called by subtypes creating the output stream on their own.


LogStreamWriter

public LogStreamWriter(LogLevel level,
                       java.io.OutputStream os)
Creates a LogStreamWriter with specified log level and output stream.

Parameters:
level - log level assigned with this LogStreamWriter
os - an OutputStream used by this LogStreamWriter
See Also:
LogStreamWriter(OutputStream)

LogStreamWriter

public LogStreamWriter(LogLevel level,
                       java.io.OutputStream os,
                       boolean autoFlush)
Creates a LogStreamWriter with specified log level and output stream. Parameter autoFlush sets flushing behavior on write() calls.

Parameters:
level - log level assigned with this LogStreamWriter
os - an OutputStream used by this LogStreamWriter
autoFlush - flush output after every successful call to write()
See Also:
LogStreamWriter(LogLevel, OutputStream)

LogStreamWriter

public LogStreamWriter(java.io.OutputStream os)
Creates a LogStreamWriter with specified output stream.

The output stream is wrapped by a BufferedWriter.

Parameters:
os - an OutputStream used by this LogStreamWriter
Method Detail

close

public void close()
Description copied from class: LogWriter
Closes the LogWriter and all its resources.

If necessary, all output is flushes before.

Specified by:
close in class LogWriter

flush

public void flush()
Description copied from class: LogWriter
Flushes all buffered output.

Specified by:
flush in class LogWriter

formatOutput

protected java.lang.String formatOutput(java.lang.String svc,
                                        LogLevel l,
                                        java.lang.String msg,
                                        java.lang.Throwable t)
Creates a formatted output string from the input parameters.

Override this method to provide a different output format.
The output returned by default follows the shown format. The date/time format is according ISO 8601 representation, extended format with decimal fraction of second (milliseconds):
"YYYY-MM-DD hh:mm:ss,sss level=level.toString(), logService: msg (t.getMessage())"
or, if throwable is null or throwable-message is null
"YYYY-MM-DD hh:mm:ss,sss logService, LogLevel=level.toString(): msg".
If logService contains '.' in the name, only the part after the last '.' will be used. This way names like "package.subpackage.name" are shortened to "name". Nevertheless, if the first character after '.' is numeric, no truncation will be done to allow e.g. IP addresses in the log service name.

Parameters:
svc - name of the log service the message comes from
l - log level of message and throwable
msg - message to format
t - an optional throwable object to format, might be null
Returns:
the formatted output

logAllowed

protected boolean logAllowed(LogLevel level)
Checks if logging output with log level is possible and would get accepted by this LogStreamWriter.

Therefore this method also checks that the underlying output stream is opened.

Parameters:
level - log level to check against
Returns:
true if log is permitted, false otherwise

setOutput

protected final void setOutput(java.io.Writer w)
Sets the underlying writer to use for logging output.

The log stream writer obtains ownership of the writer object.

Parameters:
w - the Writer

write

public void write(java.lang.String logService,
                  LogLevel level,
                  java.lang.String msg)
Description copied from class: LogWriter
Writes a message out to this LogWriter.

The message has the associated log level level. It will only be written if the LogWriter logging level is not more restrictive than level. Otherwise the message is ignored. LogWriter is responsible for formatting the output.

Specified by:
write in class LogWriter
Parameters:
logService - log service name stating the source of the message
level - log level of message
msg - the message to write

write

public void write(java.lang.String logService,
                  LogLevel level,
                  java.lang.String msg,
                  java.lang.Throwable t)
Description copied from class: LogWriter
Like LogWriter.write(String, LogLevel, String), in addition a Throwable object is taken which will be added to the message.

Specified by:
write in class LogWriter
Parameters:
logService - log service name stating the source of the message
level - log level of message
msg - the message to write
t - Throwable object, might be null