Contents of this guide
This document provides guidelines and examples on how to integrate simple logging solution from Shared Snippets Repository in Python and MATLAB/Octave apps from Application Workbench.
Where to find loggers
Access Shared Snippets Repository and locate logger matching your application environment.
In case you need information on how to use the repository please refer to this guide Using shared code snippets (Integrating snippets into application is described here).
How to use the Python Logger
This logger implementation is a configuration of the standard Python logging
package. As such, it adheres to the standard API and functionality provided by the logging
module. All standard logging functions (debug()
, info()
, warning()
, error()
, exception()
, etc.) are available and can be used in the same way as with any other logger created via the logging
package.
To use the logger in your Python script, you first need to import the getDefaultLogger
function. This function is responsible for creating or retrieving a logger instance that can be used for logging messages:
from YOUR_LOGGER_PATH import getDefaultLogger
YOUR_LOGGER_PATH
is a logger path referring to location where logger has been added.
Once you have imported the function, you can obtain an instance of the logger by calling getDefaultLogger
with the desired logger name. The logger will be configured automatically to write log messages to a specified log file:logger = getDefaultLogger(__name__)
In this example, __name__
is passed as the logger name, which typically corresponds to the name of the current module. This helps to identify the source of the log messages.
After retrieving the logger instance, you can use it to log messages at various severity levels (e.g., INFO, DEBUG, ERROR).
Figure 1. Import, get and use logger in chosen script
How to use the MATLAB/Octave Logger
To access the logger, you can call the static getInstance()
method, which retrieves or creates the single instance of the logger:
logger = base_logger.getInstance();
Once you have the logger instance, you can log messages at different levels:
trace
: Used for very fine-grained debugging or tracing information.
logger.trace('Entering function foo with value', foo);debug
: Useful for debugging purposes, typically providing variable values or states.
logger.debug('Variable x has value', x);info
: Used to log general informational messages that indicate normal program operation.
logger.info('Application started');warning
: Logs messages that indicate potential issues that are not yet errors but may require attention.
logger.warning('Memory usage is approaching the limit');error
: Logs error messages, typically when exceptions or critical issues arise. You can also pass anMException
object to log the error message along with the full stack trace.try
% some code that may throw an error
catch e
logger.error('An error occurred', e);
end
Each of these methods accepts variable arguments (varargin
) so that you can pass different types of data (strings, numbers, or MException
objects) to the logger. If an MException
object is passed, the logger will capture the error message and provide a detailed stack trace in the log.
The logger writes all messages to a log file, appending new entries each time a message is logged. The format of each log entry includes the current timestamp, the log level, the name of the calling script, and the message itself.
Example:
logger.info('Iteration number', 5);
logger.error('Error encountered
', exception);
The log file will contain entries like:
2024-09-25 10:15:45 INFO my_script Iteration number, 5
2024-09-25 10:15:45 ERROR my_script Error encountered, Error: Something went wrong
at my_script (line 10)
Figure 2. Get and use logger in chosen script