给blog添加log日志
Posted wangkaiok
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了给blog添加log日志相关的知识,希望对你有一定的参考价值。
bin/start.py
# import sys import sys import os # sys.path.append(r‘F:pythonpython练习day-07log‘) # from core import src # src.run() # print(os.path.dirname(__file__)) # print(os.path.dirname(os.path.dirname(__file__))) #动态获取你的项目目录(你所在的目录下的上级目录) # print(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) #动态获取你的上一级目录的上一级 BASE_PATH = os.path.dirname(os.path.dirname(__file__)) sys.path.append(BASE_PATH) from core import src if __name__ == ‘__main__‘: src.run()
conf/settings.py
DB_PATH = r‘F:pythonpython练习day-08logdb egister‘ LOG_PATH = r‘F:pythonpython练习day-08loglogaccess.log‘ """ logging配置 """ import os import logging.config # 定义三种日志输出格式 开始 standard_format = ‘[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]‘ ‘[%(levelname)s][%(message)s]‘ #其中name为getlogger指定的名字 simple_format = ‘[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s‘ id_simple_format = ‘[%(levelname)s][%(asctime)s] %(message)s‘ # 定义日志输出格式 结束 logfile_dir = os.path.dirname(os.path.abspath(__file__)) # log文件的目录 logfile_name = ‘高配版.log‘ # log文件名 # 如果不存在定义的日志目录就创建一个 # if not os.path.isdir(logfile_dir): # os.mkdir(logfile_dir) # log文件的全路径 logfile_path = os.path.join(logfile_dir, logfile_name) # log配置字典 LOGGING_DIC = { ‘version‘: 1, ‘disable_existing_loggers‘: False, ‘formatters‘: { ‘standard‘: { ‘format‘: standard_format }, ‘simple‘: { ‘format‘: simple_format }, }, ‘filters‘: {}, ‘handlers‘: { #打印到终端的日志 ‘console‘: { ‘level‘: ‘DEBUG‘, ‘class‘: ‘logging.StreamHandler‘, # 打印到屏幕 ‘formatter‘: ‘simple‘ }, #打印到文件的日志,收集info及以上的日志 ‘default‘: { ‘level‘: ‘DEBUG‘, ‘class‘: ‘logging.handlers.RotatingFileHandler‘, # 保存到文件 ‘formatter‘: ‘standard‘, ‘filename‘: LOG_PATH, # 日志文件 ‘maxBytes‘: 1000, # 日志大小 5M ‘backupCount‘: 5, ‘encoding‘: ‘utf-8‘, # 日志文件的编码,再也不用担心中文log乱码了 }, }, ‘loggers‘: { #logging.getLogger(__name__)拿到的logger配置 ‘‘: { ‘handlers‘: [‘default‘, ‘console‘], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕 ‘level‘: ‘DEBUG‘, ‘propagate‘: True, # 向上(更高level的logger)传递 }, }, }
core/src.py
# from lib import logger from lib import commom from conf import settings log1 = commom.get_logger(‘购物车相关:‘) log2 = commom.get_logger(‘日记页面相关:‘) def register(): with open(settings.DB_PATH, encoding=‘utf-8‘, mode=‘a+‘) as f1: f1.write(‘太白金星|123 ‘) def login(): pass # @logger def comment(): print(‘欢迎访问评论页面‘) # @logger # @commom.get_logger def article(): print(‘欢迎访问文章页面‘) # @logger def diary(): log2.info(‘日记页面出现问题‘) print(‘日记:欢迎访问日记页面‘) def shopping(): log1.info(‘购物车:小李花了300买了一个娃娃...‘) # log1.error(‘支付失败。。‘) print(‘欢迎进入购物车‘) dic = { 1: register, 2: login, 3: comment, 4: article, 5: diary, 6: shopping, } def run(): while 1: print(‘‘‘ 1: register, 2: login, 3: comment, 4: article, 5: diary 6: shopping ‘‘‘) choice = input(‘请输入‘) if choice.isdigit(): choice = int(choice) dic[choice]() else: print(‘请重新输入‘)
lib/commom.py
from conf import settings import os import logging.config def logger(f): def inner(*args, **kwargs): with open(settings.LOG_PATH, encoding=‘utf-8‘, mode=‘a+‘) as f1: f1.write(‘您访问了%s‘ % (f.__name__)) ret = f(*args, **kwargs) return ret return inner def get_logger(taskname): logging.config.dictConfig(settings.LOGGING_DIC) # 导入上面定义的logging配置 logger = logging.getLogger(taskname) # 生成一个log实例 # logger.info(‘高配版开始运行了!‘) # 记录该文件的运行状态 return logger # if __name__ == ‘__main__‘: # load_my_logging_cfg()
执行结果:
执行顺序:
以上是关于给blog添加log日志的主要内容,如果未能解决你的问题,请参考以下文章