Skip to content

Commit

Permalink
Ensure DeprecationWarning is ignored when setting up logging
Browse files Browse the repository at this point in the history
  • Loading branch information
seandst committed Jan 12, 2017
1 parent bf20f60 commit 451e7aa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions server/pulp/server/logs.py
Expand Up @@ -6,6 +6,7 @@
import os
import sys
import threading
import warnings

from celery.signals import setup_logging

Expand Down Expand Up @@ -41,6 +42,11 @@ def start_logging(*args, **kwargs):
:param kwargs: Unused
:type kwargs: dict
"""
# Filter out all deprecation warnings before setting up the logger,
# unless user-specified warning settings are passed using '-W'.
if not sys.warnoptions:
warnings.simplefilter('ignore', DeprecationWarning)

# Get and set up the root logger with our configured log level
try:
log_level = config.config.get('server', 'log_level')
Expand Down
28 changes: 28 additions & 0 deletions server/test/unit/server/test_logs.py
Expand Up @@ -12,6 +12,7 @@
import threading
import traceback
import unittest
import warnings

import mock

Expand Down Expand Up @@ -687,6 +688,15 @@ def test_calls__blacklist_loggers(self, getLogger, get, _blacklist_loggers):

_blacklist_loggers.assert_called_once_with()

@mock.patch('pulp.server.logs.warnings')
def test_calls_simplefilter(self, _warnings):
"""
Ensure that start_logging() calls warnings.simplefilter
"""
logs.start_logging()

_warnings.simplefilter.assert_called_once_with("ignore", DeprecationWarning)

@mock.patch('pulp.server.logs.logging')
def test_calls__captureWarnings(self, _logging):
"""
Expand Down Expand Up @@ -812,6 +822,24 @@ def fake_getLogger(name=None):
# And the handler should have the formatter with our format string
self.assertEqual(root_handler.formatter._fmt, logs.LOG_FORMAT_STRING)

def test_deprecation_warnings_suppressed(self):
with warnings.catch_warnings(record=True) as recorded_warnings:
# reset warnings filter to include DeprecationWarnings
# https://docs.python.org/2/library/warnings.html#updating-code-for-new-versions-of-python
warnings.simplefilter('default')

# this warning should be emitted and caught prior to calling start_logging
warnings.warn("caught", DeprecationWarning)

logs.start_logging()

# this warning should be emitted and ignored after calling start_logging
warnings.warn("ignored", DeprecationWarning)

# Only one warning was caught, and it's the one that was expected.
self.assertEqual(len(recorded_warnings), 1)
self.assertEqual(repr(recorded_warnings[0].message), repr(DeprecationWarning("caught")))


class TestStopLogging(unittest.TestCase):
"""
Expand Down

0 comments on commit 451e7aa

Please sign in to comment.