Logger

Package: kwave.toolbox Superclasses: handle

Manager for logging messages with different log levels.

Syntax

Logger.debug(message)
Logger.info(message)
Logger.warning(message)
Logger.error(errID, errorMessage)

Description

The Logger class is designed to manage messages from k-Wave classes and functions. Four levels of verbosity are supported as defined by the kwave.toolbox.LogLevels class: Debug, Info, Warning, and Error.

The setLogLevel function can be used to specify the minimum log level displayed. For example, calling kwave.toolbox.Logger.setLogLevel(kwave.toolbox.LogLevels.Warning) will suppress Debug and Info messages, and only print Warning and Error messages.

Messages can be printed to an external file instead of the command line by calling kwave.toolbox.Logger.setLogToFile("logFile.txt"). The time stamp can also be printed alongside the message by calling kwave.toolbox.Logger.showTimeStamp(true).

The class implementation uses a singleton design so that the same log settings can be used across the k-Wave toolbox. The class methods are static, enabling direct access without a manual instantiation.

Examples

% Load toolbox and clear previous logger settings.
import kwave.toolbox.*
Logger.reset();
% Display debug and info messages. By default, only the info message
% appears as the log level is |LogLevels.Info|.
Logger.debug('This debug message will not be printed.');
Logger.info('This info message will be printed.');
% Adjust the log level and display another debug message. The debug
% message now shows up.
Logger.setLogLevel(LogLevels.Debug);
Logger.debug('Another debugging message');
% Alter the log output to a file, turn on display of the time stamp,
% then log a message.
Logger.setLogToFile('logfile.txt');
Logger.setShowTimeStamp(true);
Logger.info('This message will be printed to the log file.');
% Reset the log output to the command line, then print warning and
% error messages.
Logger.setLogToCommandLine();
Logger.warning('Warning message');
try
    Logger.error('ClassName:CustomErrorID', 'Error message');
catch ME
    disp(ME.message);
end

Methods

Logging

  • debug - Logs a debug message.
  • info - Logs an informational message.
  • warning - Logs a warning message and initiates a MATLAB warning.
  • error - Logs an error message and triggers a MATLAB error.

General

  • reset - Resets all logger settings to their defaults.
  • setLogLevel - Sets the minimum log level. Messages beneath this level won't be logged. Accepts LogLevels.Debug, LogLevels.Info, LogLevels.Warning, and LogLevels.Error. Defaults to LogLevels.Info.
  • setLogToFile - Sets a text filename where log messages will be stored. If set to an empty string, messages are logged to the command window. Defaults to ''.
  • setLogToCommandLine - Clears the log filename so that log messages are displayed on the command line.
  • showTimeStamp - Specifies whether a date and time stamp is shown alongside the log message. Defaults to false.

See Also