python3 logging 日志记录模块

时间:2021-05-19 17:44:06
#coding:utf-8

import logging
logging.basicConfig(filename='log1.log',
format='%(asctime)s -%(name)s-%(levelname)s-%(module)s:%(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',
level=logging.DEBUG) while True:
option = input("input a digit:")
if option.isdigit():
print("hehe",option)
logging.info('option correct')
else:
logging.error("Must input a digit!") # logging.debug('有bug')
# logging.info('有新的信息')
# logging.warning('警告信息')
# logging.error('错误信息')
# logging.critical('紧急错误信息')
# logging.log(10,'log')
#coding:utf-8
import logging logger = logging.getLogger("simple_example")
logger.setLevel(logging.DEBUG) #输出到屏幕
ch = logging.StreamHandler()
ch.setLevel(logging.WARNING)
#输出到文件
fh = logging.FileHandler("log2.log")
fh.setLevel(logging.INFO)
#设置日志格式
fomatter = logging.Formatter('%(asctime)s -%(name)s-%(levelname)s-%(module)s:%(message)s')
ch.setFormatter(fomatter)
fh.setFormatter(fomatter)
logger.addHandler(ch)
logger.addHandler(fh) logger.debug("debug message")
logger.info("info message")
logger.warning("warning message")
logger.error("error message")
logger.critical("critical message")