Matlab logging facility

Adds ability to log messages. Borrowed ideas from Java logging, and possibly Python logging.
765 Downloads
Updated 3 Jun 2013

View License

The Logger class is used for logging messages. The logger instances are named, and they are managed by LogManager.

The logger instance will compare the logging level of the message to its logging level. If the message's logging level meets or exceeds the logging level of the logger, the log message will be passed to its registered handlers. By default, the log message will also then be passed to its parent's handlers.

Handlers are what "emit" the log message to the outside world. Loggers decide if the log message should be seen by the Handlers.

The logging classes are in a Matlab package 'logging'. You will need to place the +logging directoy somewhere in your path. That is, the directory where the +logging directoy exists should be in your Matlab path -- not the +logging directory itself.

I would suggest doing a Google on java logging and reading a bit about Logger and LogManager. I think it may do a better job of trying to explain :).

Example code:

import logging.*

% --- BASIC CONSOLE ---

% get the global LogManager
logManager = LogManager.getLogManager();

% add a logger instance to this script
logger = Logger.getLogger('TestScript');
logger.setLevel( Level.ALL );

logger.info('Hi, this is info!');
logger.warning('Hi, this is warning!');

% side-effect is to close all handlers
logManager.resetAll();

% --- BASIC CONSOLE WITH FILE ---

% get the global LogManager
logManager = LogManager.getLogManager();

% add a file handler to the root logger
fileHandler = FileHandler('./Basic-RootFileHandler.log');
fileHandler.setLevel( Level.ALL );
rootLogger = logManager.getLogger('');
rootLogger.addHandler( fileHandler );

% add a logger instance to this script
% will use stack to generate name for logger since a name is not being provided
logger = Logger.getLogger( );
logger.setLevel( Level.ALL );

logger.info('Hi, this is info!');
logger.warning('Hi, this is warning!');

% side-effect is to close all handlers
logManager.resetAll();

Cite As

Matthew Spaethe (2024). Matlab logging facility (https://www.mathworks.com/matlabcentral/fileexchange/42078-matlab-logging-facility), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2012a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags
Acknowledgements

Inspired by: Design Pattern: Singleton (Creational)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0