View License

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

» Watch video

Highlights from
log4m - A powerful and simple logger for matlab

4.8 | 14 ratings Rate this file 25 Downloads (last 30 days) File Size: 3 KB File ID: #37701 Version: 1.0

log4m - A powerful and simple logger for matlab


Luke Winslow (view profile)


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

| Watch this File

File Information

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'.


Log4matlab inspired this file.

Required Products MATLAB
MATLAB release MATLAB 7.12 (R2011a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (20)
21 Jan 2017 Andrea Laruelo

14 Sep 2016 Dominique

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

Comment only
05 Apr 2016 Alexander Andreychenko

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.

Comment only
22 Mar 2016 Alexander Andreychenko

15 Mar 2016 Alexander Andreychenko

29 Feb 2016 Max Selyuk

20 Feb 2015 David Legland

David Legland (view profile)

04 Feb 2015 Regis Ruelland

very useful, thanks !

14 Jan 2015 Semjon

Semjon (view profile)

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

11 Jan 2015 Muhammad

30 Apr 2014 Yibiao

Yibiao (view profile)

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

30 Apr 2014 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.

Comment only
29 Apr 2014 Yibiao

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?

Comment only
16 Apr 2014 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.

Comment only
16 Apr 2014 Yibiao

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!

Comment only
29 Jan 2014 E. L. Rayle

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.

28 Jan 2014 Christophe Trefois

22 Nov 2013 kpsychas

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

22 Nov 2013 kpsychas

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.

16 May 2013 Christopher


This is a great tool!

Contact us