Logging Library¶
xpresso.ai provides Logging classes for both Python as well as Java developers. These enable logging in a standardized format, with output to one or more of:
a log file
console
an instance of Logstash, where the log messages are indexed using Elastic Search, and can be queried and viewed using Kibana
Configuring the Logging library
The logging library can be configured by changing entries in the main xpresso.ai configuration file - this is located at xpresso_ai/config/common.json inside your solution root folder.
The following configuration parameters can be set:
Parameter |
Description |
Possible values |
---|---|---|
log_to_console |
Flag to indicate if logging to console is turned on or off. If true, log messages will appear on the developer console (when testing locally), or in the Kubernetes pod logs (when deployed to Kubernetes) |
true or false |
log_to_file |
Flag to indicate if logging to file is turned on or off. If true, log messages will be stored in a file called “default.log” in the log folder mentioned in the parameter “logs_folder_path” (see below) |
true or false |
log_to_elk |
Flag to indicate if logging to the ELK stack is turned on or off. If true, log messages will be sent to the ELK stack and will be visible on the Kibana dashboard |
true or false |
logs_folder_path |
Path of folder containing log file |
any valid path (path will be created if it does not exist) |
The XprLogger Class
The main class associated with xpresso.ai logging utilities is XprLogger. This is a wrapper on top of logging utilities provided in Python.
Constructor
The constructor takes two arguments - the name of the component which is using the logger, and the logging level. The logging level can be one of the levels defined in the logging class (CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET).
Tip
It is advisable to set the logger level to INFO, and use the XprLogger info method to log messages. This is because xpresso.ai core libraries also use the logger to generate debug messages. So, you tend to see too many messages if you operate the logger in DEBUG mode
Sample Code for constructing a logger
my_logger = XprLogger ("my_function", logging.INFO)
Methods
The class has methods such as debug, info, fatal, etc. to log messages of different levels.
Input Parameters for each method:
Name |
Type |
De scription |
M andatory? |
Comments* |
---|---|---|---|---|
message |
String |
log message |
Yes |
|
stack_info |
boolean |
indicates whether the stack trace is to be logged |
No |
Sample logging code
# Loading the Logger class
from xpresso.ai.core.logging.xpr_log import XprLogger
#Creating an instance of that class
logger = XprLogger("plj_clean_gender", level=logging.INFO)
#Logging using the created object
#log level = debug
logger.debug("debug message")
#log level = info
logger.info("info message")
#log level = warning
logger.warning("warning message")
#log level = error
logger.error("error message")
# log level = fatal
logger.critical("critical message")
# to log a info level message along with the stack information
logger.info("Info Message with stack information",stack_info=True)