How to use add_file_handler method in autotest

Best Python code snippet using autotest_python

mylogger.py

Source:mylogger.py Github

copy

Full Screen

...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()...

Full Screen

Full Screen

logger.py

Source:logger.py Github

copy

Full Screen

...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...

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:

YouTube

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

Run autotest 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?

Helpful

NotHelpful