tornado.log
— Logging support¶
Logging support for Tornado.
Tornado uses three logger streams:
tornado4.access
: Per-request logging for Tornado’s HTTP servers (and potentially other servers in the future)tornado4.application
: Logging of errors from application code (i.e. uncaught exceptions from callbacks)tornado4.general
: General-purpose logging, including any errors or warnings from Tornado itself.
These streams may be configured independently using the standard library’s
logging
module. For example, you may wish to send tornado4.access
logs
to a separate file for analysis.
-
class
tornado.log.
LogFormatter
(fmt='%(color)s[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d]%(end_color)s %(message)s', datefmt='%y%m%d %H:%M:%S', style='%', color=True, colors={10: 4, 20: 2, 30: 3, 40: 1})[source]¶ Log formatter used in Tornado.
Key features of this formatter are:
- Color support when logging to a terminal that supports it.
- Timestamps on every log line.
- Robust against str/bytes encoding problems.
This formatter is enabled automatically by
tornado4.options.parse_command_line
ortornado4.options.parse_config_file
(unless--logging=none
is used).Color support on Windows versions that do not support ANSI color codes is enabled by use of the colorama library. Applications that wish to use this must first initialize colorama with a call to
colorama.init
. See the colorama documentation for details.Changed in version 4.5: Added support for
colorama
. Changed the constructor signature to be compatible withlogging.config.dictConfig
.Parameters: - color (bool) – Enables color support.
- fmt (string) – Log message format.
It will be applied to the attributes dict of log records. The
text between
%(color)s
and%(end_color)s
will be colored depending on the level if color support is on. - colors (dict) – color mappings from logging level to terminal color code
- datefmt (string) – Datetime format.
Used for formatting
(asctime)
placeholder inprefix_fmt
.
Changed in version 3.2: Added
fmt
anddatefmt
arguments.-
format
(record)[source]¶ Format the specified record as text.
The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
-
tornado.log.
enable_pretty_logging
(options=None, logger=None)[source]¶ Turns on formatted logging output as configured.
This is called automatically by
tornado4.options.parse_command_line
andtornado4.options.parse_config_file
.
-
tornado.log.
define_logging_options
(options=None)[source]¶ Add logging-related flags to
options
.These options are present automatically on the default options instance; this method is only necessary if you have created your own
OptionParser
.New in version 4.2: This function existed in prior versions but was broken and undocumented until 4.2.