File Exchange

image thumbnail


version 1.2.2 (1.71 MB) by Andrew Janke
A simple but powerful logging framework for Matlab

1 Download

Updated 30 Jan 2020

GitHub view license on GitHub

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 `fprintf()` statements in and out.

SLF4M provides:

* Logging functions

* A `dispstr` API for customizing object display.

* A Log4j configuration GUI

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 logging functions are in the `+logger` package. Call them from within your Matlab code.

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;

timestamp = datetime;'Answer: z=%f, calculated at %s', z, timestamp);
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)');


The output looks like this:

>> logger.initSLF4M
>> helloWorld
06:39:45.292 INFO helloWorld - Answer: z=165.456000, calculated at 09-Jan-2020 06:39:45
06:39:45.297 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

For more details, see the User's Guide included in the distribution.


SLF4M is developed by Andrew Janke <>.

SLF4M's home page is the repo on GitHub:

Cite As

Andrew Janke (2020). apjanke/SLF4M (, GitHub. Retrieved .

Comments and Ratings (3)

@Lockywolf: I don't know! I've tested it as far back as R2016b, and I have vague recollections of using the same technique at my day job as far back as circa 2005, so I think it'll work on quite old versions of Matlab. The only requirements are a) SLF4J, b) Apache Log4j 1.x, and c) Matlab's basic Java interoperability. SLF4J and Log4j 1.x have been around a long time.

I haven't tested it on older versions of Matlab: I'm mainly a Mac user, and older versions of Matlab don't work on newer versions of macOS. But if you or another user has an actual requirement to run it on an older version of Matlab and are running in to issues, let me know, and I'll try to fix it up. It ought to be doable.


What is the earliest version of Matlab this package works on?



* R2020a compatibility
* Switch dispstr from HEAD to releases

## Version (2020-01-09)

* Dummy release just to sync up File Exchange and GitHub Releases


## Version 1.2.1 (2020-01-09)

* Dummy release just to sync up File Exchange and GitHub Releases


## Version 1.2.0 (2020-01-09)

* Rename 'logm' package to 'logger'
* Add logger.version function
* Really use the vendored SLF4M library
* Require logger.initSLF4M for real


1.1.2 - 2020-01-09
* Update dispstr dependency
* dispstr: handle datetimes and categoricals
* Fix caller class/function name detection for static methods
* Allow configureBasicConsoleLogging to be called multiple times safely

Trying another newline for better File Exchange Description formatting.

Added some newlines to the File Exchange Description to make the formatting nicer.

"pre" was not supported in MathWorks File Exchange Description markup. Reverted.

Attempting to use "pre" markup in the file description

Updated the MathWorks File Exchange "Description" text.

Switched to hosting on GitHub and linking from File Exchange to there, for compatibility with dual-licensing

Fix missing JAR file in lib for the GUI

Updated license terms: clarify that SLF4M is BSD 2-clause, and change log4j-config-gui dependency to BSD 2-clause as well. No code changes.

* Added exception formatting to the log function signatures

Uploaded a screenshot

MATLAB Release Compatibility
Created with R2016b
Compatible with R2016a to any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags