Best Python code snippet using Airtest
log.py
Source:log.py  
...21  >>> libtbx.log.debug("Debugging output.")22  >>> libtbx.log.logger.set_quiet(True)23  >>> libtbx.log.info("This should be muted.")24  # Output file25  >>> libtbx.log.logger.set_logfile("test.log")26  >>> libtbx.log.info("Redirected to test.log")27  # Experimental sys.stdout redirection28  >>> libtbx.log.logger.set_logfile("test.log")29  >>> sys.stdout = libtbx.log.logger30  >>> print "stdout redirection to test.log"31  """32  def __init__(self):33    # create logger34    self.log = logging.getLogger('phenix')35    self.log.setLevel(logging.INFO)36    # create formatter37    self.formatter = logging.Formatter()38    # Default is to print to stdout39    self.ch = None40    self.set_stdout()41  def write(self, data):42    self.log.info(data)43  def flush(self):44    try:45      self.ch.flush()46    except Exception:47      pass48  def close(self):49    try:50      self.ch.close()51    except Exception:52      pass53  def set_quiet(self, state=True):54    level = logging.INFO55    if state:56      level = logging.ERROR57    self.log.setLevel(level)58  def set_debug(self, state=True):59    level = logging.INFO60    if state:61      level = logging.DEBUG62    self.log.setLevel(level)63  def set_logfile(self, filename, mode='w'):64    ch = logging.FileHandler(filename, mode=mode)65    ch.setFormatter(self.formatter)66    if self.ch:67      self.log.removeHandler(self.ch)68    self.log.addHandler(ch)69    self.ch = ch70  def set_fileobj(self, fileobj):71    pass72  def set_stdout(self):73    ch = logging.StreamHandler()74    ch.setFormatter(self.formatter)75    if self.ch:76      self.log.removeHandler(self.ch)77    self.log.addHandler(ch)78    self.ch = ch79# Singleton80logger = logger()81# Set the module functions.82log = logger.log.info83debug = logger.log.debug84info = logger.log.info85warn = logger.log.warn86error = logger.log.error87critical = logger.log.critical88##### Testing #####89import unittest90class TestLog(unittest.TestCase):91  def test_f(self):92    logger.set_debug(True)93    tests = [debug, info, warn, error, critical]94    for test in tests:95      logger.set_logfile("test.log")96      value = "test %s"%test97      test(value)98      logger.flush()99      logger.close()100      with open("test.log") as f:101        data = f.read()102        assert value in data103  def test_debug(self):104    print("Check set_debug")105    logger.set_logfile("test.log")106    logger.set_debug(False)107    debug("debug: muted")108    logger.set_debug(True)109    debug("debug: ok")110    logger.set_debug(False)111    with open("test.log") as f:112      data = f.read()113      assert "muted" not in data114      assert "ok" in data115  def test_quiet(self):116    print("Check set_quiet")117    logger.set_logfile("test.log")118    logger.set_quiet(False)119    info("quiet: ok")120    logger.set_quiet(True)121    info("quiet: muted")122    logger.set_quiet(False)123    with open("test.log") as f:124      data = f.read()125      assert "muted" not in data126      assert "ok" in data127  def test_logfile(self):128    print("Check output file")129    logger.set_logfile("test.log")130    info("logfile: ok")131    with open("test.log") as f:132      data = f.read()133      assert "ok" in data134  def test_stdout(self):135    oldsys = sys.stdout136    print("Checking sys.stdout redirect")137    sys.stdout = logger138    print("redirect: ok")139    sys.stdout = oldsys140    print("... and back again")141if __name__ == "__main__":...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!!
