import logging

def loggin_config():
'''
logging.basicConfig函数各参数:

filename:指定日志文件名;
filemode:和file函数意义相同,指定日志文件的打开模式,'w'或者'a';
format:指定输出的格式和内容,format可以输出很多有用的信息,
参数:作用
%(levelno)s:打印日志级别的数值
%(levelname)s:打印日志级别的名称
%(pathname)s:打印当前执行程序的路径,其实就是sys.argv[0]
%(filename)s:打印当前执行程序名
%(funcName)s:打印日志的当前函数
%(lineno)d:打印日志的当前行号
%(asctime)s:打印日志的时间
%(thread)d:打印线程ID
%(threadName)s:打印线程名称
%(process)d:打印进程ID
%(message)s:打印日志信息
datefmt:指定时间格式,同time.strftime();

level:设置日志级别,默认为logging.WARNNING;
从低到高:debug、info、warning、error以及critical
stream:指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略;

'''
# 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.log',
# filemode='w')
logger = logging.getLogger()
# 设置日志输出等级
logger.setLevel(logging.DEBUG)
# 创建输出格式
formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s %(message)s')
# 输出到文件
fh = logging.FileHandler('test_log',encoding='utf8')
fh.setFormatter(formatter)
logger.addHandler(fh)

# 输出到控制台
sh = logging.StreamHandler()
sh.setFormatter(formatter)
logger.addHandler(sh)

logger.debug("debug测试")
logger.info("info")
logger.warning("warning")
logger.error("error")
logger.critical("critical")
if __name__ == '__main__':
loggin_config()
posted on 2019-07-02 17:38  天0涯  阅读(624)  评论(0编辑  收藏  举报