Best Python code snippet using avocado_python
output.py
Source:output.py
...335 LOG_UI.addHandler(app_handler)336 LOG_UI.propagate = False337 LOG_UI.level = logging.DEBUG338 else:339 disable_log_handler(LOG_UI)340 app_err_handler = ProgressStreamHandler()341 app_err_handler.setFormatter(logging.Formatter("%(message)s"))342 app_err_handler.addFilter(FilterWarnAndMore())343 app_err_handler.stream = STD_OUTPUT.stderr344 LOG_UI.addHandler(app_err_handler)345 LOG_UI.propagate = False346 if not os.environ.get("AVOCADO_LOG_EARLY"):347 LOG_JOB.getChild("stdout").propagate = False348 LOG_JOB.getChild("stderr").propagate = False349 if "early" in enabled:350 add_log_handler("", logging.StreamHandler, STD_OUTPUT.stdout,351 logging.DEBUG)352 add_log_handler(LOG_JOB, logging.StreamHandler,353 STD_OUTPUT.stdout, logging.DEBUG)354 else:355 disable_log_handler("")356 disable_log_handler(LOG_JOB)357 if "remote" in enabled:358 add_log_handler("avocado.fabric", stream=STD_OUTPUT.stdout,359 level=logging.DEBUG)360 add_log_handler("paramiko", stream=STD_OUTPUT.stdout,361 level=logging.DEBUG)362 else:363 disable_log_handler("avocado.fabric")364 disable_log_handler("paramiko")365 # Not enabled by env366 if not os.environ.get('AVOCADO_LOG_DEBUG'):367 if "debug" in enabled:368 add_log_handler(LOG_UI.getChild("debug"), stream=STD_OUTPUT.stdout)369 else:370 disable_log_handler(LOG_UI.getChild("debug"))371 # Add custom loggers372 for name in [_ for _ in enabled if _ not in iterkeys(BUILTIN_STREAMS)]:373 stream_level = re.split(r'(?<!\\):', name, maxsplit=1)374 name = stream_level[0]375 if len(stream_level) == 1:376 level = logging.DEBUG377 else:378 level = (int(stream_level[1]) if stream_level[1].isdigit()379 else logging.getLevelName(stream_level[1].upper()))380 try:381 add_log_handler(name, logging.StreamHandler, STD_OUTPUT.stdout,382 level)383 except ValueError as details:384 LOG_UI.error("Failed to set logger for --show %s:%s: %s.",385 name, level, details)386 sys.exit(exit_codes.AVOCADO_FAIL)387 # Remove the in-memory handlers388 for handler in logging.root.handlers:389 if isinstance(handler, MemStreamHandler):390 logging.root.handlers.remove(handler)391 # Log early_messages392 for record in MemStreamHandler.log:393 logging.getLogger(record.name).handle(record)394class FilterWarnAndMore(logging.Filter):395 def filter(self, record):396 return record.levelno >= logging.WARN397class FilterInfoAndLess(logging.Filter):398 def filter(self, record):399 return record.levelno <= logging.INFO400class ProgressStreamHandler(logging.StreamHandler):401 """402 Handler class that allows users to skip new lines on each emission.403 """404 def emit(self, record):405 try:406 msg = self.format(record)407 if record.levelno < logging.INFO: # Most messages are INFO408 pass409 elif record.levelno < logging.WARNING:410 msg = TERM_SUPPORT.header_str(msg)411 elif record.levelno < logging.ERROR:412 msg = TERM_SUPPORT.warn_header_str(msg)413 else:414 msg = TERM_SUPPORT.fail_header_str(msg)415 stream = self.stream416 skip_newline = False417 if hasattr(record, 'skip_newline'):418 skip_newline = record.skip_newline419 stream.write(msg)420 if not skip_newline:421 stream.write('\n')422 self.flush()423 except (KeyboardInterrupt, SystemExit):424 raise425 except Exception:426 self.handleError(record)427class MemStreamHandler(logging.StreamHandler):428 """429 Handler that stores all records in self.log (shared in all instances)430 """431 log = []432 def emit(self, record):433 self.log.append(record)434 def flush(self):435 """436 This is in-mem object, it does not require flushing437 """438class PagerNotFoundError(Exception):439 pass440class Paginator(object):441 """442 Paginator that uses less to display contents on the terminal.443 Contains cleanup handling for when user presses 'q' (to quit less).444 """445 def __init__(self):446 paginator = os.environ.get('PAGER')447 if not paginator:448 try:449 paginator = "%s -FRX" % utils_path.find_command('less')450 except utils_path.CmdNotFoundError as details:451 raise RuntimeError("Unable to enable pagination: %s" % details)452 self.pipe = os.popen(paginator, 'w')453 def __del__(self):454 self.close()455 def close(self):456 try:457 self.pipe.close()458 except Exception:459 pass460 def write(self, msg):461 try:462 self.pipe.write(msg)463 except Exception:464 pass465 def flush(self):466 if not self.pipe.closed:467 self.pipe.flush()468def add_log_handler(logger, klass=logging.StreamHandler, stream=sys.stdout,469 level=logging.INFO, fmt='%(name)s: %(message)s'):470 """471 Add handler to a logger.472 :param logger_name: the name of a :class:`logging.Logger` instance, that473 is, the parameter to :func:`logging.getLogger`474 :param klass: Handler class (defaults to :class:`logging.StreamHandler`)475 :param stream: Logging stream, to be passed as an argument to ``klass``476 (defaults to ``sys.stdout``)477 :param level: Log level (defaults to `INFO``)478 :param fmt: Logging format (defaults to ``%(name)s: %(message)s``)479 """480 if isinstance(logger, string_types):481 logger = logging.getLogger(logger)482 handler = klass(stream)483 handler.setLevel(level)484 if isinstance(fmt, string_types):485 fmt = logging.Formatter(fmt=fmt)486 handler.setFormatter(fmt)487 logger.addHandler(handler)488 logger.propagate = False489 return handler490def disable_log_handler(logger):491 if isinstance(logger, string_types):492 logger = logging.getLogger(logger)493 # Handlers might be reused elsewhere, can't delete them494 while logger.handlers:495 logger.handlers.pop()496 logger.handlers.append(NULL_HANDLER())497 logger.propagate = False498class LoggingFile(object):499 """500 File-like object that will receive messages pass them to logging.501 """502 def __init__(self, prefixes=None, level=logging.DEBUG,503 loggers=None):504 """...
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!!