com.tenduke.diagnostics
Class AbstractLogWriter

java.lang.Object
  extended by com.tenduke.diagnostics.AbstractLogWriter
All Implemented Interfaces:
LogEntrySubscriber
Direct Known Subclasses:
ConsoleLogWriter, LogFileWriter

public abstract class AbstractLogWriter
extends java.lang.Object
implements LogEntrySubscriber

Abstract base class for log writers that listen to log entries published by a Logger and write entries to a log (log file, stdout / stderr etc.)

AbstractLogWriter uses getDebugLevel(boolean) with true input parameter as the highest debug level of a received log entry LogEntry.getDebugLevel() that is written to log. LogEntries with higher debug level are discarded.

AbstractLogWriter uses formatting templates to write each LogEntry to log. Templates can be specified for each LogEntry.getEventType(). MessageFormat is used to write log entries using this templates. Supported format specifiers are:

  1. {0}: Timestamp of the log entry (printed in ISO 8601 format)
  2. {1}: Event type of the log entry as string ("Error" / "Warning" / "Info")
  3. {2}: Log entry debug level (number 1 - 5)
  4. {3}: Log entry message
  5. {4}: Message of exception attached to the log entry
  6. {5}: Stack trace of exception attached to the log entry

Table of configuration keys used by this class:

Key nameDescriptionDefault value
logging.logwriter.debug.level Highest debug level value of received log entry that is written to log. Example: Debug level is set to 3, which means that any LogEntry with debug level set to 3 or less will be printed and LogEntry objects with debug level 4 and 5 will be omitted. N/A
logging.logwriter.template.error Formatting template for log entries with event type com.tenduke.diagnostics.LogEntry.EventType.Error. N/A
logging.logwriter.template.warning Formatting template for log entries with event type com.tenduke.diagnostics.LogEntry.EventType.Warning. N/A
logging.logwriter.template.info Formatting template for log entries with event type com.tenduke.diagnostics.LogEntry.EventType.Info. N/A

Table of JVM system properties defined by this class.

System propertyDescriptionDefault value
logging.logwriter.debug.level Highest debug level value of received log entry that is written to log. Example: Debug level is set to 3, which means that any LogEntry with debug level set to 3 or less will be printed and LogEntry objects with debug level 4 and 5 will be omitted. 5
logging.logwriter.template.error Formatting template for log entries with event type com.tenduke.diagnostics.LogEntry.EventType.Error. N/A
logging.logwriter.template.warning Formatting template for log entries with event type com.tenduke.diagnostics.LogEntry.EventType.Warning. N/A
logging.logwriter.template.info Formatting template for log entries with event type com.tenduke.diagnostics.LogEntry.EventType.Info. N/A


Field Summary
static int DEFAULT_DEBUG_LEVEL
          Default debug level (5), if not defined in the main configuration file with a key 'logging.logwriter.debug.level'.
static java.lang.String DEFAULT_TEMPLATE_ERROR
          Default print template "{1}: {3}\n{5}" for LogEntry.EventType.Error, if not defined in the main configuration file with a key 'logging.logwriter.template.error'.
static java.lang.String DEFAULT_TEMPLATE_INFO
          Default print template "{1}: {3}" for LogEntry.EventType.Info, if not defined in the main configuration file with a key 'logging.logwriter.template.info'.
static java.lang.String DEFAULT_TEMPLATE_WARNING
          Default print template "{1}: {3}\n{4}" for LogEntry.EventType.Warning, if not defined in the main configuration file with a key 'logging.logwriter.template.warning'.
 
Constructor Summary
protected AbstractLogWriter()
          Initializes new instance of the AbstractLogWriter class.
 
Method Summary
 java.lang.Integer getDebugLevel(boolean allowDefault)
          Gets highest debug level of a received log entry LogEntry.getDebugLevel() that is written to log.
 java.lang.String getLogEntryTemplateError(boolean allowDefault)
          Gets output formatting template for error log entries.
 java.lang.String getLogEntryTemplateInfo(boolean allowDefault)
          Gets output formatting template for info log entries.
 java.lang.String getLogEntryTemplateWarning(boolean allowDefault)
          Gets output formatting template for warning log entries.
 void logEntryReceived(LogEntry entry)
          Method that is called for each LogEntrySubscriber by Logger when a new log entry is received.
protected abstract  void printError(LogEntry entry, java.lang.Throwable throwable)
          Prints error in the case that printing log entry failed.
protected  void printLogEntry(LogEntry logEntry)
          Prints log entry to log.
protected abstract  void printLogEntry(LogEntry logEntry, java.lang.String formatTemplate)
          Prints log entry to log using the given format template.
 void setDebugLevel(java.lang.Integer debugLevel)
          Sets highest debug level of a received log entry LogEntry.getDebugLevel() that is written to log.
 void setLogEntryTemplateError(java.lang.String logEntryTemplateError)
          Sets output formatting template for error log entries.
 void setLogEntryTemplateInfo(java.lang.String logEntryTemplateInfo)
          Sets output formatting template for info log entries.
 void setLogEntryTemplateWarning(java.lang.String logEntryTemplateWarning)
          Sets output formatting template for warning log entries.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_DEBUG_LEVEL

public static final int DEFAULT_DEBUG_LEVEL
Default debug level (5), if not defined in the main configuration file with a key 'logging.logwriter.debug.level'.

See Also:
Constant Field Values

DEFAULT_TEMPLATE_ERROR

public static final java.lang.String DEFAULT_TEMPLATE_ERROR
Default print template "{1}: {3}\n{5}" for LogEntry.EventType.Error, if not defined in the main configuration file with a key 'logging.logwriter.template.error'.

See Also:
Constant Field Values

DEFAULT_TEMPLATE_INFO

public static final java.lang.String DEFAULT_TEMPLATE_INFO
Default print template "{1}: {3}" for LogEntry.EventType.Info, if not defined in the main configuration file with a key 'logging.logwriter.template.info'.

See Also:
Constant Field Values

DEFAULT_TEMPLATE_WARNING

public static final java.lang.String DEFAULT_TEMPLATE_WARNING
Default print template "{1}: {3}\n{4}" for LogEntry.EventType.Warning, if not defined in the main configuration file with a key 'logging.logwriter.template.warning'.

See Also:
Constant Field Values
Constructor Detail

AbstractLogWriter

protected AbstractLogWriter()
Initializes new instance of the AbstractLogWriter class.

Method Detail

getDebugLevel

public java.lang.Integer getDebugLevel(boolean allowDefault)
Gets highest debug level of a received log entry LogEntry.getDebugLevel() that is written to log.

Parameters:
allowDefault - If true, default debug level is returned if no debug level has been explicitly set.
Returns:
Highest debug level of a received LogEntry that is written to log. Debug level value 5 means that all received log entries are written to log, 0 means that nothing is written.

getLogEntryTemplateError

public java.lang.String getLogEntryTemplateError(boolean allowDefault)
Gets output formatting template for error log entries.

Parameters:
allowDefault - If true, default template is returned if no template has been explicitly set.
Returns:
Output formatting template for error log entries.

getLogEntryTemplateInfo

public java.lang.String getLogEntryTemplateInfo(boolean allowDefault)
Gets output formatting template for info log entries.

Parameters:
allowDefault - If true, default template is returned if no template has been explicitly set.
Returns:
Output formatting template for info log entries.

getLogEntryTemplateWarning

public java.lang.String getLogEntryTemplateWarning(boolean allowDefault)
Gets output formatting template for warning log entries.

Parameters:
allowDefault - If true, default template is returned if no template has been explicitly set.
Returns:
Output formatting template for warning log entries.

logEntryReceived

public void logEntryReceived(LogEntry entry)
Method that is called for each LogEntrySubscriber by Logger when a new log entry is received.

Specified by:
logEntryReceived in interface LogEntrySubscriber
Parameters:
entry - the log entry

printError

protected abstract void printError(LogEntry entry,
                                   java.lang.Throwable throwable)
Prints error in the case that printing log entry failed.

Parameters:
entry - LogEntry for which writing failed.
throwable - The error that occurred when attempting to write.

printLogEntry

protected void printLogEntry(LogEntry logEntry)
                      throws java.io.IOException
Prints log entry to log. This method resolves format template to be used for printing the log entry and calls printLogEntry(com.tenduke.diagnostics.LogEntry, java.lang.String).

Parameters:
logEntry - LogEntry to write.
Throws:
java.io.IOException - Thrown if printing log entry to writer fails.

printLogEntry

protected abstract void printLogEntry(LogEntry logEntry,
                                      java.lang.String formatTemplate)
                               throws java.io.IOException
Prints log entry to log using the given format template.

Parameters:
logEntry - LogEntry to write.
formatTemplate - Format template to use.
Throws:
java.io.IOException - Thrown if printing log entry fails.

setDebugLevel

public void setDebugLevel(java.lang.Integer debugLevel)
Sets highest debug level of a received log entry LogEntry.getDebugLevel() that is written to log.

Parameters:
debugLevel - Highest debug level of a received LogEntry that is written to log. Debug level value 5 means that all received log entries are written to log, 0 means that nothing is written.

setLogEntryTemplateError

public void setLogEntryTemplateError(java.lang.String logEntryTemplateError)
Sets output formatting template for error log entries.

Parameters:
logEntryTemplateError - Output formatting template for error log entries.

setLogEntryTemplateInfo

public void setLogEntryTemplateInfo(java.lang.String logEntryTemplateInfo)
Sets output formatting template for info log entries.

Parameters:
logEntryTemplateInfo - Output formatting template for info log entries.

setLogEntryTemplateWarning

public void setLogEntryTemplateWarning(java.lang.String logEntryTemplateWarning)
Sets output formatting template for warning log entries.

Parameters:
logEntryTemplateWarning - Output formatting template for warning log entries.