python记录日志
配置logger文件,记录日志
-
首先导入依赖包
import logging #日志 import time # 时间 import os # 路径
-
创建一个logger
logger = logging.getLogger() #获取logger logger.setLevel(logging.DEBUG) # 设置记录等级时debug
-
创建日志文件
rq = time.strftime('%Y-%m-%d-%H', time.localtime(time.time())) #以小时分割记录日志 log_path = os.getcwd() + '/../Logs/all/' #设置日志路径 log_name = log_path + rq + '_all.log' #设置日志名称 logfile = log_name if not os.path.exists(log_path): # 创建路径 os.makedirs(log_path) for root, dirs, files in os.walk(os.path.dirname(log_path)): # 删除空文件 for i in files: fpath = os.path.join(root, i) if os.path.getsize(fpath) == 0: os.remove(fpath) if not os.path.exists(logfile): # 创建文件 f = open(logfile, mode='w', encoding="utf-8") f.close() fh_all = logging.FileHandler(logfile, mode='a', encoding='utf-8') # 输出到文件 fh_all.setLevel(logging.DEBUG) # 以上是所有日志,其他分类日志同上 ch = logging.StreamHandler() ch.setLevel(logging.WARNING) # 控制台输出的日志级别
-
定义输出格式
formatter = logging.Formatter( "%(asctime)s - %(filename)s[line:%(lineno)d](%(funcName)s) - %(levelname)s: %(message)s") fh_debug.setFormatter(formatter) # 添加格式 logger.addHandler(fh_debug) # 保存 ch.setFormatter(formatter) logger.addHandler(ch)
完整代码
import logging
import os
import time
logger = logging.getLogger()
logger.setLevel(logging.DEBUG) # Log等级总开关
# 第二步,创建一个 file handler,用于写入日志文件
def log(func):
@functools.wraps(func)
def wrapper(*args, **kw):
dicc = {}
dinp = {}
varnames = func.__code__.co_varnames
deft = func.__defaults__
if deft is None:
deft = ()
for i in range(len(args)):
dinp[varnames[i]] = str(args[i])
for j in range(len(deft)):
dinp[varnames[i + j + 1]] = str(deft[j])
for i, j in kw.items():
dinp[i] = str(j)
# print(str(func.__name__))
filter = ContextFilter(
os.path.basename(str(func.__code__.co_filename)), int(func.__code__.co_firstlineno), str(func.__name__))
try:
aa = func(*args, **kw)
except Exception as e:
aa = 'err:' + str(e)
if aa is None:
dretrun = ''
elif isinstance(aa, str):
dretrun = aa
elif isinstance(aa, tuple):
dretrun = list(aa)
else:
dretrun = str(aa)
# dicc['run_info'] = dinfo
dicc['run_input'] = dinp
dicc['run_return'] = dretrun
logger.addFilter(filter)
logger.debug(dicc)
logger.error(func.__name__ + '运行错误:', exc_info=True)
logger.removeFilter(filter)
raise e
if aa is None:
dretrun = ''
elif isinstance(aa, str):
dretrun = aa
elif isinstance(aa, tuple):
dretrun = list(aa)
else:
dretrun = str(aa)
# dicc['run_info'] = dinfo
dicc['run_input'] = dinp
dicc['run_return'] = dretrun
logger.addFilter(filter)
logger.debug(dicc)
logger.removeFilter(filter)
return aa
return wrapper
-
使用logger记录日志
logger.error('err', exc_info=True) #exc_info 记录错误信息
使用@log装饰器记录log
-
导入依赖包
import functools
-
完整代码
def log(func): @functools.wraps(func) def wrapper(*args, **kw): dicc = {} dinp = {} varnames = func.__code__.co_varnames deft = func.__defaults__ if deft is None: deft = () for i in range(len(args)): dinp[varnames[i]] = str(args[i]) for j in range(len(deft)): dinp[varnames[i + j + 1]] = str(deft[j]) for i, j in kw.items(): dinp[i] = str(j) # print(str(func.__name__)) filter = ContextFilter( os.path.basename(str(func.__code__.co_filename)), int(func.__code__.co_firstlineno), str(func.__name__)) try: aa = func(*args, **kw) except Exception as e: aa = 'err:' + str(e) if aa is None: dretrun = '' elif isinstance(aa, str): dretrun = aa elif isinstance(aa, tuple): dretrun = list(aa) else: dretrun = str(aa) # dicc['run_info'] = dinfo dicc['run_input'] = dinp dicc['run_return'] = dretrun logger.addFilter(filter) logger.debug(dicc) logger.error(func.__name__ + '运行错误:', exc_info=True) logger.removeFilter(filter) raise e if aa is None: dretrun = '' elif isinstance(aa, str): dretrun = aa elif isinstance(aa, tuple): dretrun = list(aa) else: dretrun = str(aa) # dicc['run_info'] = dinfo dicc['run_input'] = dinp dicc['run_return'] = dretrun logger.addFilter(filter) logger.debug(dicc) logger.removeFilter(filter) return aa return wrapper
-
发现输出文件格式不是喜欢的类型只是记录,修改,在最上方添加方法
class ContextFilter(logging.Filter): # filename = 'IP' # lineno = 'USER' def __init__(self, filename, lineno, funcname): self.filename = filename self.lineno = lineno self.funcname = funcname def filter(self, record): record.filename = self.filename record.lineno = self.lineno record.funcName = self.funcname return True