Skip to the content.


View SLF4M on File Exchange

SLF4M is a simple but flexible logging framework for Matlab, built on top of SLF4J and Apache Log4j. You can use it to do runtime-configurable logging from your Matlab scripts and programs. This can be more informative and more manageable than commenting in and out fprintf() statements.

SLF4M provides:

The API is simple enough that you can get up and running with it quickly, or even use it casually in scripts, but it’s flexible and powerful enough to be useful for larger systems.

(The dispstr part is optional; you don’t have to learn it in order to use SLF4M logging.)

SLF4M screenshot showing log output and configuration GUI


To install, unzip the distribution somewhere, and add its Mcode/ directory to your Matlab path.

The logging functions are in the +logger package. Call them from within your Matlab code to emit log messages. In order of descending severity of logging level, they are:

The logging functions take sprintf()-style formatting arguments. You can also pass an MException as the first argument to include the error message and stack trace in the log message.

function helloWorld(x)

if nargin < 1 || isempty(x)
    x = 123.456;
    % These debug() calls will only show up if you set log level to DEBUG
    logger.debug('Got empty x input; defaulted to %f', x);
z = x + 42;'Answer z=%f', z);
if z > intmax('int32')
    logger.warn('Large value z=%f will overflow int32', z);

catch err
    logger.error(err, 'Something went wrong in some_bad_operation(x=%f)', x);


The output looks like this:

>> helloWorld
16:53:18.279 INFO helloWorld() - Answer z=165.456000
16:53:18.291 ERROR helloWorld() - Something went wrong in some_bad_operation(123.456000)
Undefined function 'some_bad_operation' for input arguments of type 'double'.
Error in helloWorld (line 16)

Thanks to dispstr(), you can also pass Matlab objects to the %s conversions.

>> m = containers.Map;
>> m('foo') = 42; m('bar') = struct;
>>'Hello, world! %s', m)
09:52:29.809 INFO  base - Hello, world! 2-by-1 containers.Map

If you want to customize how logging is displayed and where it goes, call logger.Log4jConfigurator.configureBasicConsoleLogging() at the beginning of your program to set up basic logging. If you want your logs to be saved to a file somewhere, call Matlab’s diary function.

To launch an interactive configuration GUI, run logger.Log4jConfigurator.showGui. This GUI lets you see and set the logging levels and other attributes of the various loggers in your Matlab session.

For more details, see the User’s Guide.


A kind of recent version of Matlab. SLF4M was developed and tested on Matlab R2016b and R2017b, but it will probably work on some older Matlab versions too.


SLF4M is a thin layer built on top of SLF4J and Log4j. It is compatible with any other Java or Matlab code that uses SLF4J or Log4j.

Log4j was chosen as the back-end because that’s what ships with Matlab.


SLF4M is dual licensed under the business-friendly Apache 2.0 and BSD 2-clause licenses. Pick whichever you like.

More info



SLF4M is developed by Andrew Janke. View its home page or get the code from the repo on GitHub.