Django Managemengt命令记录会加入消息

时间:2020-06-03 09:36:25

标签: python django logging

当我从Django管理命令发出日志语句时,它们始终会合并为一个大日志语句,以执行整个命令。相反,我希望像通常的应用程序日志一样逐行单独发布它们。这样一来,日志聚合就可以正确地将它们收集起来,而不会像一条极大的消息一样。

LOGGING_ROOT_HANDLERS = ['prometheus', 'console']

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'json': {
            '()': 'pythonjsonlogger.jsonlogger.JsonFormatter',
            'fmt': '%(levelname)s %(levelno)s %(pathname)s %(funcName)s %(asctime)s %(message)s',
        },
    },
    'filters': {
        'require_debug_true': {'()': 'django.utils.log.RequireDebugTrue'},
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'json',
        },
        'prometheus': {
            'level': 'WARNING',
            'class': 'app.logging_handlers.PrometheusHandler',
        },
    },
    'loggers': {
        '': {'level': 'INFO', 'handlers': LOGGING_ROOT_HANDLERS},
        'myapp.management.commands': {
            'handlers': LOGGING_ROOT_HANDLERS.copy(),
            'level': 'INFO',
            'propagate': True,
        },
        'django': {
            'handlers': LOGGING_ROOT_HANDLERS.copy(),
            'level': 'WARNING',
            'propagate': False,
        },
        'django.request': {
            'handlers': LOGGING_ROOT_HANDLERS.copy(),
            'level': 'INFO',
            'propagate': False,
        },
    },
}
import logging

from django.core.management.base import BaseCommand

logger = logging.getLogger(__name__)


class Command(BaseCommand):
    def handle(self, *args, **options):
        logger.info("Message %s", "1")
        logger.info("Message %s", "2")

当前输出:

{"levelname": "INFO", "levelno": 20, "pathname": "runcommand.py", "funcName": "<module>", "asctime": "2020-06-03 09:32:19,497", "message": ""}
{"levelname": "WARNING", "levelno": 30, "pathname": "runcommand.py", "funcName": "<module>", "asctime": "2020-06-03 09:32:19,498", "message": "{\"levelname\": \"INFO\", \"levelno\": 20, \"pathname\": \"/www/app/myapp/management/commands/mycommand.py\", \"funcName\": \"handle\", \"asctime\": \"2020-06-03 09:32:19,345\", \"message\": \"Message 1\"}\n{\"levelname\": \"INFO\", \"levelno\": 20, \"pathname\": \"/www/app/myapp/management/commands/mycommand.py\", \"funcName\": \"handle\", \"asctime\": \"2020-06-03 09:32:19,345\", \"message\": \"Message 1\"}\n{\"levelname\": \"INFO\", \"levelno\": 20, \"pathname\": \"/www/app/myapp/management/commands/mycommand.py\", \"funcName\": \"handle\", \"asctime\": \"2020-06-03 09:32:19,345\", \"message\": \"Message 2\"}\n{\"levelname\": \"INFO\", \"levelno\": 20, \"pathname\": \"/www/app/myapp/management/commands/mycommand.py\", \"funcName\": \"handle\", \"asctime\": \"2020-06-03 09:32:19,345\", \"message\": \"Message 2\"}\n"}
{"levelname": "INFO", "levelno": 20, "pathname": "runcommand.py", "funcName": "<module>", "asctime": "2020-06-03 09:32:19,498", "message": "script \"python manage.py mycommand\" exited with 0"}

如何将第二行改为两行?我不希望邮件被汇总为一个。 -如果我从命令中发出普通的print语句,也会发生这种情况。

1 个答案:

答案 0 :(得分:0)

我知道了。我正在运行python runcommand.py-使用python manage.py进行日志记录的行为与人们期望的一样。