# Logger setup in python
def get_logger(self) -> logging.RootLogger:
"""instance of logger module, will be used for logging operations"""
# logger config
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# log format
lg_format = "%(levelname)s|%(filename)s:%(lineno)d|%(asctime)s|%(message)s"
log_format = logging.Formatter(lg_format)
# file handler
file_handler = logging.FileHandler("log/monitor_model.log")
file_handler.setFormatter(log_format)
logger.handlers.clear()
logger.addHandler(file_handler)
return logger
formatter = logging.Formatter('[%(asctime)s] p%(process)s {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s','%m-%d %H:%M:%S')
dependencies {
implementation 'com.github.Minecraftian14:MyLOGGER:v5.3'
}
How do you use Log4J in your framework?
printing/logging the important events of the application/test run.
in my project I did logging using the log4j library.
I added the library dependency into pom.xml.
For logging we create an object from
Logger Interface and LogManager class using
getLogger method and passing the class name in it;
private static Logger log = LogManager.getLogger(LogDemo.class.getName());
static Logger log = Logger.getLogger(log4jExample.class.getName());
We create it by passing the name of the current class.
Then we can use this object to do our logging.
log.info
log.debug
log.fatal
log.error
The Logger object is responsible for capturing
logging information and they are stored in a namespace hierarchy.
[loggers]
keys=root
[handlers]
keys=console, file
[formatters]
keys=std_out
[logger_root]
handlers = console, file
level = DEBUG
[handler_console]
class = logging.StreamHandler
level = DEBUG
formatter = std_out
[handler_file]
class = logging.FileHandler
kwargs = {"filename": "all_messages_conf.log"}
level = INFO
formatter = std_out
[formatter_std_out]
format = %(levelname)s : %(name)s : %(module)s : %(funcName)s : %(message)s
import logging
def get_logger(log_file_name, log_sub_dir=""):
""" Creates a Log File and returns Logger object """
# Build Log File Full Path
logPath = log_file_name if os.path.exists(log_file_name) else os.path.join(log_dir, (str(log_file_name) + '.log'))
# Create logger object and set the format for logging and other attributes
logger = logging.Logger(log_file_name)
# Return logger object
return logger