Skip to content

Commit 9c3c336

Browse files
committed
Move set warnings filters to logging module
This is the first step in moving logging out of shell.py Change-Id: I3dcb4e17bb4687988ddf9b793ad1a308ef89b242 Implements: blueprint logging-migration
1 parent e23dd6d commit 9c3c336

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

openstackclient/common/context.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@
1414
"""Context and Formatter"""
1515

1616
import logging
17+
import warnings
1718

1819
_LOG_MESSAGE_FORMAT = ('%(asctime)s.%(msecs)03d %(process)d '
1920
'%(levelname)s %(name)s [%(clouds_name)s '
2021
'%(username)s %(project_name)s] %(message)s')
2122
_LOG_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
2223

2324

25+
def set_warning_filter(log_level):
26+
if log_level == logging.ERROR:
27+
warnings.simplefilter("ignore")
28+
elif log_level == logging.WARNING:
29+
warnings.simplefilter("ignore")
30+
elif log_level == logging.INFO:
31+
warnings.simplefilter("once")
32+
33+
2434
def setup_handler_logging_level(handler_type, level):
2535
"""Setup of the handler for set the logging level
2636

openstackclient/shell.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import logging
2121
import sys
2222
import traceback
23-
import warnings
2423

2524
from cliff import app
2625
from cliff import command
@@ -117,18 +116,16 @@ def configure_logging(self):
117116
if self.options.verbose_level == 0:
118117
# --quiet
119118
log_level = logging.ERROR
120-
warnings.simplefilter("ignore")
121119
elif self.options.verbose_level == 1:
122120
# This is the default case, no --debug, --verbose or --quiet
123121
log_level = logging.WARNING
124-
warnings.simplefilter("ignore")
125122
elif self.options.verbose_level == 2:
126123
# One --verbose
127124
log_level = logging.INFO
128-
warnings.simplefilter("once")
129125
elif self.options.verbose_level >= 3:
130126
# Two or more --verbose
131127
log_level = logging.DEBUG
128+
context.set_warning_filter(log_level)
132129

133130
# Set the handler logging level of FileHandler(--log-file)
134131
# and StreamHandler

openstackclient/tests/common/test_context.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ def test_setup_logging(self, setuph):
6262
context.setup_logging(shell, cloud_config)
6363
self.assertEqual(True, shell.enable_operation_logging)
6464

65+
@mock.patch('warnings.simplefilter')
66+
def test_set_warning_filter(self, simplefilter):
67+
context.set_warning_filter(logging.ERROR)
68+
simplefilter.assert_called_with("ignore")
69+
context.set_warning_filter(logging.WARNING)
70+
simplefilter.assert_called_with("ignore")
71+
context.set_warning_filter(logging.INFO)
72+
simplefilter.assert_called_with("once")
73+
6574

6675
class Test_LogContext(utils.TestCase):
6776
def setUp(self):

0 commit comments

Comments
 (0)