python 日志模块工具类

2020-01-19 10:34:46 浏览数 (1)

代码语言:javascript复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging


# logName 日志中的某个格式化的字段名,logFile生成的日志文件名
def getlogger(logName, logFile):

    logger=logging.getLogger(logName)
    logger.setLevel(logging.DEBUG)

    screenHandle = logging.StreamHandler()
    screenHandle.setLevel(logging.DEBUG)


    fileHandle = logging.FileHandler(logFile,'a')
    fileHandle.setLevel(logging.DEBUG)

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    screenHandle.setFormatter(formatter)
    fileHandle.setFormatter(formatter)

    logger.addHandler(fileHandle)
    logger.addHandler(screenHandle)

    return logger

0 人点赞