File Exchange

image thumbnail

SLF4M

version 1.3.0 (1.64 MB) by Andrew Janke
A simple logging framework for Matlab, built on top of SLF4J and Log4j

6 Downloads

Updated 16 Jan 2021

From GitHub

View license on GitHub

Summary:
A simple but powerful logging framework for Matlab

Description:

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.

Usage
-------------------------

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);
end

z = x + 42;

timestamp = datetime;
logger.info('Answer: z=%f, calculated at %s', z, timestamp);
if z > intmax('int32')
logger.warn('Large value z=%f will overflow int32', z);
end

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

end

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)
some_bad_operation(x);

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

>> m = containers.Map;
>> m('foo') = 42; m('bar') = struct;
>> logger.info('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.

Author
-----------------------------------

SLF4M is developed by Andrew Janke <andrew@apjanke.net>.

SLF4M's home page is the repo on GitHub: https://github.com/apjanke/SLF4M.

Cite As

Andrew Janke (2021). SLF4M (https://github.com/janklab/SLF4M/releases/tag/v1.3.0), GitHub. Retrieved .

MATLAB Release Compatibility
Created with R2016b
Compatible with R2016b and later releases
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!

Mcode/+logger

Mcode/+logger/+internal

Mcode/+logger/private

dev-tools

dev-tools/+database/+jdbc/@connection

dev-tools/+pkg

dev-tools/+pkg/+a/+deeply/+nested/+package

lib/matlab/dispstr/dispstr-1.1.1/Mcode

lib/matlab/dispstr/dispstr-1.1.1/Mcode-examples

lib/matlab/dispstr/dispstr-1.1.1/Mcode/+dispstrlib

lib/matlab/dispstr/dispstr-1.1.1/Mcode/+dispstrlib/+internal

lib/matlab/dispstr/dispstr-1.1.1/Mcode/private

To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.