首页 > 编程语言 > 详细

【Python】公共类-logger

时间:2020-01-26 18:31:53      阅读:62      评论:0      收藏:0      [点我收藏+]
# -*- coding: utf-8 -*-
__author__ = zhangh

import logging

class Logging(object):
    def __init__(self, path):
        self.path = path

    def log(self):
        log_format = logging.Formatter("%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s")

        logger         = logging.getLogger()

        logging_file   = logging.FileHandler(self.path)
        logging_file.setFormatter(log_format)
        logging_stream = logging.StreamHandler()
        logging_stream.setFormatter(log_format)

        logger.addHandler(logging_file)
        logger.addHandler(logging_stream)
        logger.setLevel("DEBUG")

        return logger

# logger = Logging(‘/root/Desktop/test.log‘).log()
# logger.info(‘info message‘)
# logger.warning(‘warning message‘)
# logger.error(‘warning message‘)
# logger.debug(‘debug message‘)
# logger.critical(‘critical message‘)

 

【Python】公共类-logger

原文:https://www.cnblogs.com/haohaozhang/p/11363363.html

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