Package edu.cmu.tetrad.util
Class TetradLogger
java.lang.Object
edu.cmu.tetrad.util.TetradLogger
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 explicity construct instance of TetradLoggerConfig
and set them in the logger, the
configuration details 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 addiong to the feature arbitrary streams can be add and removed
from the logger by calling addOutputStream
and
removeOutputStream
.
- Author:
- Tyler Gibson
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A empty config, where no event is active.static interface
Represents an output stream that can get its own length. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addOutputStream
(OutputStream stream) Sets theOutputStream
that is used to log matters out to.void
addTetradLoggerConfig
(Class model, TetradLoggerConfig config) Adds the givenTetradLoggerConfig
to the logger, so that it can be used throughout the life of the application.void
Adds the given listener to the logger.void
clear()
Removes all streams from the logger.void
Logs an error, this will log the message regardless of any configuration information.void
flush()
Flushes the writers.void
forceLogMessage
(String message) Logs the given message regardless of the logger's getModel settings.static TetradLogger
getLoggerForClass
(Class clazz) States whether the automatic log display is enabled or not, or returns null if there is no value stored in the user's prefs.boolean
States whether to display the log display.boolean
isEventActive
(String id) States whether the given event is active or not.boolean
States whether file logging is enabled or not.boolean
States whether the logger is turned on or not.void
Logs the given message with a newline associated with the given event.void
void
removeOutputStream
(OutputStream stream) Removes the given stream from the logger.void
Removes the given listener from the logger.void
reset()
Resets the logger by removing any configuration info set withsetTetradLoggerConfig
.void
setAutomaticLogDisplayEnabled
(boolean enable) States whether log displays should be automatically displayed or not.void
setConfigForClass
(Class model) If there is a pre-defined configuration for the given model it is set, otherwise an exception is thrown.void
setDisplayLogEnabled
(boolean enabled) Sets whether the display log should be used or not.void
setEventsToLog
(String... events) This can be used to tell the logger which events to log without having to first define aTetradLoggerConfig
.void
setFileLoggingEnabled
(boolean enabled) Sets whether "file logging" is enabled or not, that is whether calls tosetNextOutputStream
will be respected.void
setForceLog
(boolean force) Forces the logger to log all events, useful for testing.void
setLogging
(boolean logging) Sets whether the logger is on or not.void
setLoggingDirectory
(String directory) Sets the logging directory, but first checks whether we can write to it etc.void
setLoggingFilePrefix
(String loggingFilePrefix) Sets the logging prefix.void
Sets the next output stream to use for logging, callremoveNextOutputStream
to remove it.void
Sets what configuration should be used to determine which events to log.
-
Method Details
-
getInstance
- Returns:
- - instance
-
addTetradLoggerListener
Adds the given listener to the logger. -
removeTetradLoggerListener
Removes the given listener from the logger. -
setTetradLoggerConfig
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. -
setEventsToLog
This can be used to tell the logger which events to log without having to first define aTetradLoggerConfig
. -
setForceLog
public void setForceLog(boolean force) Forces the logger to log all events, useful for testing. -
setConfigForClass
If there is a pre-defined configuration for the given model it is set, otherwise an exception is thrown. -
addTetradLoggerConfig
Adds the givenTetradLoggerConfig
to the logger, so that it can be used throughout the life of the application. -
getLoggerForClass
-
reset
public void reset()Resets the logger by removing any configuration info set withsetTetradLoggerConfig
. -
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. -
flush
public void flush()Flushes the writers. -
log
Logs the given message with a newline associated with the given event.- Parameters:
event
- - The name of the log event.message
- - The messag eto be logged.
-
error
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 of course if there are no streams attached. -
forceLogMessage
Logs the given message regardless of the logger's getModel settings. Although nothing will be logged if the logger has been turned off. -
addOutputStream
Sets theOutputStream
that is used to log matters out to. -
removeOutputStream
Removes the given stream from the logger. -
clear
public void clear()Removes all streams from the logger. -
setNextOutputStream
public void setNextOutputStream()Sets the next output stream to use for logging, callremoveNextOutputStream
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() -
getLoggingFilePrefix
- Returns:
- - prefix
-
setLoggingFilePrefix
Sets the logging prefix. -
isDisplayLogEnabled
public boolean isDisplayLogEnabled()States whether to display the log display. -
setDisplayLogEnabled
public void setDisplayLogEnabled(boolean enabled) Sets whether the display log should be used or not. -
isFileLoggingEnabled
public boolean isFileLoggingEnabled()States whether file logging is enabled or not. -
setFileLoggingEnabled
public void setFileLoggingEnabled(boolean enabled) Sets whether "file logging" is enabled or not, that is whether calls tosetNextOutputStream
will be respected. -
isAutomaticLogDisplayEnabled
States whether the automatic log display is enabled or not, or returns null if there is no value stored in the user's prefs. -
setAutomaticLogDisplayEnabled
public void setAutomaticLogDisplayEnabled(boolean enable) States whether log displays should be automatically displayed or not. -
getLoggingDirectory
- Returns:
- - logging directory.
-
setLoggingDirectory
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.
-
isEventActive
States whether the given event is active or not. -
getLatestFilePath
-
getLoggerConfig
-