Class 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 INFO with tag "STDOUT". Lines on standard error will be logged at level ERROR with tag "STDERR". This feature is by default disabled, but it can be enabled with setCaptureStdOut() and setCaptureStdErr().
    • Log files. All log messages can be written to log files using a FileLogger. This feature can be enabled with setFileLogger().

    The first time an instance of this class is constructed, it will store the current values of System.out and System.err. You should ensure that they have the default system values. The values are changed when you call setCaptureStdOut(true) or setCaptureStdErr(true). The default system values are restored when you call setCaptureStdOut(false) and setCaptureStdErr(false).

    Subclasses can get access to the default system values with the methods getDefaultStdOut() and getDefaultStdErr(). 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 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 that System.out and System.err have 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 invalid
        IOException - 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. with setLogLevel(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 tag
        level - 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 of System.out. Subclasses must use this value if they want to print to standard output, because the current value of System.out may be a stream that is captured by the logger.
        Returns:
        the default standard output
      • getDefaultStdErr

        protected PrintStream getDefaultStdErr()
        Returns the default system value of System.err. Subclasses must use this value if they want to print to standard error, because the current value of System.err may 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: LogDelegate
        Writes a log message.
        Specified by:
        println in interface LogDelegate
        Parameters:
        priority - the log level
        tag - the tag
        msg - 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 level
        tag - the message tag
        msg - the tagged message
        Returns:
        0 if no error occurred, an error code otherwise.
      • getStackTraceString

        public String getStackTraceString​(Throwable tr)
        Description copied from interface: LogDelegate
        Returns 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:
        getStackTraceString in interface LogDelegate
        Parameters:
        tr - the exception
        Returns:
        the stack trace string
      • isLoggable

        public boolean isLoggable​(String tag,
                                  int level)
        Description copied from interface: LogDelegate
        Determines if messages at the specified level are logged for the specified tag.
        Specified by:
        isLoggable in interface LogDelegate
        Parameters:
        tag - the tag
        level - 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: LogDelegate
        Writes a message at level DEBUG.
        Specified by:
        d in interface LogDelegate
        Parameters:
        tag - the tag
        msg - the message
        tr - an exception
        Returns:
        0 if no error occurred, an error code otherwise
      • d

        public int d​(String tag,
                     String msg)
        Description copied from interface: LogDelegate
        Writes a message at level DEBUG.
        Specified by:
        d in interface LogDelegate
        Parameters:
        tag - the tag
        msg - the message
        Returns:
        0 if no error occurred, an error code otherwise
      • e

        public int e​(String tag,
                     String msg)
        Description copied from interface: LogDelegate
        Writes a message at level ERROR.
        Specified by:
        e in interface LogDelegate
        Parameters:
        tag - the tag
        msg - 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: LogDelegate
        Writes a message at level ERROR.
        Specified by:
        e in interface LogDelegate
        Parameters:
        tag - the tag
        msg - the message
        tr - 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: LogDelegate
        Writes a message at level INFO.
        Specified by:
        i in interface LogDelegate
        Parameters:
        tag - the tag
        msg - the message
        tr - an exception
        Returns:
        0 if no error occurred, an error code otherwise
      • i

        public int i​(String tag,
                     String msg)
        Description copied from interface: LogDelegate
        Writes a message at level INFO.
        Specified by:
        i in interface LogDelegate
        Parameters:
        tag - the tag
        msg - 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: LogDelegate
        Writes a message at level VERBOSE.
        Specified by:
        v in interface LogDelegate
        Parameters:
        tag - the tag
        msg - the message
        tr - 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: LogDelegate
        Writes a message at level VERBOSE.
        Specified by:
        v in interface LogDelegate
        Parameters:
        tag - the tag
        msg - the message
        Returns:
        0 if no error occurred, an error code otherwise
      • w

        public int w​(String tag,
                     String msg)
        Description copied from interface: LogDelegate
        Writes a message at level WARN.
        Specified by:
        w in interface LogDelegate
        Parameters:
        tag - the tag
        msg - 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: LogDelegate
        Writes a message at level WARN.
        Specified by:
        w in interface LogDelegate
        Parameters:
        tag - the tag
        tr - 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: LogDelegate
        Writes a message at level WARN.
        Specified by:
        w in interface LogDelegate
        Parameters:
        tag - the tag
        msg - the message
        tr - 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: LogDelegate
        Writes a message at level ASSERT.
        Specified by:
        wtf in interface LogDelegate
        Parameters:
        tag - the tag
        msg - the message
        Returns:
        0 if no error occurred, an error code otherwise
      • wtf

        public int wtf​(String tag,
                       Throwable tr)
        Description copied from interface: LogDelegate
        Writes a message at level ASSERT.
        Specified by:
        wtf in interface LogDelegate
        Parameters:
        tag - the tag
        tr - 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: LogDelegate
        Writes a message at level ASSERT.
        Specified by:
        wtf in interface LogDelegate
        Parameters:
        tag - the tag
        msg - the message
        tr - an exception or null
        Returns:
        0 if no error occurred, an error code otherwise