如何使用python自带的 logging 模块实现日志功能
import os
import logging
import sys
LOG_PATH = 'logs' #设置log路径
LOG_FILE = 'text.txt' #设置log文件名
#设置根路径为起始位置
logger = logging.getLogger(__name__)
#生成log指定路径
if os.path.exists(LOG_PATH):
pass
else:
os.mkdir(LOG_PATH)
formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s')
file_handler = logging.FileHandler("%s/%s" % (LOG_PATH, LOG_FILE))
# 可以通过setFormatter指定输出格式
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
分为如下几个级别:
_nameToLevel = {
'CRITICAL': CRITICAL,
'FATAL': FATAL,
'ERROR': ERROR,
'WARN': WARNING,
'WARNING': WARNING,
'INFO': INFO,
'DEBUG': DEBUG,
'NOTSET': NOTSET,
}
使用setLevel函数指定输出级别 指定之后将只显示级别以上的日志类型
logger.setLevel(logging.DEBUG)
#输出debug类型日志
logger.debug("debug")
#多参数传递 输入info类型日志
pam= "dshck"
pam2="cxjkdhc"
logger.info("%s%s"%(pam,pam2))
log.py 代码如下
import os
import logging
from logging.handlers import TimedRotatingFileHandler
import datetime
import json
LOG_PATH = "log"
LOG_INFO = '_info.log'
LOG_ERROR = '_error.log'
class logger:
def __init__(self,prefix_name = "flask"):
if os.path.exists(LOG_PATH):
pass
else:
os.mkdir(LOG_PATH)
self.prefix = prefix_name
self.info_logger = logging.getLogger("info")
self.error_logger = logging.getLogger("error")
self.format = logging.Formatter('[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s]\
[%(filename)s:%(lineno)d]' '[%(levelname)s] : %(message)s')
#指定文件位置文件名以及输出格式
info_file_handler = logging.FileHandler("%s/%s%s" % (LOG_PATH, prefix_name,LOG_INFO))
info_file_handler.setFormatter(self.format)
error_file_handler = logging.FileHandler("%s/%s%s" % (LOG_PATH, prefix_name,LOG_ERROR))
error_file_handler.setFormatter(self.format)
self.info_logger.addHandler(info_file_handler)
self.error_logger.addHandler(error_file_handler)
# 指定日志的最低输出级别
self.info_logger.setLevel(logging.NOTSET)
self.error_logger.setLevel(logging.ERROR)
def debug(self, msg, *args, **kwargs):
self.info_logger.debug(msg, *args, **kwargs)
def info(self, msg, *args, **kwargs):
self.info_logger.info(msg, *args, **kwargs)
def warn(self, msg, *args, **kwargs):
self.info_logger.warning(msg, *args, **kwargs)
def warning(self, msg, *args, **kwargs):
self.info_logger.warning(msg, *args, **kwargs)
def error(self, msg, *args, **kwargs):
self.error_logger.error(msg, *args, **kwargs)
def fatal(self, msg, *args, **kwargs):
self.error_logger.fatal(msg, *args, **kwargs)
def critical(self, msg, *args, **kwargs):
self.error_logger.critical(msg, *args, **kwargs)
# log =logger()
# log.info("jdshskh")
# log.error("hdskck")
# log.debug("1122debug")
# log.warn("warn")
# log.warning("warning")
# log.critical("critical")
# log.fatal("fatal")
log =logger("celery")
log.info("jdshskh")
log.error("hdskck")
log.debug("1122debug")
log.warn("warn")
log.warning("warning")
log.critical("critical")
log.fatal("fatal")