简体   繁体   English

如何更改龙卷风的记录日期时间格式?

[英]How to change Tornado's logging datetime format?

The default tornado log like this 像这样的默认龙卷风日志

[I 160418 21:51:16 web:1946] 200 GET /hello (123.123.123.123) 21.72ms

I want change the date format to this 我想将日期格式更改为此

[I 2016-04-18 21:51:16 web:1946] 200 GET /hello (123.123.123.123) 21.72ms

How to implement it? 如何执行呢?

您可以按照以下说明更改日期格式: http : //www.tornadoweb.org/en/stable/log.html

datefmt (string) – Datetime format. Used for formatting (asctime) placeholder in prefix_fmt.

Tornado using the standard library's logging module. 使用标准库的日志记录模块进行龙卷风。 This means that when you configure a logging module, you configure Tornado's logs. 这意味着在配置日志记录模块时,将配置龙卷风的日志。 And when you in your application output logs through this standard module, they fall into the logs of Tornado. 当您在应用程序中通过此标准模块输出日志时,它们将落入Tornado日志中。

1. The best way to set up Tornado's logging datetime format is configure logging handlers after Tornado's config is parsed. 1.设置Tornado的日志记录日期时间格式的最佳方法是在解析Tornado的配置后配置日志记录处理程序。

import logging
from tornado.options import parse_command_line

parse_command_line()  # parsing Tornado's default config

formatter = logging.Formatter(
    '[%(levelname)1.1s %(asctime)s.%(msecs)d '
    '%(module)s:%(lineno)d] %(message)s',
    "%Y-%m-%d %H:%M:%S"
)  # creating own format
for handler in logging.getLogger().handlers:  # setting format for all handlers
    handler.setFormatter(formatter)

logging.info("message")  # will display in tornado logs info:
# [I 2018-11-07 17:37:20.463 datefmt_0:14] message

2. Other way is remove handlers before configure: 2.另一种方法是在配置之前删除处理程序:

import logging
from tornado.options import parse_command_line

for hendler in logging.getLogger().handlers:  # remove current handlers
    logging.root.removeHandler(hendler)

logging.basicConfig(format='[%(levelname)1.1s %(asctime)s.%(msecs)d '
                           '%(module)s:%(lineno)d] %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')
parse_command_line()  # parsing tornado default config
logging.info("message")  # will display in tornado log info:
# [I 2018-11-01 17:27:09.824 datefmt:11] message

3. Try also (i get it from here: https://github.com/tornadoweb/tornado/issues/1960 ): 3.也尝试一下(我从这里得到它: https : //github.com/tornadoweb/tornado/issues/1960 ):

import logging
import logging.config

logging.config.dictConfig({
    'version': 1,
    'formatters': {
        'default': {
            'class': 'tornado.log.LogFormatter',
            'format': '[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d] %(message)s',
        },
    },
    'handlers': {
        'default': {
            'class': 'logging.StreamHandler',
            'formatter': 'default',
        },
    },
    'root': {   # settings of root logger.
        'level': 'DEBUG',
        'handlers': ['default'],
        'propagate': False,
    },
})

logging.info("message")

4. Try also (from here Python Logging: dictConfig ): 4.也尝试(从此处进行Python日志记录:dictConfig ):

import logging
import logging.handlers

console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(
    '[%(levelname)1.1s %(asctime)s.%(msecs)d '
    '%(module)s:%(lineno)d] %(message)s',
    "%Y-%m-%d %H:%M:%S")
)
logging.root.setLevel(logging.DEBUG)
logging.root.addHandler(console_handler)

logging.info("message")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM