您的当前位置:首页正文

python logging模块打印到文件日志-重复数据的解决

2024-11-28 来源:个人技术集锦

import logging

def getMyLogger():
    logger = logging.getLogger('aaasgdssdfgaefag')
    if not logger.handlers:
        logger.setLevel(logging.DEBUG)
        # create file handler which logs even debug messages
        fh = logging.FileHandler('/Users/fangyuanyuan/Downloads/check_insert_enum.log')
        fh.setLevel(logging.DEBUG)
        # create formatter and add it to the handlers
        formatter = logging.Formatter('%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s')
        fh.setFormatter(formatter)
        # add the handlers to logger
        logger.addHandler(fh)
    return logger
    
if  __name__ == '__main__':

    logger = getMyLogger()
    # 'application' code
    logger.debug('debug message=-----111')
    logger.info('info message=-----111')
    
显示全文