Python #logging

###


#-*- coding:utf-8 -*-
import logging
import logging.handlers
#日志目录
LOG_FILE ='log.zk'
log_fmt='%(asctime)s - %(filename)s - %(levelname)s - %(message)s'
date_fmt='%a, %d %b %Y %H:%M:%S'
#实例化formatter,datefmt的格式是使用yime.strftime(format[, t])的格式
formatter=logging.Formatter(fmt=log_fmt,datefmt=date_fmt)
# Thu, 21 Sep 2017 10:46:29 - LOG.py - ERROR - The ERROR MESSAGE
#创建一个Handler处理器
handler = logging.handlers.RotatingFileHandler(LOG_FILE)
# 为handler添加日志格式
handler.setFormatter(formatter)
#创建一个logger实例
logger = logging.getLogger('log.zk')
#设置日志级别
logger.setLevel(logging.ERROR)
#添加一个处理器
logger.addHandler(handler)

logger.debug('The DEBUG MESSAGE')
logger.info('The INFO MESSAGE')
logger.warning('The WARN MESSAGE')
logger.error('The ERROR MESSAGE')
logger.critical('The critical MESSAGE')

###


#-*- coding:utf-8 -*-
import logging
import logging.handlers
# filename:日志名称,level 日志等级(输出INFO以上的日志包括INFO)
logging.basicConfig(filename='log.kafka',level=logging.INFO,format='%(asctime)s - %(filename)s - %(levelname)s - %(message)s')
logging.debug('The DEBUG MESSAGE')
logging.info('The INFO MESSAGE')
logging.warning('The WARN MESSAGE')
logging.error('The ERROR MESSAGE')
logging.critical('The critical MESSAGE')

###

参考博客:

http://blog.csdn.net/zyz511919766/article/details/25136485

原文地址:https://www.cnblogs.com/lwsup/p/7567519.html