How to use set_logfile method in Airtest

Best Python code snippet using Airtest Github


Full Screen

...21 >>> libtbx.log.debug("Debugging output.")22 >>> libtbx.log.logger.set_quiet(True)23 >>>"This should be muted.")24 # Output file25 >>> libtbx.log.logger.set_logfile("test.log")26 >>>"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 = None40 self.set_stdout()41 def write(self, data):42 def flush(self):44 try:45 except Exception:47 pass48 def close(self):49 try:50 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.log.removeHandler( self.log.addHandler(ch)69 = ch70 def set_fileobj(self, fileobj):71 pass72 def set_stdout(self):73 ch = logging.StreamHandler()74 ch.setFormatter(self.formatter)75 if self.log.removeHandler( self.log.addHandler(ch)78 = 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 = 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 = 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 = 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 = 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__":...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Airtest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?