import logging
from logging.handlers import RotatingFileHandler
class Log():
"""对日志输出功能进行封装"""
def __init__(self, filename):
"""初始化日志格式"""
self.filename = filename + ".log"
self.LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
self.DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
def log(self):
# 配置日志超过指定大小删除
rfh = RotatingFileHandler(
filename=self.filename,
maxBytes=1024*1024*5,
backupCount=1)
# 配置日志格式
logging.basicConfig(
format=self.LOG_FORMAT,
datefmt=self.DATE_FORMAT,
level=logging.INFO, # 日志最低严重级别
handlers=[rfh]
)
return logging
if __name__ == "__main__":
log = Log('test').log()
log.debug('TEST, 42')