首页 > 编程语言 > 详细

python logging模块

时间:2019-09-06 14:06:33      阅读:70      评论:0      收藏:0      [点我收藏+]
import logging

# 在文件和窗口二选一个位置打印信息,有filename的话在写入文件
logging.basicConfig(
    level=logging.DEBUG,  # 设置错误级别
    filename=logging.txt,  # 日志文件名
    filemode=w,  # 文件打开模式
    format=%(asctime)s %(filename)s [%(lineno)d] %(message)s
)

logging.debug("this is a debug")
logging.info(this is info)
logging.warning(this is warning)
logging.error(this is error)
logging.critical(this is critical)


# -------------------------------------------------
# 文件和窗口同时打印错误信息
def logger():
    logger_obj = logging.getLogger()
    fh = logging.FileHandler(logging_text)
    ch = logging.StreamHandler()

    fm = logging.Formatter(%(asctime)s %(filename)s [%(lineno)d] %(message)s)

    fh.setFormatter(fm)
    ch.setFormatter(fm)

    logger_obj.addHandler(fh)
    logger_obj.addHandler(ch)

    logger_obj.setLevel(DEBUG)
    return logger_obj


logger = logger()
logging.debug("this is a debug")
logging.info(this is info)
logging.warning(this is warning)
logging.error(this is error)
logging.critical(this is critical)

 

python logging模块

原文:https://www.cnblogs.com/kehaimin/p/11474160.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!