简体   繁体   中英

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.

1. The best way to set up Tornado's logging datetime format is configure logging handlers after Tornado's config is parsed.

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:

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 ):

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 ):

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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