From 96d0edff2348040362b77491892e525217a17562 Mon Sep 17 00:00:00 2001 From: Soren Hansen Date: Mon, 14 Feb 2011 23:19:15 +0100 Subject: Resurrect logdir option. --- nova/flags.py | 2 ++ nova/log.py | 11 +++++++++-- nova/twistd.py | 2 -- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/nova/flags.py b/nova/flags.py index 3ba3fe6fa..f64a62da9 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -282,6 +282,8 @@ DEFINE_integer('auth_token_ttl', 3600, 'Seconds for auth tokens to linger') DEFINE_string('state_path', os.path.join(os.path.dirname(__file__), '../'), "Top-level directory for maintaining nova's state") +DEFINE_string('logdir', None, 'output to a per-service log file in named ' + 'directory') DEFINE_string('sql_connection', 'sqlite:///$state_path/nova.sqlite', diff --git a/nova/log.py b/nova/log.py index b541488bd..a5b4828d5 100644 --- a/nova/log.py +++ b/nova/log.py @@ -28,9 +28,11 @@ It also allows setting of formatting information through flags. import cStringIO +import inspect import json import logging import logging.handlers +import os import sys import traceback @@ -123,8 +125,13 @@ def basicConfig(): syslog = SysLogHandler(address='/dev/log') syslog.setFormatter(_formatter) logging.root.addHandler(syslog) - if FLAGS.logfile: - logfile = FileHandler(FLAGS.logfile) + if FLAGS.logfile or FLAGS.logdir: + if FLAGS.logfile: + logfile = FLAGS.logfile + else: + binary = os.path.basename(inspect.stack()[-1][1]) + logpath = '%s.log' % (os.path.join(FLAGS.logdir, binary),) + logfile = FileHandler(logpath) logfile.setFormatter(_formatter) logging.root.addHandler(logfile) diff --git a/nova/twistd.py b/nova/twistd.py index 6390a8144..60ff7879a 100644 --- a/nova/twistd.py +++ b/nova/twistd.py @@ -43,8 +43,6 @@ else: FLAGS = flags.FLAGS -flags.DEFINE_string('logdir', None, 'directory to keep log files in ' - '(will be prepended to $logfile)') class TwistdServerOptions(ServerOptions): -- cgit From 52a443dfb53e8fa2226e7ae8b8dac0fa6e32a69d Mon Sep 17 00:00:00 2001 From: Soren Hansen Date: Tue, 15 Feb 2011 23:11:51 +0100 Subject: Refactor code that decides which logfile to use, if any. Adds unit tests. --- nova/log.py | 17 +++++++++++------ nova/tests/test_log.py | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/nova/log.py b/nova/log.py index a5b4828d5..ec6681edd 100644 --- a/nova/log.py +++ b/nova/log.py @@ -112,6 +112,15 @@ def _dictify_context(context): context = context.to_dict() return context +def _get_binary_name(): + return os.path.basename(inspect.stack()[-1][1]) + +def get_log_file_path(binary=None): + if FLAGS.logfile: + return FLAGS.logfile + if FLAGS.logdir: + binary = binary or _get_binary_name() + return '%s.log' % (os.path.join(FLAGS.logdir, binary),) def basicConfig(): logging.basicConfig() @@ -125,12 +134,8 @@ def basicConfig(): syslog = SysLogHandler(address='/dev/log') syslog.setFormatter(_formatter) logging.root.addHandler(syslog) - if FLAGS.logfile or FLAGS.logdir: - if FLAGS.logfile: - logfile = FLAGS.logfile - else: - binary = os.path.basename(inspect.stack()[-1][1]) - logpath = '%s.log' % (os.path.join(FLAGS.logdir, binary),) + logpath = get_log_file_path() + if logpath: logfile = FileHandler(logpath) logfile.setFormatter(_formatter) logging.root.addHandler(logfile) diff --git a/nova/tests/test_log.py b/nova/tests/test_log.py index 868a5ead3..7a5c52935 100644 --- a/nova/tests/test_log.py +++ b/nova/tests/test_log.py @@ -46,6 +46,27 @@ class RootLoggerTestCase(test.TestCase): self.assert_(True) # didn't raise exception +class LogHandlerTestCase(test.TestCase): + def test_log_path_logdir(self): + self.flags(logdir='/some/path') + self.assertEquals(log.get_log_file_path(binary='foo-bar'), + '/some/path/foo-bar.log') + + def test_log_path_logfile(self): + self.flags(logfile='/some/path/foo-bar.log') + self.assertEquals(log.get_log_file_path(binary='foo-bar'), + '/some/path/foo-bar.log') + + def test_log_path_none(self): + self.assertIsNone(log.get_log_file_path(binary='foo-bar')) + + def test_log_path_logfile_overrides_logdir(self): + self.flags(logdir='/some/other/path', + logfile='/some/path/foo-bar.log') + self.assertEquals(log.get_log_file_path(binary='foo-bar'), + '/some/path/foo-bar.log') + + class NovaFormatterTestCase(test.TestCase): def setUp(self): super(NovaFormatterTestCase, self).setUp() -- cgit From a00b8b78ce16bbfe4368f0e5e383b7b8f49ba9ef Mon Sep 17 00:00:00 2001 From: Soren Hansen Date: Wed, 16 Feb 2011 10:02:17 +0100 Subject: assertIsNone is a 2.7-ism. --- nova/tests/test_log.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/tests/test_log.py b/nova/tests/test_log.py index 7a5c52935..c2c9d7772 100644 --- a/nova/tests/test_log.py +++ b/nova/tests/test_log.py @@ -58,7 +58,7 @@ class LogHandlerTestCase(test.TestCase): '/some/path/foo-bar.log') def test_log_path_none(self): - self.assertIsNone(log.get_log_file_path(binary='foo-bar')) + self.assertTrue(log.get_log_file_path(binary='foo-bar') is None) def test_log_path_logfile_overrides_logdir(self): self.flags(logdir='/some/other/path', -- cgit From 9f1c46f5be0520afac21c3320e206ced98acd9ba Mon Sep 17 00:00:00 2001 From: Soren Hansen Date: Wed, 16 Feb 2011 10:46:59 +0100 Subject: Fix PEP-8 stuff --- nova/log.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nova/log.py b/nova/log.py index ec6681edd..90c40cca7 100644 --- a/nova/log.py +++ b/nova/log.py @@ -112,9 +112,11 @@ def _dictify_context(context): context = context.to_dict() return context + def _get_binary_name(): return os.path.basename(inspect.stack()[-1][1]) + def get_log_file_path(binary=None): if FLAGS.logfile: return FLAGS.logfile @@ -122,6 +124,7 @@ def get_log_file_path(binary=None): binary = binary or _get_binary_name() return '%s.log' % (os.path.join(FLAGS.logdir, binary),) + def basicConfig(): logging.basicConfig() for handler in logging.root.handlers: -- cgit