Skip to content

Instantly share code, notes, and snippets.

@m2kar
Created March 5, 2020 05:46
Show Gist options
  • Save m2kar/432205757656148e9230cb0264126568 to your computer and use it in GitHub Desktop.
Save m2kar/432205757656148e9230cb0264126568 to your computer and use it in GitHub Desktop.
log配置
import logging
import const
#DEBUG级别的错误,输出到 const.LOGFILE
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=const.LOGFILE,
                    filemode='a')
#################################################################################################
# 定义一个StreamHandler,将INFO级别或更高的日志信息打印到标准错误,并将其添加到当前的日志处理对象#
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s: %(levelname)-5s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
#定义一个INFO级别的错误 输出到 const.INFOLOGFILE
infolog = logging.FileHandler(const.INFOLOGFILE)
infolog.setLevel(logging.INFO)
errformatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
infolog.setFormatter(errformatter)
logging.getLogger('').addHandler(infolog)
#定义一个ERROR级别的错误,输出到const.ERRLOGFILE
errlog = logging.FileHandler(const.ERRLOGFILE)
errlog.setLevel(logging.WARNING)
errformatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
errlog.setFormatter(errformatter)
logging.getLogger('').addHandler(errlog)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment