File Exchange

image thumbnail

log4m - A powerful and simple logger for matlab

version 1.0 (3 KB) by

A simple logger with log4j functionality but designed with the matlab-user in mind.

17 Ratings



View License

Description: Log4m is designed to be relatively fast and very easy to use. It has been designed to work well in a matlab environment.

log4m uses the same level system as log4j {'ALL','TRACE','DEBUG','INFO','WARN','ERROR','FATAL','OFF'} and is an attempt to create a single-file, robust drop-in system for more advanced logging. It only provides a single logger object within an entire matlab instance, so you don't need to track a file or object reference.

I currently use this in long-running compiled jobs so I can track how they are performing without manual intervention or observation.


%To create the logger reference:
L = log4m.getLogger('logfile.txt');

% To log an error event
L.error('exampleFunction','An error occurred');

% To log a trace event
L.trace('function','Trace this event');


If you want to display all logging information to the command prompt while only writing major events worse than an error to the log file, you can set the desired log levels accordingly.


%Now all messages will be displayed to the command prompt while only error and fatal messages will be logged to file.

Note: This project is similar to the log4matlab code acknowledged, but is easier to use and has an API more in the 'matlab style'.

Comments and Ratings (24)

E Swan

E Swan (view profile)

Suggestion: Add a varargin argument to each logging function and update . This allows the client to pass in a string format with inputs, but avoid interpolation if the current log level is > the call. For example:

L.trace('func', 'function called with arg %d', arg);

I've done this already in my local version.

Belal Dawwas

Ales Zita


Alexander: here is a modification of this package that allows multiple loggers and colored output in the terminal:

Is there any opportunity to modify this awesome logger such that it supports several 'logger' objects. My purpose is to have the array of logger objects and use it in the parallel execution such that each of those loggers uses its own file.
I was trying to modify that by having the additional property like stringId but I was not successful.

Max Selyuk

David Legland

David Legland (view profile)

very useful, thanks !


Semjon (view profile)

Exactly the thing I was looking for. Well documented and easy to use. Works for multi-access, too.



Yibiao (view profile)

Ah, now it works. This an awesome logger!Thanks Luke!

Luke Winslow

Ah, I see your issue. This doesn't catch *any* error, it only logs errors you specifically send to it using the "L.error" method. You could use a try-catch block to catch the error and then send the text to L.error.


Yibiao (view profile)

Hi Luke, thanks for the reply!

I did double check the spelling and access permission, but just to avoid any mistake I made the path as short as in the code below. After running it, I do see the logfile.txt file generated in H:\.

Here is the code:

clear all;
L = log4m.getLogger('H:\logfile.txt');

myfunc is a non-existing fucntion, so I get the expected error on command window(in red):

Undefined function or variable 'myfunc'.
Error in Foo (line 4)

But nothing is written into logfile.txt.
Do you have idea what I am doing wrong?

Luke Winslow

Yibiao, I'm not sure. Can you check that you have access rights to that area of the file system? Also, you may want to double-check the path. If you have typos in it, log4m may not give you an error saying it can't write to that area.


Yibiao (view profile)

This looks amazing, but I have trouble making it work:

I put the following code in my "main function":

L = log4m.getLogger('<MyDir>\logfile.txt');

It does create a logfile.txt in <MyDir>, however, nothing is written to it after I interntionally triggering some errors in my program(I can see the errors on command window).

What am I doing wrong here? Any suggestion would be appreciated!

This is very good. Allows for the standard levels of logging from trace to debug to error (any more in between). And you can limit which level of logging occurs. It would be even better if it could mimic the log4j profiles that let you set patterns to determine which classes will show logging. Thanks for writing it. It has saved me a lot of time.


I was wrong before, log4m.getLogger returns an instance of the logger and this is the way to go


I think this is the best you can get in matlab. Unfortunately you either need to define L as a global variable or pass it as argument to functions to reuse it.



This is a great tool!

MATLAB Release
MATLAB 7.12 (R2011a)
Tags Add Tags

Inspired by: log4matlab

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today