Best Python code snippet using autotest_python
loggers.py
Source:loggers.py
1# Configuring all Loggers2import sys3from defs import SYSTEM_LOG4import logging5# Dict of all instantiated loggers6loggers = {}7# Main System Logger8class SystemLogger:9 def __init__(self, logger_name: str, add_file_handler: bool = True, add_stream_handler: bool = True):10 self.logger_name = logger_name11 # If logger exists, return existing logger12 if loggers.get(self.logger_name):13 self.logger = loggers.get(self.logger_name)14 # Otherwise create new logger15 else:16 # input params17 self.add_file_handler = add_file_handler18 self.add_stream_handler = add_stream_handler19 # Define log formatter20 self.formatter = logging.Formatter('%(asctime)s | %(name)s | %(levelname)s | %(message)s')21 # Define handlers22 self.file_handler = logging.FileHandler(SYSTEM_LOG, mode='a')23 self.file_handler.setFormatter(self.formatter)24 self.stream_handler = logging.StreamHandler(sys.stdout)25 self.stream_handler.setFormatter(self.formatter)26 # Get logger27 self.logger = self.getLogger()28 # Set default logging level to DEBUG29 self.logger.setLevel(logging.DEBUG)30 # Save to loggers dict31 loggers[self.logger_name] = self.logger32 # Function to get logger33 def getLogger(self) -> logging.Logger:34 logger = logging.getLogger(name=self.logger_name)35 self.addHandlers(logger)36 return logger37 # Function to add handlers to logger38 def addHandlers(self, logger: logging.Logger) -> logging.Logger:39 # Add handler iff logger is parent (i.e. not child) and requested40 if ("." not in self.logger_name) and self.add_file_handler:41 logger.addHandler(self.file_handler)42 if ("." not in self.logger_name) and self.add_stream_handler:43 logger.addHandler(self.stream_handler)...
log.py
Source:log.py
...3import logging4LOG_FORMAT = "%(asctime)s [%(levelname)s - %(module)s] %(message)s"5TIME_FORMAT = "%Y-%m-%d %H:%M:%S"6FORMATTER = logging.Formatter( LOG_FORMAT, TIME_FORMAT )7def add_stream_handler( logger, stream=sys.stdout, log_level=logging.INFO ):8 # Set up a simple Stream handler9 stream_handler = logging.StreamHandler( stream=stream )10 stream_handler.setFormatter( FORMATTER )11 stream_handler.setLevel( log_level )12 logger.addHandler( stream_handler )13def add_file_handler( logger, log_file='hla_pipeline.log', log_level=logging.INFO ):14 # Set a second handler for the log file15 file_handler = logging.FileHandler( log_file )16 file_handler.setFormatter( FORMATTER )17 file_handler.setLevel( log_level )18 logger.addHandler( file_handler )19def initialize_logger( logger, stream=None, log_file=None, debug=False):20 if debug:21 log_level = logging.DEBUG22 else:23 log_level = logging.INFO24 logger.setLevel( log_level )25 if stream:26 add_stream_handler( logger, stream=stream, log_level=log_level )27 else:28 add_stream_handler( logger, log_level=log_level )29 if log_file:30 add_file_handler( logger, log_file=log_file, log_level=log_level )31 else:32 add_file_handler( logger, log_level=log_level )...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!