Home

Awesome

Logging for Matlab

This simple logging module is a modification of log4m with the following improvements:

Each logger's output can be directed to the standard output and/or to a file.

Each logger is assigned a logging level that will control the amount of output. The possible levels are, from high to low:

The default level in INFO. If a logger outputs at a level lower than or equal to its assigned level, the output will be logged. To silence a logger, set its level to OFF.

All loggers output a string according to the Matlab format '%-s %-23s %-8s %s\n'. Note that a newline is always appended, so there is no need to terminate log lines with a newline. The format is as follows:

API

An instance of the logging class is created using the logging.getLogger function. The first argument for this function must be the name of the logger. Four additional optional arguments are also available. These can either be provided as name/value pairs (such as logger.getlogger(name, 'path', path)) or as a struct where the field names are the names of the argument (such as logger.getlogger(name, struct('path', path)). The available arguments are:

If logger is an instance of the logging class, the following methods can be used to log output at different levels:

The following utility methods are also available:

The following properties can be read or written:

The following properties are read-only (note that these are called in a different way):

Examples

A logger at default level INFO logs messages at levels INFO, WARNING, ERROR and CRITICAL, but not at levels TRACE or DEBUG:

>> addpath('/path/to/logging4matlab')
>> logger = logging.getLogger('mylogger')  % new logger with default level INFO
>> logger.info('life is just peachy')
logging.info 2016-09-14 15:10:06,049 INFO     life is just peachy
>> logger.debug('Easy as pi! (Euclid)')    % produces no output
>> logger.critical('run away!')
logging.critical 2016-09-14 15:12:37,652 CRITICAL run away!

Use formatting for logged messages similar to sprintf or fprintf

>> logger.critical('Item %d (%s) not found', 217, 'foo');
logging.critical 2016-09-14 15:12:37,652 CRITICAL Item 217 (foo) not found

A logger's assigned level for the command window (or terminal) can be changed:

>> logger.setCommandWindowLevel(logging.logging.WARNING)

A logger can also output to file:

>> logger2 = logging.getLogger('myotherlogger', 'path', '/tmp/logger2.log')
>> logger.setLogLevel(logging.logging.WARNING)

Output to either the command window or a file can be suppressed with logging.logging.OFF.

FAQ

  1. Why is there no colored logging in the Matlab command window? I haven't gotten around to evaluating the performance of cprintf, which seems to be the only viable option for colored output in the command window. Pull request welcome!
  2. Can I change the colors? Currently, no, but feel free to submit a pull request!
  3. Can I change the format string used by loggers? Currently, no, but feel free to submit a pull request!

Tests

Invoke runtests('test') in a MATLAB prompt to run the unit tests.