File Exchange

image thumbnail

apjanke/SLF4M

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.

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 (2020). apjanke/SLF4M (https://www.github.com/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.

Lockywolf

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

Updates

1.2.2

* R2020a compatibility
* Switch dispstr from HEAD to releases

1.2.1.1

## Version 1.2.1.1 (2020-01-09)

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

1.2.1

## Version 1.2.1 (2020-01-09)

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

1.2.0

## 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

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

1.1.1.5

Trying another newline for better File Exchange Description formatting.

1.1.1.4

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

1.1.1.3

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

1.1.1.3

Attempting to use "pre" markup in the file description

1.1.1.2

Updated the MathWorks File Exchange "Description" text.

1.1.1.1

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

1.1.1.0

Fix missing JAR file in lib for the GUI

1.1.0.1

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.

1.1.0.0

* Added exception formatting to the log function signatures

1.0.0.1

Uploaded a screenshot

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

Mcode/+logger

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