伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

Python_学习之项目日志管理

来源:本站原创 浏览:143次 时间:2022-02-27

作为一个开发者,日志的作用相信大家对它的重要性,是毋庸置疑的,它是你代码优化及bug修复的最佳助手,也是"责任"的最佳证据,如果你是初学者或刚入职场,代码的编写还不够老练,没有关系,但日志记得一定一定要记得保留,最基本的原则就是,人过留声【谁访问你,什么时候,传了什么,来干嘛,你什么时候响应的,响应了什么】,重要的事情重复说,记日志,记日志,记日志!
废话不多说,直接干货送上,你要做的就是copy!
以下是前篇文章<<Python_学习之项目目录结构构建>>中说的日志配置log_config.py,本日志配置适合python任何项目。

#!/usr/bin/env python# -*- coding:utf-8 -*-"""本地记录日志配置文件"""import osimport platformBASEDIR = os.path.dirname(    os.path.dirname(        os.path.dirname(            os.path.abspath(__file__))))# 本地项目日志路径CURRENT_LOG_PATH = os.path.join(BASEDIR, 'log')PREFIX = CURRENT_LOG_PATH if platform.system() != 'Windows' else '/opt/logs/python_apps_logs/{}'.format(os.path.basename(BASEDIR))# 判断目录是否存在,若不存在则创建if not os.path.exists(PREFIX):    os.makedirs(PREFIX)# 日志文件路径LOG_PATH_DEBUG = r'%s\debug.log' % PREFIX if platform.system() == 'Windows' else '%s/debug.%s.log' % (PREFIX, os.getpid())LOG_PATH_INFO = r'%s\info.log' % PREFIX if platform.system() == 'Windows' else '%s/info.%s.log' % (PREFIX, os.getpid())LOG_PATH_WARN = r'%s\warn.log' % PREFIX if platform.system() == 'Windows' else '%s/warn.%s.log' % (PREFIX, os.getpid())LOG_PATH_ERROR = r'%s\error.log' % PREFIX if platform.system() == 'Windows' else '%s/error.%s.log' % (PREFIX, os.getpid())# 日志配置LOGGING_CONFIG = {    'version': 1,    'disable_existing_loggers': True,    'formatters'�ٴ�,���: {        'standard': {            'format': '[%(asctime)s] %(levelname)s::(%(process)d %(thread)d)::%(module)s[line:%(lineno)d] - %(message)s'        },    },    'handlers': {        'error': {            'class': 'logging.handlers.TimedRotatingFileHandler',            'level': 'ERROR',            'formatter': 'standard',            'filename': LOG_PATH_ERROR + '_file',            'when': 'H',            'interval': 1        },        'warn': {            'class': 'logging.handlers.TimedRotatingFileHandler',            'level': 'WARN',            'formatter': 'standard',            'filename': LOG_PATH_WARN + '_file',            'when': 'H',            'interval': 1        },        'info': {            'class': 'logging.handlers.TimedRotatingFileHandler',            'level': 'INFO',            'formatter': 'standard',            'filename': LOG_PATH_INFO + '_file',            'when': 'H',            'interval': 1        },        'debug': {            'class': 'logging.handlers.TimedRotatingFileHandler',            'level': 'DEBUG',            'formatter': 'standard',            'filename': LOG_PATH_DEBUG + '_file',            'when': 'H',            'interval': 1        }    },    'loggers': {        'default': {            'handlers': ['debug', 'info', 'warn', 'error'],            'level': 'DEBUG',            'propagate': True        }    }}

为了更加的通用性,如果参照我的写法,建议目录结构如下图:

在log_conf包中的init.py文件中,构建logger对象,如下:

#!/usr/bin/env python

-- coding:utf-8 --

import logging.config
from log_conf import log_config

logging.config.dictConfig(log_config.LOGGING_CONFIG)
logger = logging.getLogger('default')

这样一个完整的日志管理就完成了,剩下的就是用了,它的方法非常简单。

# 使用配置好的log_configfrom log_conf import logger# 能力接口def test(request_data):    code, desc, result = 0, 'success', None    logger.debug("请求参数为:%s" % request_data)    try:        """do something"""        pass    except Exception as e:        logger.error("xx能力接口发生异常,异常原因为:%s" % e.args)        code, desc = 1, "系统异常"    finally:        return code, desc, result

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net