4.8

4.8 | 6 ratings Rate this file 33 Downloads (last 30 days) File Size: 3 KB File ID: #37701

log4m - A powerful and simple logger for matlab

by

 

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

| Watch this File

File Information
Description

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.

--

Example:
%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.

L.setCommandWindowLevel(L.ALL);
L.setLogLevel(L.ERROR);

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

Acknowledgements

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 (10)
30 Apr 2014 Yibiao

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.

29 Apr 2014 Yibiao

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');
L.setLogLevel(L.ALL);
myfunc();

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)
myfunc();

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

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.

16 Apr 2014 Yibiao

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');
L.setLogLevel(L.ALL);

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!

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

Thanks!

This is a great tool!

Contact us