Skip to content

Commit

Permalink
Merge 16abb49 into 315a33c
Browse files Browse the repository at this point in the history
  • Loading branch information
doismellburning committed Feb 22, 2015
2 parents 315a33c + 16abb49 commit ae8211e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions django12factor/__init__.py
Expand Up @@ -62,6 +62,7 @@ def factorise(custom_settings=None):
'stdout': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stdout',
}
},
'root': {
Expand Down
40 changes: 39 additions & 1 deletion tests/test_logging.py
@@ -1,8 +1,13 @@
from __future__ import absolute_import
from __future__ import (
absolute_import,
print_function,
)

import StringIO
import django12factor
import logging
import logging.config
import sys
import unittest

from .env import env
Expand All @@ -14,6 +19,19 @@ def has_handler(logger, handler_name):
return any([handler.name == handler_name for handler in logger.handlers])


def capture_stdout(command, *args, **kwargs):
normal_stdout = sys.stdout
captured_stdout = StringIO.StringIO()
sys.stdout = captured_stdout

command(*args, **kwargs)

captured_stdout.seek(0)
sys.stdout = normal_stdout

return captured_stdout.read()


class TestLogging(unittest.TestCase):

def test_root_logger_config(self):
Expand All @@ -31,3 +49,23 @@ def test_root_logger_config(self):
with debug_env:
logging.config.dictConfig(django12factor.factorise()['LOGGING'])
self.assertTrue(has_handler(logging.root, "stdout"))

def test_capture_stdout_works_with_print(self):
"""
Assert that `capture_stdout` captures `print` text
"""
output = capture_stdout(print, "wibble")
self.assertIn("wibble", output)

def test_logging_to_stdout(self):

with debug_env:
logging.config.dictConfig(django12factor.factorise()['LOGGING'])

message = "lorem ipsum"
output = capture_stdout(logging.info, message)
self.assertIn(
message, output,
"Message '%s' should have been logged to stdout and captured;"
"instead '%s' was captured" % (message, output)
)

0 comments on commit ae8211e

Please sign in to comment.