From 56e16fce415b14855e44d6434921d394b3a89f8e Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 31 Oct 2013 08:57:36 -0400 Subject: [PATCH] Don't log to stdout when log_dir is set In addition to checking if log_file is set, we should check for log_dir as well. Previously, If log_file is not set, oslo will output logs to stdout even if log_dir is set. The only way for example the Heat folks avoided logging to stdout was to set log_file, but then all the heat processes ended up logging to the same file. So a check for both log_dir and log_file should avoid any output to stdout. Please note that use_stderr should be set to false as well to avoid any output to console Change-Id: Ie2886da679daedea0197ee3b3963ebedb8f11a0b Closes-Bug: #1226287 --- openstack/common/log.py | 2 +- tests/unit/test_log.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/openstack/common/log.py b/openstack/common/log.py index c7c7260e..5150bc76 100644 --- a/openstack/common/log.py +++ b/openstack/common/log.py @@ -477,7 +477,7 @@ def _setup_logging_from_conf(): streamlog = ColorHandler() log_root.addHandler(streamlog) - elif not CONF.log_file: + elif not logpath: # pass sys.stdout as a positional argument # python2.6 calls the argument strm, in 2.7 it's stream streamlog = logging.StreamHandler(sys.stdout) diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py index 751732b9..aea4f98b 100644 --- a/tests/unit/test_log.py +++ b/tests/unit/test_log.py @@ -17,6 +17,7 @@ import logging import os import sys +import tempfile import mock from oslo.config import cfg @@ -437,6 +438,16 @@ class LogConfigOptsTestCase(test.BaseTestCase): self.CONF(['--log-file', log_file]) self.assertEqual(self.CONF.log_file, log_file) + def test_log_dir_handlers(self): + log_dir = tempfile.mkdtemp() + self.CONF(['--log-dir', log_dir]) + self.CONF.set_default('use_stderr', False) + log._setup_logging_from_conf() + logger = log._loggers[None].logger + self.assertEqual(1, len(logger.handlers)) + self.assertIsInstance(logger.handlers[0], + logging.handlers.WatchedFileHandler) + def test_logfile_deprecated(self): logfile = '/some/other/path/foo-bar.log' self.CONF(['--logfile', logfile])