首页 > 编程语言 > 详细

python logging 工具

时间:2017-12-25 15:33:12      阅读:222      评论:0      收藏:0      [点我收藏+]

使用 python logging记录debug 等日志信息

debug 以上信息写入文件

info 以上信息输出在控制台

import os
import logging

VDE_LOGGING_NAME = "vde_logging"
LOG_FILE_PATH = os.path.join(os.path.dirname(__file__), "vde_regression.log")


def logger_initialization():
    # 1. logging
    logger = logging.getLogger(VDE_LOGGING_NAME)
    logger.setLevel(logging.DEBUG)
    # 2.handler
    # file handler
    fh = logging.FileHandler(LOG_FILE_PATH, mode="w")
    fh.setLevel(logging.DEBUG)
    # standard control console
    ch = logging.StreamHandler()
    ch.setLevel(logging.INFO)
    # 3.format
    formatter = logging.Formatter("[%(asctime)s %(name)s].%(levelname)s: %(message)s")
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)

    logger.addHandler(fh)
    logger.addHandler(ch)


def debug_logging_recoder(debug_msg):
    vde_logging = logging.getLogger(VDE_LOGGING_NAME)
    vde_logging.debug(debug_msg)


def info_logging_recoder(info_msg):
    vde_logging = logging.getLogger(VDE_LOGGING_NAME)
    vde_logging.info(info_msg)


if __name__ == ‘__main__‘:
    logger_initialization()
    debug_logging_recoder("debug")
    info_logging_recoder("info")

  References:

http://www.zlovezl.cn/articles/replacing-print-simple-introduction-to-logging/

https://blog.igevin.info/posts/python-log/

python logging 工具

原文:http://www.cnblogs.com/dasheng-maritime/p/8109560.html

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