背景
仅输出到文件中
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='myapp.txt',
filemode='w')
logging.debug("这是一个debug")
同时输出到终端和文件中
basic_format = '%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s'
date_format = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(format=basic_format, datefmt=date_format, level=logging.DEBUG)
# 使用FileHandler输出到文件
fh = logging.FileHandler("outout.log")
formatter = logging.Formatter(basic_format, date_format)
fh.setFormatter(formatter)
fh.setLevel(logging.DEBUG)
logger = logging.getLogger() # 不加名称设置root logger
logger.addHandler(fh)