From 6dbc4f8a59a7452c3b8bdfb9ee5ecd07d96c8595 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Sat, 4 Aug 2012 15:15:44 -0400 Subject: [PATCH] Only log deprecated config warnings once. Updates Nova's deprecated warn function so that we only log messages about deprecated config options once. The motivation for this change is to cut down the log file noise when deprecated config code gets called more than once. Change-Id: If8919817330dc9461a0472fea982d43c78a86f66 --- nova/common/deprecated.py | 15 ++++++++++++++- nova/tests/test_deprecated.py | 6 ++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/nova/common/deprecated.py b/nova/common/deprecated.py index feef86d9..6b1c587e 100644 --- a/nova/common/deprecated.py +++ b/nova/common/deprecated.py @@ -14,6 +14,8 @@ # License for the specific language governing permissions and limitations # under the License. +import warnings + from nova import exception from nova import flags from nova.openstack.common import cfg @@ -30,6 +32,17 @@ FLAGS = flags.FLAGS FLAGS.register_opts(deprecate_opts) +def _showwarning(message, category, filename, lineno, file=None, line=None): + """ + Redirect warnings into logging. + """ + LOG.warn(str(message)) + + +# Install our warnings handler +warnings.showwarning = _showwarning + + def warn(msg=""): """ Warn of a deprecated config option that an operator has specified. @@ -37,6 +50,6 @@ def warn(msg=""): we use some operator changeable parameter to indicate that it will go away in a future version of OpenStack. """ - LOG.warn(_("Deprecated Config: %s") % msg) + warnings.warn(_("Deprecated Config: %s") % msg) if FLAGS.fatal_deprecations: raise exception.DeprecatedConfig(msg=msg) diff --git a/nova/tests/test_deprecated.py b/nova/tests/test_deprecated.py index e65baa53..ebc6fed9 100644 --- a/nova/tests/test_deprecated.py +++ b/nova/tests/test_deprecated.py @@ -38,3 +38,9 @@ class DeprecatedConfigTestCase(test.TestCase): self.assertRaises(exception.DeprecatedConfig, deprecated.warn, "test2") self.assertEqual(self.logbuffer, 'Deprecated Config: test2') + + def test_deprecated_logs_only_once(self): + deprecated.warn('only once!') + deprecated.warn('only once!') + deprecated.warn('only once!') + self.assertEqual(self.logbuffer, 'Deprecated Config: only once!')