Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix an import that's been missing since the options/log refactoring.

Add a test for this previously-uncovered code.
  • Loading branch information...
commit 0b43aef0589afa6a8f6cb9975a559039277d9db8 1 parent a8cb0ee
@bdarnell bdarnell authored
Showing with 40 additions and 7 deletions.
  1. +8 −6 tornado/log.py
  2. +32 −1 tornado/test/log_test.py
View
14 tornado/log.py
@@ -31,6 +31,7 @@
from __future__ import absolute_import, division, with_statement
import logging
+import logging.handlers
import sys
import time
@@ -141,7 +142,7 @@ def format(self, record):
formatted = formatted.rstrip() + "\n" + record.exc_text
return formatted.replace("\n", "\n ")
-def enable_pretty_logging(options=None):
+def enable_pretty_logging(options=None, logger=None):
"""Turns on formatted logging output as configured.
This is called automaticaly by `tornado.options.parse_command_line`
@@ -151,22 +152,23 @@ def enable_pretty_logging(options=None):
from tornado.options import options
if options.logging == 'none':
return
- root_logger = logging.getLogger()
- root_logger.setLevel(getattr(logging, options.logging.upper()))
+ if logger is None:
+ logger = logging.getLogger()
+ logger.setLevel(getattr(logging, options.logging.upper()))
if options.log_file_prefix:
channel = logging.handlers.RotatingFileHandler(
filename=options.log_file_prefix,
maxBytes=options.log_file_max_size,
backupCount=options.log_file_num_backups)
channel.setFormatter(LogFormatter(color=False))
- root_logger.addHandler(channel)
+ logger.addHandler(channel)
if (options.log_to_stderr or
- (options.log_to_stderr is None and not root_logger.handlers)):
+ (options.log_to_stderr is None and not logger.handlers)):
# Set up color if we are in a tty and curses is installed
channel = logging.StreamHandler()
channel.setFormatter(LogFormatter())
- root_logger.addHandler(channel)
+ logger.addHandler(channel)
def define_logging_options(options=None):
View
33 tornado/test/log_test.py
@@ -16,6 +16,7 @@
from __future__ import absolute_import, division, with_statement
import contextlib
+import glob
import logging
import os
import re
@@ -23,7 +24,8 @@
import warnings
from tornado.escape import utf8
-from tornado.log import LogFormatter
+from tornado.log import LogFormatter, define_logging_options, enable_pretty_logging
+from tornado.options import OptionParser
from tornado.test.util import unittest
from tornado.util import b, bytes_type
@@ -116,3 +118,32 @@ def make_handler(self, filename):
def test_unicode_logging(self):
self.logger.error(u"\u00e9")
self.assertEqual(self.get_output(), utf8(u"\u00e9"))
+
+
+class EnablePrettyLoggingTest(unittest.TestCase):
+ def setUp(self):
+ super(EnablePrettyLoggingTest, self).setUp()
+ self.options = OptionParser()
+ define_logging_options(self.options)
+ self.logger = logging.Logger('tornado.test.log_test.EnablePrettyLoggingTest')
+ self.logger.propagate = False
+
+ def test_log_file(self):
+ tmpdir = tempfile.mkdtemp()
+ try:
+ self.options.log_file_prefix = tmpdir + '/test_log'
+ enable_pretty_logging(options=self.options, logger=self.logger)
+ self.assertEqual(1, len(self.logger.handlers))
+ self.logger.error('hello')
+ self.logger.handlers[0].flush()
+ filenames = glob.glob(tmpdir + '/test_log*')
+ self.assertEqual(1, len(filenames))
+ with open(filenames[0]) as f:
+ self.assertRegexpMatches(f.read(), r'^\[E [^]]*\] hello$')
+ finally:
+ for handler in self.logger.handlers:
+ handler.flush()
+ handler.close()
+ for filename in glob.glob(tmpdir + '/test_log*'):
+ os.unlink(filename)
+ os.rmdir(tmpdir)
Please sign in to comment.
Something went wrong with that request. Please try again.