【Python】公共类-logger

# -*- 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')

猜你喜欢

转载自www.cnblogs.com/haohaozhang/p/11363363.html