[docs]defread_log_config(config_parser):""" Read an Oasis standard logging config """log_file=config_parser['LOG_FILE']log_level=config_parser['LOG_LEVEL']log_max_size_in_bytes=int(config_parser['LOG_MAX_SIZE_IN_BYTES'])log_backup_count=int(config_parser['LOG_BACKUP_COUNT'])log_dir=os.path.dirname(log_file)ifnotos.path.exists(log_dir):os.makedirs(log_dir)logger=logging.getLogger('oasislmf')forhandlerinlist(logger.handlers):ifhandler.name=='oasislmf':logger.removeHandler(handler)breakhandler=RotatingFileHandler(log_file,maxBytes=log_max_size_in_bytes,backupCount=log_backup_count)handler.name='oasislmf'logging.getLogger('oasislmf').setLevel(log_level)logging.getLogger('oasislmf').addHandler(handler)formatter=logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")handler.setFormatter(formatter)
[docs]defoasis_log(*args,**kwargs):""" Decorator that logs the entry, exit and execution time. """logger=logging.getLogger('oasislmf')defactual_oasis_log(func):@wraps(func)defwrapper(*args,**kwargs):func_name=func.__name__caller_module_name=func.__globals__.get('__name__')iffunc_name=='__init__':logger.debug("RUNNING: {}.{}".format(caller_module_name,func_name))else:logger.info("RUNNING: {}.{}".format(caller_module_name,func_name))args_name=getargspec(func)[0]args_dict=dict(zip(args_name,args))iflogger.level<=logging.DEBUG:forkey,valueinargs_dict.items():ifkey=="self":continuelogger.debug(" {} == {}".format(key,value))iflen(args)>len(args_name):foriinrange(len(args_name),len(args)):logger.debug(" {}".format(args[i]))forkey,valueinkwargs.items():logger.debug(" {} == {}".format(key,value))start=time.time()result=func(*args,**kwargs)end=time.time()# Only log timestamps on functions which took longer than 10msif(end-start)>0.01:logger.info("COMPLETED: {}.{} in {}s".format(caller_module_name,func_name,round(end-start,2)))else:logger.debug("COMPLETED: {}.{} in {}s".format(caller_module_name,func_name,round(end-start,2)))returnresultreturnwrapperiflen(args)==1andcallable(args[0]):returnactual_oasis_log(args[0])else:returnactual_oasis_log