Best Python code snippet using nose2
test_log_factory.py
Source:test_log_factory.py
...141 self.logger.x = 1142 self.logger.log_level = 'INFO'143 self.logger.warning('warning message')144 self.assertEqual(1, self.logger.x)145 def test_parse_log_level(self):146 log_range = self.logger.parse_log_level("<=INFO")147 self.assertEqual([0,1], log_range)148 log_range = self.logger.parse_log_level("<INFO")149 self.assertEqual([0], log_range)150 log_range = self.logger.parse_log_level(">=WARNING")151 self.assertEqual([2,3,4], log_range)152 log_range = self.logger.parse_log_level(">WARN")153 self.assertEqual([3,4], log_range)154 log_range = self.logger.parse_log_level("=INFO")155 self.assertEqual([1], log_range)156 log_range = self.logger.parse_log_level("CRITICAL")157 self.assertEqual([4], log_range)158 def test_register_callback(self):159 def add_1(log_obj, log_message=None, log_extra=None):160 pass161 def add_2(log_obj, log_message=None, log_extra=None):162 pass163 def add_3(log_obj, log_message=None, log_extra=None):164 pass165 def add_4(log_obj, log_message=None, log_extra=None):166 pass167 self.logger.register_callback('>=INFO', add_1)168 self.logger.register_callback('<=WARN', add_2)169 self.logger.register_callback('ERROR', add_3)170 self.logger.register_callback('*', add_4)...
service_logging.py
Source:service_logging.py
...38 if not file_cli:39 file_cli = config.get(LoggingConfKey.FILE)40 if not level_cli:41 level_cli = config.get(LoggingConfKey.LEVEL)42 level_cli = cls.parse_log_level(level_cli)43 if not print_console_cli:44 print_console_cli = config.get(LoggingConfKey.PRINT_CONSOLE, False)45 if not skip_times_cli:46 skip_times_cli = config.get(LoggingConfKey.SKIP_TIMES, False)47 format_with_ts = '%(asctime)s [%(levelname)8s] %(name)s: %(message)s'48 format_no_ts = '[%(levelname)8s] %(name)s: %(message)s'49 if file_cli:50 log_dir = os.path.dirname(file_cli)51 if not os.path.exists(log_dir):52 os.makedirs(log_dir, exist_ok=True)53 max_bytes = config.get(LoggingConfKey.MAX_BYTES, 1048576)54 max_count = config.get(LoggingConfKey.MAX_COUNT, 5)55 handler = logging.handlers.RotatingFileHandler(56 file_cli,57 maxBytes=int(max_bytes),58 backupCount=int(max_count)59 )60 formatter = logging.Formatter(format_with_ts)61 handler.setFormatter(formatter)62 handlers.append(handler)63 if skip_times_cli:64 log_format = format_no_ts65 else:66 log_format = format_with_ts67 if print_console_cli or skip_times_cli:68 handlers.append(logging.StreamHandler(sys.stdout))69 logging.basicConfig(70 format=log_format,71 level=level_cli,72 handlers=handlers73 )74 module_levels = config.get(LoggingConfKey.MODULES_LEVELS, {})75 for logger_name, log_level in module_levels.items():76 log_level = cls.parse_log_level(log_level)77 logger = logging.getLogger(logger_name)78 logger.setLevel(log_level)79 @classmethod80 def parse_log_level(cls, value):81 value = value or LOGGING_DEFAULT_LOG_LEVEL82 if not isinstance(value, type(logging.INFO)):83 input_value = str(value).lower().strip() if value is not None else value84 if input_value == "debug":85 value = logging.DEBUG86 elif input_value == "info":87 value = logging.INFO88 elif input_value == "warning":89 value = logging.WARNING90 elif input_value == "error":91 value = logging.ERROR92 else:93 value = logging.INFO94 return value...
log.py
Source:log.py
...44 def indentation(self):45 return self.__indentation46 def __init__(self, configuration_string = None, indentation = None):47 self.__indentation = indentation or Logger.Indentation()48 def parse_log_level(string):49 string = string.lower()50 if string == 'log':51 return LogLevel.log52 elif string == 'trace':53 return LogLevel.trace54 elif string == 'debug':55 return LogLevel.debug56 elif string == 'dump':57 return LogLevel.dump58 else:59 raise Exception('invalid log level: %s' % string)60 self.__components = {}61 if configuration_string is not None:62 for component in configuration_string.split(','):63 colons = component.count(':')64 if colons == 0:65 level = parse_log_level(component)66 component = None67 elif colons == 1:68 component, level = component.split(':')69 level = parse_log_level(level)70 else:71 raise Exception('invalid log configuration: %s' % component)72 if component is not None:73 self.__components[component] = level74 def log(self, component, level, message, *args):75 if level <= self.__components.setdefault(component, LogLevel.log):76 print('%s%s' % (' ' * self.__indentation.indentation,77 message % args),78 file = sys.stderr)79 return self.__indentation80 else:81 return NOOP82# DEBUG_TRACE = 183# DEBUG_TRACE_PLUS = 2...
app_logging.py
Source:app_logging.py
...20 if not log_file:21 log_file = config_data.get("log_file")22 if not log_level:23 log_level = config_data.get("log_level")24 log_level = cls.parse_log_level(log_level)25 if print_logs is None:26 print_logs = config_data.get("print_logs", False)27 if systemd_mode is None:28 systemd_mode = config_data.get("systemd_mode", False)29 format_with_ts = '%(asctime)s [%(levelname)8s] %(name)s: %(message)s'30 format_no_ts = '[%(levelname)8s] %(name)s: %(message)s'31 if log_file:32 log_dir = os.path.dirname(log_file)33 if not os.path.exists(log_dir):34 os.makedirs(log_dir, exist_ok=True)35 max_bytes = config_data.get("max_bytes", 1048576)36 max_count = config_data.get("max_count", 5)37 handler = logging.handlers.RotatingFileHandler(38 log_file,39 maxBytes=int(max_bytes),40 backupCount=int(max_count)41 )42 formatter = logging.Formatter(format_with_ts)43 handler.setFormatter(formatter)44 handlers.append(handler)45 if systemd_mode:46 log_format = format_no_ts47 else:48 log_format = format_with_ts49 if print_logs or systemd_mode:50 handlers.append(logging.StreamHandler(sys.stdout))51 logging.basicConfig(52 format=log_format,53 level=log_level,54 handlers=handlers55 )56 @classmethod57 def parse_log_level(cls, value):58 value = value or LOGGING_DEFAULT_LOG_LEVEL59 if not isinstance(value, type(logging.INFO)):60 input_value = str(value).lower().strip() if value is not None else value61 if input_value == "debug":62 value = logging.DEBUG63 elif input_value == "info":64 value = logging.INFO65 elif input_value == "warning":66 value = logging.WARNING67 elif input_value == "error":68 value = logging.ERROR69 else:70 value = logging.INFO71 return value
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!!