Skip to content

Commit

Permalink
Fix an import that's been missing since the options/log refactoring.
Browse files Browse the repository at this point in the history
Add a test for this previously-uncovered code.
  • Loading branch information
bdarnell committed Dec 30, 2012
1 parent a8cb0ee commit 0b43aef
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
14 changes: 8 additions & 6 deletions tornado/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from __future__ import absolute_import, division, with_statement

import logging
import logging.handlers
import sys
import time

Expand Down Expand Up @@ -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`
Expand All @@ -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):
Expand Down
33 changes: 32 additions & 1 deletion tornado/test/log_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
from __future__ import absolute_import, division, with_statement

import contextlib
import glob
import logging
import os
import re
import tempfile
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

Expand Down Expand Up @@ -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)

0 comments on commit 0b43aef

Please sign in to comment.