python可以分开文件记录日志的类

#!/usr/bin/python
#coding=utf-8

import logging
import sys
import os

class Logger:
    def __init__(self, logName, logFile):
        self._logger = logging.getLogger(logName)

        log_dir = "./log"
        if os.path.exists(log_dir) == False:
            os.mkdir(log_dir)

        log_filename = log_dir+'/'+logFile

        handler = logging.FileHandler(log_filename)
        formatter = logging.Formatter('%(asctime)s %(funcName)s [line:%(lineno)d]  %(levelno)s %(levelname)s  threadID:%(thread)d threadName:%(threadName)s msg:%(message)s')
        handler.setFormatter(formatter)
        self._logger.addHandler(handler)
        self._logger.setLevel(logging.INFO)

    def info(self, msg):
        if self._logger is not None:
            self._logger.info(msg)



这个类的构造函数,logName是日志对象名称,logFile是日志文件名称。

而且文件是记录在当前目录的LOG文件夹下的。


备注:如果有需要,logFile是一个全路径。

你可能感兴趣的:(Python)