Best Python code snippet using autotest_python
mylogger.py
Source:mylogger.py
...135 '''136 new_func = lambda self, msg, *args, **kwargs: Logger._log(self, level, msg, args, **kwargs)137 setattr(Logger, level_name_lower, new_func)138 return level139def add_file_handler(self, log_path, level=DEBUG, formatter=DEFAULT_FILEHANDLER_FORMATTER):140 ''' ç»Loggerå®ä¾æ·»å filehandler141 :param self: Loggerå®ä¾142 :param log_path: logæ件å°å143 :param level: filehandlerçæ¥å¿ç级144 :param formatter: filehandlerçæ¥å¿æ ¼å¼145 :return: è¿åæ°å建çfilehandlerï¼æ¹ä¾¿remove146 '''147 #å¿
é¡»å¾æ¯Loggerå®ä¾æ¥è°ç¨148 if not isinstance(self, Logger):149 root.error('the caller must be an instance of Logger')150 return False151 file_handler = logging.FileHandler(log_path, encoding='utf-8')152 file_handler.setLevel(level)153 file_handler.setFormatter(formatter)154 self.addHandler(file_handler)155 return file_handler156def __init():157 #设置æ§å¶å°è¾åº158 set_console_output(DEFAULT_FORMATTERS)159 '''160 types.MethodTypeæ¯ç»å®ä¾æ·»å æ¹æ³161 è¥ä½¿ç¨è¯¥æ¹æ³ç»ç±»æ·»å æ¹æ³ï¼åæ¯ç»ç±»æ·»å ç±»æ¹æ³ï¼ä¸ç®¡æ¯å®ä¾è¿æ¯ç±»å¯¹è±¡è°ç¨ï¼é½æ¯ä¼ å
¥ç±»å¯¹è±¡ä¸ºclsåæ°162 '''163 # Logger.add_file_handler = types.MethodType(add_file_handler, Logger)164 '''165 setattræ¯ç»å¯¹è±¡æ·»å å±æ§/æ¹æ³166 ç»ç±»æ·»å æ¶ï¼æ¯ä½ä¸ºç±»çæ®éå½æ°ï¼è°ç¨è¯¥æ¹æ³çå®ä¾ä¼å½åselfåæ°è¢«ä¼ å
¥167 ç»å®ä¾æ·»å æ¶ï¼æ¯ä½ä¸ºå®ä¾çæ¹æ³ï¼ä¸ä¼ä¼ å
¥self168 '''169 #éè¿ç»Loggeræ·»å éæå½æ°çæ¹æ³æ¥æ·»å add_file_handler170 setattr(Logger, 'add_file_handler', add_file_handler)171 if not os.path.exists(LOG_FOLDER):172 os.makedirs(LOG_FOLDER)173 debug_file_handle = RotatingFileHandler(filename=os.path.join(LOG_FOLDER, 'log'),174 maxBytes=1024*1024, backupCount=20, encoding='utf-8')175 debug_file_handle.setLevel(DEFAULT_FILE_LEVEL)176 debug_file_handle.setFormatter(DEFAULT_FILEHANDLER_FORMATTER)177 root.addHandler(debug_file_handle)178 warning_file_handle = RotatingFileHandler(filename=os.path.join(LOG_FOLDER, 'warning'),179 maxBytes=1024*1024, backupCount=20, encoding='utf-8')180 warning_file_handle.setLevel(WARNING)181 warning_file_handle.setFormatter(DEFAULT_FILEHANDLER_FORMATTER)182 root.addHandler(warning_file_handle)183 error_file_handle = RotatingFileHandler(filename=os.path.join(LOG_FOLDER, 'error'),184 maxBytes=1024*1024, backupCount=20, encoding='utf-8')185 error_file_handle.setLevel(ERROR)186 error_file_handle.setFormatter(DEFAULT_FILEHANDLER_FORMATTER)187 root.addHandler(error_file_handle)188__init()189if __name__ == '__main__':190 def test_root_logger():191 logger = getLogger()192 logger.debug('debug')193 logger.info('info')194 logger.warning('warning')195 logger.error('error')196 logger.critical('critical')197 198 def test_sub_logger():199 sub_logger = getLogger('lance')200 sub_logger.debug('debug')201 sub_logger.info('info')202 sub_logger.warning('warning')203 sub_logger.error('error')204 sub_logger.critical('critical')205 def test_root_add_file_handler():206 root = getLogger()207 file_handler = root.add_file_handler('./test_root_log.log', INFO)208 root.debug('debug')209 root.info('info')210 root.warning('warning')211 root.error('error')212 root.critical('critical')213 root.removeHandler(file_handler)214 def test_sub_logger_add_file_handler():215 root = getLogger()216 sub_logger = getLogger('lance')217 root_file_handler = root.add_file_handler('./test_root_log.log', INFO)218 sub_logger_file_handler = sub_logger.add_file_handler('./test_sub_log.log', INFO)219 sub_logger.debug('debug')220 sub_logger.info('info')221 sub_logger.warning('warning')222 sub_logger.error('error')223 sub_logger.critical('critical')224 root.removeHandler(root_file_handler)225 sub_logger.removeHandler(sub_logger_file_handler)226 def test_add_level():227 NORMAL = add_level(11, 'normal')228 root = getLogger()229 sub_logger = getLogger('lance')230 root_file_handler = root.add_file_handler('./test_root_log.log', NORMAL)231 sub_logger_file_handler = sub_logger.add_file_handler('./test_sub_log.log', NORMAL)232 root.normal('normal')233 sub_logger.normal('normal')234 root.removeHandler(root_file_handler)235 sub_logger.removeHandler(sub_logger_file_handler)236 # test_root_logger()237 # test_sub_logger()238 # test_root_add_file_handler()239 # test_sub_logger_add_file_handler()...
logger.py
Source:logger.py
...22file_date_format = '%Y-%m-%d'23formatter = logging.Formatter(24 '%(asctime)s\t%(levelname)-8s\t%(name)s\t%(message)s', datefmt='%y-%m-%d %H:%M:%S'25)26def add_file_handler(level):27 file_prefix = LOG_ROOT + NODE + '_' + format(datetime.now(), file_date_format)28 name = logging.getLevelName(level)29 file_handler = logging.FileHandler(filename=(file_prefix + '_%s.log' % name), encoding="utf-8")30 file_handler.name = name.lower()31 file_handler.setFormatter(formatter)32 file_handler.setLevel(level)33 logging.getLogger().addHandler(file_handler)34 if logging.getLogger().level > level:35 logging.getLogger().setLevel(level)36 return file_handler37def setSentry(url):38 global SENTRY_SET39 if SENTRY_SET:40 return41 try:42 import raven.handlers.logging as rhl43 except:44 SENTRY_SET = False45 return46 client = rhl.Client(url)47 sentry_handler = rhl.SentryHandler(client)48 sentry_handler.setFormatter(formatter)49 sentry_handler.setLevel(logging.WARNING)50 sentry_handler.name = "sentry"51 logging.getLogger().addHandler(sentry_handler)52 SENTRY_SET = True53def get_loggers(name):54 logger = logging.getLogger(name)55 logd = logger.debug56 logw = logger.warning57 loge = logger.error58 logi = logger.info59 return logi, logd, logw, loge60def setup(only_console=True, log_level=logging.INFO):61 global handler_error, handler_warning, handler_console, handler_debug, NO_FILE62 if handler_debug is not None:63 logging.getLogger().removeHandler(handler_debug)64 if handler_warning is not None:65 logging.getLogger().removeHandler(handler_warning)66 if handler_error is not None:67 logging.getLogger().removeHandler(handler_error)68 handlers = logging.getLogger().handlers69 if len(handlers):70 for h in handlers:71 if isinstance(h, logging.StreamHandler):72 handler_console = h73 break74 if handler_console is None:75 handler_console = logging.StreamHandler()76 if handler_console is not None:77 logging.getLogger().removeHandler(handler_console)78 handler_console.name = "console"79 handler_console.setFormatter(formatter)80 handler_console.setLevel(log_level)81 logging.getLogger().addHandler(handler_console)82 logging.getLogger().setLevel(log_level)83 if not only_console:84 if not os.path.exists(LOG_ROOT):85 try:86 os.makedirs(LOG_ROOT)87 NO_FILE = False88 except:89 traceback.print_exc()90 NO_FILE = True91 if not NO_FILE:92 handler_debug = add_file_handler(logging.DEBUG) if log_level <= logging.DEBUG else None93 handler_warning = add_file_handler(logging.WARNING) if log_level <= logging.WARNING else None94 handler_error = add_file_handler(logging.ERROR) if log_level <= logging.ERROR else None...
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!!