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!')