Matlab logging facility

版本 1.0.0.0 (12.2 KB) 作者: Matthew Spaethe
Adds ability to log messages. Borrowed ideas from Java logging, and possibly Python logging.
776.0 次下载
更新时间 2013/6/3

查看许可证

The Logger class is used for logging messages. The logger instances are named, and they are managed by LogManager.

The logger instance will compare the logging level of the message to its logging level. If the message's logging level meets or exceeds the logging level of the logger, the log message will be passed to its registered handlers. By default, the log message will also then be passed to its parent's handlers.

Handlers are what "emit" the log message to the outside world. Loggers decide if the log message should be seen by the Handlers.

The logging classes are in a Matlab package 'logging'. You will need to place the +logging directoy somewhere in your path. That is, the directory where the +logging directoy exists should be in your Matlab path -- not the +logging directory itself.

I would suggest doing a Google on java logging and reading a bit about Logger and LogManager. I think it may do a better job of trying to explain :).

Example code:

import logging.*

% --- BASIC CONSOLE ---

% get the global LogManager
logManager = LogManager.getLogManager();

% add a logger instance to this script
logger = Logger.getLogger('TestScript');
logger.setLevel( Level.ALL );

logger.info('Hi, this is info!');
logger.warning('Hi, this is warning!');

% side-effect is to close all handlers
logManager.resetAll();

% --- BASIC CONSOLE WITH FILE ---

% get the global LogManager
logManager = LogManager.getLogManager();

% add a file handler to the root logger
fileHandler = FileHandler('./Basic-RootFileHandler.log');
fileHandler.setLevel( Level.ALL );
rootLogger = logManager.getLogger('');
rootLogger.addHandler( fileHandler );

% add a logger instance to this script
% will use stack to generate name for logger since a name is not being provided
logger = Logger.getLogger( );
logger.setLevel( Level.ALL );

logger.info('Hi, this is info!');
logger.warning('Hi, this is warning!');

% side-effect is to close all handlers
logManager.resetAll();

引用格式

Matthew Spaethe (2024). Matlab logging facility (https://www.mathworks.com/matlabcentral/fileexchange/42078-matlab-logging-facility), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2012a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Scope Variables and Generate Names 的更多信息
标签 添加标签

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.0.0.0