logging使用
import logging
import os
from logging import handlers
from constants.constants import Constants
class LogHandlerC:
"""
日志封装类
"""
def __init__(self, name="root", level="DEBUG", file=None,
format="'%(asctime)s-%(filename)s:%(lineno)d:%(name)s:%(levelname)s:%(message)s"):
# 创建一个logger
logger = logging.getLogger(name)
# 设置收集器级别
logger.setLevel(level)
# 初始化format 设置格式
formatter = logging.Formatter(format)
# 控制输出到控制台日志格式、级别
handler_console = logging.StreamHandler()
handler_console.setFormatter(formatter)
# 日志保留10天,一天保存一个文件
if file:
file_handler = handlers.TimedRotatingFileHandler(file, 'D', 1, 10)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
# 添加到logger 的 handler
logger.addHandler(handler_console)
# 赋值
self.logger = logger
# log = LogHandlerC(file=Constants.LOG_PATH).logger
log = LogHandlerC().logger
if __name__ == '__main__':
cwd = os.getcwd()
log.debug(cwd)
log.debug("你好!")
使用
from log.logHandler import *
if __name__ == '__main__':
log.debug("---------------------log")