Best Python code snippet using robotframework-pageobjects_python
abstractedlogger.py
Source:abstractedlogger.py
...10 def __init__(self):11 self.in_robot = Context.in_robot()12 self.formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')13 self.threshold_level_as_str = self.get_threshold_level_as_str()14 self.threshold_level_as_int = self.get_log_level_from_str(self.threshold_level_as_str)15 if self.in_robot:16 self.logger = robot.api.logger17 else:18 # Stream handler is attached from log() since19 # that must be decided at run-time, but here we might as well20 # do the setup to keep log() clean.21 self.stream_handler = logging.StreamHandler(sys.stdout)22 self.stream_handler.setFormatter(self.formatter)23 # We have to instantiate a logger to something and this is a global24 # logger so we name it by this class. Later in this class'25 # log method, we manipulate the msg string to include the calling26 # page class name.27 logger = logging.getLogger(self.__class__.__name__)28 logger.setLevel(self.threshold_level_as_int)29 fh = logging.FileHandler("po_log.txt", "w")30 fh.setLevel(self.threshold_level_as_int)31 fh.setFormatter(self.formatter)32 logger.addHandler(fh)33 self.logger = logger34 @staticmethod35 def get_threshold_level_as_str():36 ret = OptionHandler(object()).get("log_level") or "INFO"37 return ret.upper()38 @staticmethod39 def get_log_level_from_str(level_as_str):40 """ Convert string log level to integer41 logging level."""42 str_upper = level_as_str.upper()43 try:44 return getattr(logging, str_upper)45 except AttributeError:46 return getattr(logging, "INFO")47 @staticmethod48 def get_normalized_logging_levels(level_as_str, in_robot):49 """ Given a log string, returns the translated log level string and the translated50 python logging level integer. This is needed because there are logging level51 constants defined in Robot that are not in Python, and Python logging level52 constants that are not defined in Robot.53 """...
log_levels.py
Source:log_levels.py
...18logging._nameToLevel["SPAM"] = logging.SPAM # type: ignore19logging._nameToLevel["VERBOSE"] = logging.VERBOSE # type: ignore20logging._nameToLevel["NOTICE"] = logging.NOTICE # type: ignore21logging._nameToLevel["SUCCESS"] = logging.SUCCESS # type: ignore22def get_log_level_from_str(log_level_str: str) -> int:23 """24 gets the log level as integer from a string25 >>> assert get_log_level_from_str('42') == 4226 >>> assert get_log_level_from_str('info') == 2027 >>> get_log_level_from_str('unknown')28 Traceback (most recent call last):29 ...30 ValueError: can not detect log level from string "unknown"31 """32 try:33 log_level = int(log_level_str)34 return log_level35 except ValueError:36 pass37 try:38 log_level = logging._nameToLevel[log_level_str.upper()] # noqa39 return log_level40 except KeyError:41 raise ValueError(f'can not detect log level from string "{log_level_str}"')
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!!