Django日志不正常工作,日志未保存到专用文件中。

huangapple go评论69阅读模式
英文:

Django logging does not work properly and logs are not saved to dedicated files

问题

我正在尝试在Django中进行日志记录。

我希望all_logs 处理程序将所有日志写入一个共同的文件中。
error_file 处理程序将日志写入错误文件,warning_file 处理程序将日志写入警告文件,info_file 处理程序将日志写入信息消息文件。

但结果是我在error_file 中看到错误日志,而在info_file 中看到所有内容:信息、警告和错误。而且根本没有warning_file

只有记录all_logs 的处理程序正常工作。

我期望每个日志级别都会被写入到不同的文件中。我还想知道如何限制日志文件的大小,以及如果日志文件太大,如何将日志划分到不同的文件中。

英文:

I'm trying to do logging in Django.

I want the all_logs handler to write all logs to a common file.
The error_file handler writes logs to the error file, warning_file to the warning file, and info_file to the informative message file.

But as a result I see errors in the error_file and I see everything in the info_file: info, warnings and errors. And there is no warning_file at all.

Only the handler for recording all_logs works well.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '{asctime} | {threadName}({thread}) [{levelname}] {message} ({name} {module}:{lineno})',
            'style': '{',
        },
        'simple': {
            'format': '{asctime} [{levelname}] {message} ({name}:{lineno})',
            'datefmt': '%H:%M:%S',
            'style': '{',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        },

        'all_logs': {
            'class': 'logging.handlers.WatchedFileHandler',
            'filename': str(BASE_DIR.joinpath('log', 'all.log')),
            'formatter': 'verbose',
        },

        'error_file': {
            'level': 'ERROR',
            'class': 'logging.FileHandler',
            'filename': str(BASE_DIR.joinpath('log', 'error.log')),
            'formatter': 'verbose',
        },

        'warning_file': {
            'level': 'WARNING',
            'class': 'logging.FileHandler',
            'filename': str(BASE_DIR.joinpath('log', 'warning.log')),
            'formatter': 'verbose',
        },

        'info_file': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': str(BASE_DIR.joinpath('log', 'info.log')),
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['all_logs', 'console', 'error_file', 'warning_file', 'info_file'],
            'propagate': False,
            'formatter': 'verbose'
        },
        # my app
        'applications': {
            'handlers': ['all_logs', 'console', 'error_file', 'warning_file', 'info_file'],
            'propagate': False,
            'formatter': 'verbose'
        },
    },
}

So, I am expecting that every log level will be written to dedicated file. I also would like to know how can I restrict log file size and divide logs in difirent files if log file is too big.

答案1

得分: 1

但结果是,我在错误文件中看到错误,而在信息文件中看到了一切:信息、警告和错误。

这是由Django中的level工作方式引起的。以下是文档的解释:

当将消息传递给记录器时,消息的日志级别将与记录器的日志级别进行比较。如果消息的日志级别满足或超过记录器本身的日志级别,消息将进一步处理。否则,消息将被忽略。

换句话说,INFO级别日志包含INFO消息以及比它更严重的消息。

而且根本没有warning_file

查看与该文件关联的内容:

'filename': str(BASE_DIR.joinpath('log', 'error.log')),

这也是错误日志。

英文:

>But as a result I see errors in the error_file and I see everything in the info_file: info, warnings and errors.

This is caused by how level works in Django. Here's what the documentation says:

>When a message is given to the logger, the log level of the message is compared to the log level of the logger. If the log level of the message meets or exceeds the log level of the logger itself, the message will undergo further processing. If it doesn’t, the message will be ignored.

In other words, an INFO level log contains INFO messages, and anything more serious than that.

>And there is no warning_file at all.

Look at the file associated with that one:

'filename': str(BASE_DIR.joinpath('log', 'error.log')),

It's also the error log.

huangapple
  • 本文由 发表于 2023年2月6日 07:08:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定