Django按日期或小时记录到文件 [英] Django log to file by date or hour

查看:159
本文介绍了Django按日期或小时记录到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道django可以登录到一个文件,但是我想知道如何在差异时间内打印到不同的文件。文件名例如 20150101.log 20150102.log

I know django can log to one file, but I want know how to print to different files in diff times. File name like 20150101.log 20150102.log.

两个问题:


  1. 我可以通过 LOGGING->文件名 config来实现吗?

  2. 如果第一季度无法解决问题,如何动态更改处理程序

  1. Can I implement this by LOGGING->filename config ?
  2. if Q1 can't, How can I dynamic change handlers ?






换句话说,我如何不按文件大小而是按日期时间制作django拆分文件。

推荐答案

TimedRotatingFileHandler

要登录到基于您可以在日期或时间使用 logging.handlers.TimedRotatingFileHandler class。

To log to different files based on the date or time you can use the logging.handlers.TimedRotatingFileHandler class.

TimedRotatingFileHandler 将所有消息记录到具有您指定名称的文件中。在指定的时间间隔过后,它将把当前文件旋转到具有给定名称并带有日期和时间的文件。

The TimedRotatingFileHandler logs all messages to a file with a name that you specify. After the designated interval has elapsed it will rotate the current file to a file with the given name with the date and time appended to it.

如果文件名是 myproject.log ,然后在每个间隔结束时,它将文件旋转为 myproject.log.2015-01-01 ,然后继续将当前消息记录到 myproject.log

If your filename is myproject.log then at the end of each interval it will rotate that file to be myproject.log.2015-01-01 and then continue logging current messages to myproject.log.

不幸的是,此类的行为不像您预期​​的那样。

Unfortunately, this class does not behave quite like you might expect. Rather than rotating based on the actual date and time, it rotates based on the time interval.

如果您选择的间隔是1天,而您恰好在中午启动Django,则它不会根据实际的日期和时间进行轮换。 1月1日起,它将不会在午夜旋转,并将1月1日起的所有消息放入名为 myproject.log.2015-01-01 的文件中。相反,它将在开始1天后的1月2日中午旋转。它将所有消息旋转到名为 myproject.log.2015-01-02 的文件中。因此,此文件将包含来自1月1日和2月2日的消息。

If your chosen interval is 1 day and you happen to start Django running at noon on January 1st it will not rotate at midnight and put all messages from January 1st into a file named myproject.log.2015-01-01. Rather, it will rotate at noon on January 2nd which is 1 day after you started. It will rotate all messages to a file named myproject.log.2015-01-02. So this file will contain messages from both January 1st and 2nd.

如果您希望该类根据实际时间运行,则覆盖该类和专门覆盖名为 shouldRollover 的方法。

If you wish for the class to behave based on actual time it would be fairly trivial to override the class and specifically override the method called shouldRollover.

至少该课程每天会给您一些时间间隔,因此您可以快速找到所需的日志文件。您只需查看两个文件即可。

At least the class will give you daily intervals so you can quickly find the log files that you need. You will only have to look in two files.

要在Django中进行设置,请执行以下操作:

To set it up in Django do the following:

LOGGING = { 
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },  
    'handlers': {
        'file': {
            'level': 'INFO',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': 'myproject.log',
            'when': 'D', # this specifies the interval
            'interval': 1, # defaults to 1, only necessary for other values 
            'backupCount': 10, # how many backup file to keep, 10 days
            'formatter': 'verbose',
        },

    },  
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
        },
        '': {
            'handlers': ['file'],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
        }
    },  
}

这篇关于Django按日期或小时记录到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆