码迷,mamicode.com
首页 > 编程语言 > 详细

python logging 工具

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

标签:sts   记录   class   __file__   enc   handler   mod   logger   python   

使用 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 工具

标签:sts   记录   class   __file__   enc   handler   mod   logger   python   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!