为不同设置配置不同日志文件位置的 DRY 方法是什么?

发布于 2024-07-26 22:03:23 字数 867 浏览 6 评论 0原文

我在 django 项目中使用 python 的 logging 模块。 我正在我的 settings.py 文件中执行基本日志记录配置。 像这样的事情:

import logging   
import logging.handlers
logger = logging.getLogger('project_logger')
logger.setLevel(logging.INFO)

LOG_FILENAME = '/path/to/log/file/in/development/environment'
handler = logging.handlers.TimedRotatingFileHandler(LOG_FILENAME, when = 'midnight')
formatter = logging.Formatter(LOG_MSG_FORMAT)
handler.setFormatter(formatter)
logger.addHandler(handler)

我有一个单独的生产设置文件。 此文件 (production.py) 导入 settings 中的所有内容并覆盖一些选项(将 DEBUG 设置为 False , 例如)。 我希望使用不同的 LOG_FILENAME 进行生产。 我该怎么办? 我可以重复 Production.py 中的整个配置部分,但是如果生产中不存在 /path/to/log/file/in/development/environment ,则会产生问题机器。 此外,它看起来不太“干”。

谁能建议一个更好的方法来解决这个问题?

I am using python's logging module in a django project. I am performing the basic logging configuration in my settings.py file. Something like this:

import logging   
import logging.handlers
logger = logging.getLogger('project_logger')
logger.setLevel(logging.INFO)

LOG_FILENAME = '/path/to/log/file/in/development/environment'
handler = logging.handlers.TimedRotatingFileHandler(LOG_FILENAME, when = 'midnight')
formatter = logging.Formatter(LOG_MSG_FORMAT)
handler.setFormatter(formatter)
logger.addHandler(handler)

I have a separate settings file for production. This file (production.py) imports everything from settings and overrides some of the options (set DEBUG to False, for instance). I wish to use a different LOG_FILENAME for production. How should I go about it? I can repeat the entire configuration section in production.py but that creates problems if /path/to/log/file/in/development/environment is not present in the production machine. Besides it doesn't look too "DRY".

Can anyone suggest a better way to go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2024-08-02 22:03:24

为什么不把这条语句放在settings.py的末尾,并使用DEBUG flal es指示器进行开发呢?

像这样的事情:

import logging   
import logging.handlers
logger = logging.getLogger('project_logger')
logger.setLevel(logging.INFO)

[snip]
if DEBUG:
    LOG_FILENAME = '/path/to/log/file/in/development/environment'
else:
    LOG_FILENAME = '/path/to/log/file/in/production/environment'

handler = logging.handlers.TimedRotatingFileHandler(LOG_FILENAME, when = 'midnight')
formatter = logging.Formatter(LOG_MSG_FORMAT)
handler.setFormatter(formatter)
logger.addHandler(handler)

Why don't you put this statements at the end of settings.py and use the DEBUG flal es indicator for developement?

Something like this:

import logging   
import logging.handlers
logger = logging.getLogger('project_logger')
logger.setLevel(logging.INFO)

[snip]
if DEBUG:
    LOG_FILENAME = '/path/to/log/file/in/development/environment'
else:
    LOG_FILENAME = '/path/to/log/file/in/production/environment'

handler = logging.handlers.TimedRotatingFileHandler(LOG_FILENAME, when = 'midnight')
formatter = logging.Formatter(LOG_MSG_FORMAT)
handler.setFormatter(formatter)
logger.addHandler(handler)
朮生 2024-08-02 22:03:24

找到了一个相当有效的“DRY”解决方案。 感谢 Django 中的 Python 日志记录

我现在有了一个 log.py ,它看起来像什么像这样:

import logging, logging.handlers
from django.conf import settings

LOGGING_INITIATED = False
LOGGER_NAME = 'project_logger'

def init_logging():
    logger = logging.getLogger(LOGGER_NAME)
    logger.setLevel(logging.INFO)
    handler = logging.handlers.TimedRotatingFileHandler(settings.LOG_FILENAME, when = 'midnight')
    formatter = logging.Formatter(LOG_MSG_FORMAT)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

if not LOGGING_INITIATED:
    LOGGING_INITIATED = True
    init_logging()

我的 settings.py 现在包含

LOG_FILENAME = '/path/to/log/file/in/development/environment

并且 product.py 包含:

from settings import *
LOG_FILENAME = '/path/to/log/file/in/production/environment'

Found a reasonably "DRY" solution that worked. Thanks to Python logging in Django

I now have a log.py which looks something like this:

import logging, logging.handlers
from django.conf import settings

LOGGING_INITIATED = False
LOGGER_NAME = 'project_logger'

def init_logging():
    logger = logging.getLogger(LOGGER_NAME)
    logger.setLevel(logging.INFO)
    handler = logging.handlers.TimedRotatingFileHandler(settings.LOG_FILENAME, when = 'midnight')
    formatter = logging.Formatter(LOG_MSG_FORMAT)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

if not LOGGING_INITIATED:
    LOGGING_INITIATED = True
    init_logging()

My settings.py now contains

LOG_FILENAME = '/path/to/log/file/in/development/environment

and production.py contains:

from settings import *
LOG_FILENAME = '/path/to/log/file/in/production/environment'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文