Class AbstractLogDelegate
- java.lang.Object
-
- eu.woolplatform.utils.log.AbstractLogDelegate
-
- All Implemented Interfaces:
LogDelegate
- Direct Known Subclasses:
DefaultLogDelegate
public abstract class AbstractLogDelegate extends Object implements LogDelegate
This abstract log delegate provides the following features:
- Every line that is printed in a log message will get three tags: the log level, the message tag (identifies the source of the log message) and the current date and time.
- Capture standard output and standard error. Everything that is printed
to standard output and standard error can get the same treatment as other
log messages. Lines on standard output will be logged at level
INFOwith tag "STDOUT". Lines on standard error will be logged at levelERRORwith tag "STDERR". This feature is by default disabled, but it can be enabled withsetCaptureStdOut()andsetCaptureStdErr(). - Log files. All log messages can be written to log files using a
FileLogger. This feature can be enabled withsetFileLogger().
The first time an instance of this class is constructed, it will store the current values of
System.outandSystem.err. You should ensure that they have the default system values. The values are changed when you callsetCaptureStdOut(true)orsetCaptureStdErr(true). The default system values are restored when you callsetCaptureStdOut(false)andsetCaptureStdErr(false).Subclasses can get access to the default system values with the methods
getDefaultStdOut()andgetDefaultStdErr(). If a log message needs to be printed to standard output or standard error, these default system values MUST be used. Otherwise the log messages will be recursively captured.A subclass need only implement
printTaggedMessage().Every log method may return an error code. This is in fact an OR of error constants defined in this class or a subclass. This class defines the following constants:
-
-
Field Summary
Fields Modifier and Type Field Description protected static intERROR_BASESubclasses can define error codes starting at this constant.static intERROR_WRITE_FILEThis error code indicates that an error occurred while writing a log message to a log file.
-
Constructor Summary
Constructors Constructor Description AbstractLogDelegate()Constructs a new abstract log delegate.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description intd(String tag, String msg)Writes a message at levelDEBUG.intd(String tag, String msg, Throwable tr)Writes a message at levelDEBUG.inte(String tag, String msg)Writes a message at levelERROR.inte(String tag, String msg, Throwable tr)Writes a message at levelERROR.protected PrintStreamgetDefaultStdErr()Returns the default system value ofSystem.err.protected PrintStreamgetDefaultStdOut()Returns the default system value ofSystem.out.FileLoggergetFileLogger()Returns the file logger.StringgetStackTraceString(Throwable tr)Returns the stack trace string for the specified exception.inti(String tag, String msg)Writes a message at levelINFO.inti(String tag, String msg, Throwable tr)Writes a message at levelINFO.booleanisLoggable(String tag, int level)Determines if messages at the specified level are logged for the specified tag.intprintln(int priority, String tag, String msg)Writes a log message.abstract intprintTaggedMessage(int priority, String tag, String msg)Writes a log message.voidsetCaptureStdErr(boolean capture)Sets whether standard error should be captured.voidsetCaptureStdOut(boolean capture)Sets whether standard output should be captured.voidsetFileLogger(FileLogger fileLogger)Sets a file logger to which log messages should be written.voidsetLogLevel(int level)Sets the default log level.voidsetLogLevel(String tag, int level)Sets the log level for messages with the specified tag.voidsetLogLevels(File xmlFile)Sets the log levels as defined in an XML file.voidsetLogLevels(InputStream in)Sets the log levels as defined in an XML file.intv(String tag, String msg)Writes a message at levelVERBOSE.intv(String tag, String msg, Throwable tr)Writes a message at levelVERBOSE.intw(String tag, String msg)Writes a message at levelWARN.intw(String tag, String msg, Throwable tr)Writes a message at levelWARN.intw(String tag, Throwable tr)Writes a message at levelWARN.intwtf(String tag, String msg)Writes a message at levelASSERT.intwtf(String tag, String msg, Throwable tr)Writes a message at levelASSERT.intwtf(String tag, Throwable tr)Writes a message at levelASSERT.
-
-
-
Field Detail
-
ERROR_WRITE_FILE
public static int ERROR_WRITE_FILE
This error code indicates that an error occurred while writing a log message to a log file.
-
ERROR_BASE
protected static int ERROR_BASE
Subclasses can define error codes starting at this constant. An error code should have exactly one bit set, in order that an OR of error codes is possible.
-
-
Constructor Detail
-
AbstractLogDelegate
public AbstractLogDelegate()
Constructs a new abstract log delegate. The first time an abstract log delegate is constructed, you should ensure thatSystem.outandSystem.errhave the default system values.
-
-
Method Detail
-
setLogLevels
public void setLogLevels(File xmlFile) throws ParseException, IOException
Sets the log levels as defined in an XML file.- Parameters:
xmlFile- the XML file- Throws:
ParseException- if the content of the XML file is invalidIOException- if a reading error occurs
-
setLogLevels
public void setLogLevels(InputStream in) throws ParseException, IOException
Sets the log levels as defined in an XML file.- Parameters:
in- the input stream for the XML file- Throws:
ParseException- if the content of the XML file is invalidIOException- if a reading error occurs
-
setLogLevel
public void setLogLevel(int level)
Sets the default log level. This level is used for message tags for which no specific log level was set (e.g. withsetLogLevel(tag, level)). The logger will not write log messages at a lower level than the specified level.- Parameters:
level- the log level
-
setLogLevel
public void setLogLevel(String tag, int level)
Sets the log level for messages with the specified tag. The logger will not write log messages at a lower level than the specified level.- Parameters:
tag- the message taglevel- the log level
-
setCaptureStdOut
public void setCaptureStdOut(boolean capture)
Sets whether standard output should be captured.- Parameters:
capture- true if standard output should be captured, false otherwise
-
setCaptureStdErr
public void setCaptureStdErr(boolean capture)
Sets whether standard error should be captured.- Parameters:
capture- true if standard error should be captured, false otherwise
-
setFileLogger
public void setFileLogger(FileLogger fileLogger)
Sets a file logger to which log messages should be written. You may set this to null to disable logging to files (that is the default).- Parameters:
fileLogger- the file logger or null
-
getFileLogger
public FileLogger getFileLogger()
Returns the file logger. If not file logger was set, this method returns null.- Returns:
- the file logger or null
-
getDefaultStdOut
protected PrintStream getDefaultStdOut()
Returns the default system value ofSystem.out. Subclasses must use this value if they want to print to standard output, because the current value ofSystem.outmay be a stream that is captured by the logger.- Returns:
- the default standard output
-
getDefaultStdErr
protected PrintStream getDefaultStdErr()
Returns the default system value ofSystem.err. Subclasses must use this value if they want to print to standard error, because the current value ofSystem.errmay be a stream that is captured by the logger.- Returns:
- the default standard error
-
println
public int println(int priority, String tag, String msg)Description copied from interface:LogDelegateWrites a log message.- Specified by:
printlnin interfaceLogDelegate- Parameters:
priority- the log leveltag- the tagmsg- the message- Returns:
- 0 if no error occurred, an error code otherwise
-
printTaggedMessage
public abstract int printTaggedMessage(int priority, String tag, String msg)Writes a log message. Every line in the message text has already been tagged with the log level, message tag (identifier of the source of the log message) and the current date and time. Every line ends with a new line character, including the last line.This method is only called if
isLoggable()returns true. It's called inside a lock, so it's thread safe.For returned error codes, a subclass may define its own error codes based on
ERROR_BASE.- Parameters:
priority- the log leveltag- the message tagmsg- the tagged message- Returns:
- 0 if no error occurred, an error code otherwise.
-
getStackTraceString
public String getStackTraceString(Throwable tr)
Description copied from interface:LogDelegateReturns the stack trace string for the specified exception. The returned string probably contains new lines, but it does not have a trailing new line.- Specified by:
getStackTraceStringin interfaceLogDelegate- Parameters:
tr- the exception- Returns:
- the stack trace string
-
isLoggable
public boolean isLoggable(String tag, int level)
Description copied from interface:LogDelegateDetermines if messages at the specified level are logged for the specified tag.- Specified by:
isLoggablein interfaceLogDelegate- Parameters:
tag- the taglevel- the level- Returns:
- true if the message will be logged, false otherwise
-
d
public int d(String tag, String msg, Throwable tr)
Description copied from interface:LogDelegateWrites a message at levelDEBUG.- Specified by:
din interfaceLogDelegate- Parameters:
tag- the tagmsg- the messagetr- an exception- Returns:
- 0 if no error occurred, an error code otherwise
-
d
public int d(String tag, String msg)
Description copied from interface:LogDelegateWrites a message at levelDEBUG.- Specified by:
din interfaceLogDelegate- Parameters:
tag- the tagmsg- the message- Returns:
- 0 if no error occurred, an error code otherwise
-
e
public int e(String tag, String msg)
Description copied from interface:LogDelegateWrites a message at levelERROR.- Specified by:
ein interfaceLogDelegate- Parameters:
tag- the tagmsg- the message- Returns:
- 0 if no error occurred, an error code otherwise
-
e
public int e(String tag, String msg, Throwable tr)
Description copied from interface:LogDelegateWrites a message at levelERROR.- Specified by:
ein interfaceLogDelegate- Parameters:
tag- the tagmsg- the messagetr- an exception- Returns:
- 0 if no error occurred, an error code otherwise
-
i
public int i(String tag, String msg, Throwable tr)
Description copied from interface:LogDelegateWrites a message at levelINFO.- Specified by:
iin interfaceLogDelegate- Parameters:
tag- the tagmsg- the messagetr- an exception- Returns:
- 0 if no error occurred, an error code otherwise
-
i
public int i(String tag, String msg)
Description copied from interface:LogDelegateWrites a message at levelINFO.- Specified by:
iin interfaceLogDelegate- Parameters:
tag- the tagmsg- the message- Returns:
- 0 if no error occurred, an error code otherwise
-
v
public int v(String tag, String msg, Throwable tr)
Description copied from interface:LogDelegateWrites a message at levelVERBOSE.- Specified by:
vin interfaceLogDelegate- Parameters:
tag- the tagmsg- the messagetr- an exception or null- Returns:
- 0 if no error occurred, an error code otherwise
-
v
public int v(String tag, String msg)
Description copied from interface:LogDelegateWrites a message at levelVERBOSE.- Specified by:
vin interfaceLogDelegate- Parameters:
tag- the tagmsg- the message- Returns:
- 0 if no error occurred, an error code otherwise
-
w
public int w(String tag, String msg)
Description copied from interface:LogDelegateWrites a message at levelWARN.- Specified by:
win interfaceLogDelegate- Parameters:
tag- the tagmsg- the message- Returns:
- 0 if the message was written successfully, an error code otherwise
-
w
public int w(String tag, Throwable tr)
Description copied from interface:LogDelegateWrites a message at levelWARN.- Specified by:
win interfaceLogDelegate- Parameters:
tag- the tagtr- an exception or null- Returns:
- 0 if no error occurred, an error code otherwise
-
w
public int w(String tag, String msg, Throwable tr)
Description copied from interface:LogDelegateWrites a message at levelWARN.- Specified by:
win interfaceLogDelegate- Parameters:
tag- the tagmsg- the messagetr- an exception or null- Returns:
- 0 if no error occurred, an error code otherwise
-
wtf
public int wtf(String tag, String msg)
Description copied from interface:LogDelegateWrites a message at levelASSERT.- Specified by:
wtfin interfaceLogDelegate- Parameters:
tag- the tagmsg- the message- Returns:
- 0 if no error occurred, an error code otherwise
-
wtf
public int wtf(String tag, Throwable tr)
Description copied from interface:LogDelegateWrites a message at levelASSERT.- Specified by:
wtfin interfaceLogDelegate- Parameters:
tag- the tagtr- an exception or null- Returns:
- 0 if no error occurred, an error code otherwise
-
wtf
public int wtf(String tag, String msg, Throwable tr)
Description copied from interface:LogDelegateWrites a message at levelASSERT.- Specified by:
wtfin interfaceLogDelegate- Parameters:
tag- the tagmsg- the messagetr- an exception or null- Returns:
- 0 if no error occurred, an error code otherwise
-
-