diff options
author | Soren Hansen <soren@linux2go.dk> | 2011-02-15 23:11:51 +0100 |
---|---|---|
committer | Soren Hansen <soren@linux2go.dk> | 2011-02-15 23:11:51 +0100 |
commit | 52a443dfb53e8fa2226e7ae8b8dac0fa6e32a69d (patch) | |
tree | 820bc917cddc04f216c275236eba66f9abce1bf4 /nova | |
parent | 96d0edff2348040362b77491892e525217a17562 (diff) | |
download | nova-52a443dfb53e8fa2226e7ae8b8dac0fa6e32a69d.tar.gz nova-52a443dfb53e8fa2226e7ae8b8dac0fa6e32a69d.tar.xz nova-52a443dfb53e8fa2226e7ae8b8dac0fa6e32a69d.zip |
Refactor code that decides which logfile to use, if any.
Adds unit tests.
Diffstat (limited to 'nova')
-rw-r--r-- | nova/log.py | 17 | ||||
-rw-r--r-- | 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() |