From f9c8664e0f3587c934c5969cf750904ba05a1805 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Mon, 6 Jan 2014 22:39:29 -0500 Subject: [PATCH] Rename "normal" to "end_color" in log format strings. When not colorizing, use empty strings instead of two copies of "normal". Update docs. --- docs/releases/next.rst | 2 +- tornado/log.py | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/docs/releases/next.rst b/docs/releases/next.rst index 4e99ab186..3c96560d9 100644 --- a/docs/releases/next.rst +++ b/docs/releases/next.rst @@ -108,7 +108,7 @@ New modules * Fix an error from `tornado.log.enable_pretty_logging` when `sys.stderr` does not have an ``isatty`` method. -* `tornado.log.LogFormatter` now accepts keyword arguments ``prefix_fmt`` +* `tornado.log.LogFormatter` now accepts keyword arguments ``fmt`` and ``datefmt``. `tornado.netutil` diff --git a/tornado/log.py b/tornado/log.py index 16b30f480..a4c742348 100644 --- a/tornado/log.py +++ b/tornado/log.py @@ -80,7 +80,7 @@ class LogFormatter(logging.Formatter): `tornado.options.parse_command_line` (unless ``--logging=none`` is used). """ - DEFAULT_FORMAT = '%(color)s[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d]%(normal)s %(message)s' + DEFAULT_FORMAT = '%(color)s[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d]%(end_color)s %(message)s' DEFAULT_DATE_FORMAT = '%y%m%d %H:%M:%S' DEFAULT_COLORS = { logging.DEBUG: 4, # Blue @@ -95,7 +95,7 @@ class LogFormatter(logging.Formatter): :arg bool color: Enables color support. :arg string fmt: Log message format. It will be applied to the attributes dict of log records. The - text between ``%(color)s`` and ``%(normal)s`` will be colored + text between ``%(color)s`` and ``%(end_color)s`` will be colored depending on the level if color support is on. :arg dict colors: color mappings from logging level to terminal color code @@ -104,7 +104,7 @@ class LogFormatter(logging.Formatter): .. versionchanged:: 3.2 - Added ``prefix_fmt`` and ``datefmt`` arguments. + Added ``fmt`` and ``datefmt`` arguments. """ logging.Formatter.__init__(self, datefmt=datefmt) self._fmt = fmt @@ -155,8 +155,11 @@ class LogFormatter(logging.Formatter): record.asctime = self.formatTime(record, self.datefmt) - record.color = self._colors.get(record.levelno, self._normal) - record.normal = self._normal + if record.levelno in self._colors: + record.color = self._colors.get(record.levelno, '') + record.end_color = self._normal + else: + record.color = record.end_color = '' formatted = self._fmt % record.__dict__ -- 2.47.2