Best Python code snippet using keyboard
Log.py
Source:Log.py
...27def set_handler(levels):28 if levels == 'error':29 logger.addHandler(MyLog.err_handler) # åæ¤æ¥å¿è®°å½å¨æ·»å æå®çå¤çç¨åºã30 logger.addHandler(MyLog.handler)31def remove_handler(levels):32 if levels == 'error':33 logger.removeHandler(MyLog.err_handler)34 logger.removeHandler(MyLog.handler)35def get_current_time():36 return time.strftime(MyLog.date, time.localtime(time.time()))37class MyLog:38 path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # å·¥ç¨æ ¹ç®å½39 log_file = path+'/Log/log.log'40 err_file = path+'/Log/err.log'41 logger.setLevel(LEVELS.get(level, logging.INFO)) # 设置æ¤æ¥å¿è®°å½å¨çæ¥å¿çº§å«ãç级å¿
é¡»æ¯intæstrã42 create_file(log_file)43 create_file(err_file)44 date = '%Y-%m-%d %H:%M:%S'45 handler = logging.FileHandler(log_file, encoding='utf-8') # ä¸ä¸ªå¤çç¨åºç±»ï¼å®å°æ ¼å¼åçæ¥å¿è®°å½åå
¥ç£çæ件ã46 err_handler = logging.FileHandler(err_file, encoding='utf-8')47 @staticmethod # éææ¹æ³æ éå®ä¾å;ä¹å¯ä»¥å®ä¾ååè°ç¨48 def debug(log_meg):49 set_handler('debug')50 logger.debug("[DEBUG " + get_current_time() + "]" + log_meg)51 remove_handler('debug')52 @staticmethod53 def info(log_meg):54 set_handler('info')55 logger.info("[INFO " + get_current_time() + "]" + log_meg)56 remove_handler('info')57 @staticmethod58 def warning(log_meg):59 set_handler('warning')60 logger.warning("[WARNING " + get_current_time() + "]" + log_meg)61 remove_handler('warning')62 @staticmethod63 def error(log_meg):64 set_handler('error')65 logger.error("[ERROR " + get_current_time() + "]" + log_meg)66 remove_handler('error')67 @staticmethod68 def critical(log_meg):69 set_handler('critical')70 logger.error("[CRITICAL " + get_current_time() + "]" + log_meg)71 remove_handler('critical')72if __name__ == "__main__":73 MyLog.debug("This is debug message") # æªå®ä¾åè°ç¨éææ¹æ³74 MyLog.info("This is info message")75 MyLog.warning("This is warning message")76 MyLog.error("This is error")77 MyLog.critical("This is critical message")78 mylog = MyLog() # å®ä¾åä¹ååè°ç¨...
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!!