Class TetradLogger

java.lang.Object
edu.cmu.tetrad.util.TetradLogger

public class TetradLogger extends Object
Represents a logging utility used throughout tetrad. Unlike a typical logger, this does not work on levels; instead one can set events need to be logged. This is done by giving the logger a TetradLoggerConfig which will be used to determine whether some event should be logged.

Although one can explicitly construct instance of TetradLoggerConfig and set them in the logger, the configuration detail for most models is defined in the configuration.xml file and added to the logger at startup. A pre-configured TetradLoggerConfig for some model can be found by calling getTetradLoggerConfigForModel(Class)

Furthermore, the logger supports logging to a sequence of files in some directory. To start logging to a new file in the logging directory (assuming it has been set) call setNextOutputStream to remove this stream from the logger call removeNextOutputStream. In adding to the feature arbitrary streams can be added and removed from the logger by calling addOutputStream and removeOutputStream.

Version:
$Id: $Id
Author:
Tyler Gibson
  • Method Details

    • getInstance

      public static TetradLogger getInstance()
      Returns an instance of TetradLogger.
      Returns:
      an instance of TetradLogger
    • addTetradLoggerListener

      public void addTetradLoggerListener(TetradLoggerListener l)
      Adds a TetradLoggerListener to the TetradLogger. The listener will be notified whenever a logger configuration is set or reset.
      Parameters:
      l - the TetradLoggerListener to add
    • removeTetradLoggerListener

      public void removeTetradLoggerListener(TetradLoggerListener l)
      Removes a TetradLoggerListener from the TetradLogger.
      Parameters:
      l - the TetradLoggerListener to remove
    • setTetradLoggerConfig

      public void setTetradLoggerConfig(TetradLoggerConfig config)
      Sets what configuration should be used to determine which events to log. Null can be given to remove a previously set configuration from the logger.
      Parameters:
      config - a TetradLoggerConfig object
    • setConfigForClass

      public void setConfigForClass(Class<?> model)
      If there is a pre-defined configuration for the given model it is set, otherwise an exception is thrown.
      Parameters:
      model - a Class object
    • addTetradLoggerConfig

      public void addTetradLoggerConfig(Class<?> model, TetradLoggerConfig config)
      Adds the given TetradLoggerConfig to the logger, so that it can be used throughout the life of the application.
      Parameters:
      model - a Class object
      config - a TetradLoggerConfig object
    • getLoggerForClass

      public TetradLoggerConfig getLoggerForClass(Class<?> clazz)

      getLoggerForClass.

      Parameters:
      clazz - a Class object
      Returns:
      a TetradLoggerConfig object
    • reset

      public void reset()
      Resets the logger by removing any configuration info set with setTetradLoggerConfig.
    • isLogging

      public boolean isLogging()
      States whether the logger is turned on or not.
      Returns:
      true iff the logger is logging.
    • setLogging

      public void setLogging(boolean logging)
      Sets whether the logger is on or not.
      Parameters:
      logging - a boolean
    • flush

      public void flush()
      Flushes the writers.
    • error

      public void error(String message)
      Logs an error, this will log the message regardless of any configuration information. Although it won't be logged if the logger is off and if there are no streams attached.
      Parameters:
      message - a String object
    • forceLogMessage

      public void forceLogMessage(String message)
      Logs the given message regardless of the logger's getModel settings. Although nothing will be logged if the logger has been turned off.
      Parameters:
      message - a String object
    • addOutputStream

      public void addOutputStream(OutputStream stream)
      Sets the OutputStream that is used to log matters out to.
      Parameters:
      stream - a OutputStream object
    • removeOutputStream

      public void removeOutputStream(OutputStream stream)
      Removes the given stream from the logger.
      Parameters:
      stream - a OutputStream object
    • clear

      public void clear()
      Removes all streams from the logger.
    • setNextOutputStream

      public void setNextOutputStream()
      Sets the next output stream to use it for logging, call removeNextOutputStream to remove it. This will create the next output file in the output directory and form a stream from it and add it to the logger.
      Throws:
      IllegalStateException - - Thrown if there is an error setting the stream, the message will state the nature of the error.
    • removeNextOutputStream

      public void removeNextOutputStream()

      removeNextOutputStream.

    • getLoggingFilePrefix

      public String getLoggingFilePrefix()

      getLoggingFilePrefix.

      Returns:
      - prefix
    • setLoggingFilePrefix

      public void setLoggingFilePrefix(String loggingFilePrefix)
      Sets the logging prefix.
      Parameters:
      loggingFilePrefix - a String object
    • isDisplayLogEnabled

      public boolean isDisplayLogEnabled()
      States whether to display the log display.
      Returns:
      a boolean
    • setDisplayLogEnabled

      public void setDisplayLogEnabled(boolean enabled)
      Sets whether the display log should be used or not.
      Parameters:
      enabled - a boolean
    • isFileLoggingEnabled

      public boolean isFileLoggingEnabled()
      States whether file logging is enabled or not.
      Returns:
      a boolean
    • setFileLoggingEnabled

      public void setFileLoggingEnabled(boolean enabled)
      Sets whether "file logging" is enabled or not; that is whether calls to setNextOutputStream will be respected.
      Parameters:
      enabled - a boolean
    • setAutomaticLogDisplayEnabled

      public void setAutomaticLogDisplayEnabled(boolean enable)
      States whether log displays should be automatically displayed or not.
      Parameters:
      enable - a boolean
    • getLoggingDirectory

      public String getLoggingDirectory()

      getLoggingDirectory.

      Returns:
      - logging directory.
    • setLoggingDirectory

      public void setLoggingDirectory(String directory)
      Sets the logging directory, but first checks whether we can write to it, etc.
      Parameters:
      directory - - The directory to set.
      Throws:
      IllegalStateException - if there is a problem with the directory.
    • getLatestFilePath

      public String getLatestFilePath()

      Getter for the field latestFilePath.

      Returns:
      a String object