import os
import time
import logging
from logging.handlers import RotatingFileHandler
dir = os.path.dirname(__file__)
dir_time = time.strftime('%Y-%m-%d', time.localtime())
handlers = {
logging.DEBUG: os.path.join(dir, 'debug.log'),
logging.INFO: os.path.join(dir, 'info.log'),
logging.WARNING: os.path.join(dir, 'warning.log'),
logging.ERROR: os.path.join(dir, 'error.log'),
}
def createHandlers():
logLevels = handlers.keys()
for level in logLevels:
path = os.path.abspath(handlers[level])
handlers[level] = RotatingFileHandler(path, maxBytes=102400, backupCount=999, encoding='utf-8')
createHandlers()
class TNLog(object):
def printfNow(self):
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
def __init__(self, level=logging.NOTSET):
self.__loggers = {}
logLevels = handlers.keys()
for level in logLevels:
logger = logging.getLogger(str(level))
logger.addHandler(handlers[level])
logger.setLevel(level)
self.__loggers.update({level: logger})
def getLogMessage(self, level, message):
''' 日志格式:[日志级别] 信息 '''
return "[%s] - [%s]:%s" % (self.printfNow(), level, message)
def info(self, message):
message = self.getLogMessage("info", message)
self.__loggers[logging.INFO].info(message)
def error(self, message):
message = self.getLogMessage("error", message)
self.__loggers[logging.ERROR].error(message)
def warning(self, message):
message = self.getLogMessage("warning", message)
self.__loggers[logging.WARNING].warning(message)
def debug(self, message):
message = self.getLogMessage("debug", message)
self.__loggers[logging.DEBUG].debug(message)
def critical(self, message):
message = self.getLogMessage("critical", message)
self.__loggers[logging.CRITICAL].critical(message)