From 74e8e48a95e8cd9fa2746ffc32db742061924d93 Mon Sep 17 00:00:00 2001 From: Daniel Vincze Date: Wed, 31 Oct 2018 16:44:01 +0200 Subject: [PATCH] Add Windows Event Log handler The Python built-in logging module already provides a Windows Event Log handler. This change ensures that oslo.log exposes it. Change-Id: I287260b5046c88c433dfa66064da14faf15610e0 Implements: blueprint windows-event-log --- oslo_log/_options.py | 3 +++ oslo_log/log.py | 8 ++++++++ oslo_log/tests/unit/test_log.py | 19 +++++++++++++++++++ .../windows-eventlog-2beb0a6010e342eb.yaml | 4 ++++ 4 files changed, 34 insertions(+) mode change 100644 => 100755 oslo_log/tests/unit/test_log.py create mode 100644 releasenotes/notes/windows-eventlog-2beb0a6010e342eb.yaml diff --git a/oslo_log/_options.py b/oslo_log/_options.py index e2162482..8f010434 100644 --- a/oslo_log/_options.py +++ b/oslo_log/_options.py @@ -108,6 +108,9 @@ generic_log_opts = [ default=False, help='Log output to standard error. ' + _IGNORE_MESSAGE), + cfg.BoolOpt('use_eventlog', + default=False, + help='Log output to Windows Event Log.'), ] log_opts = [ diff --git a/oslo_log/log.py b/oslo_log/log.py index b39e8219..d663ec46 100644 --- a/oslo_log/log.py +++ b/oslo_log/log.py @@ -361,6 +361,14 @@ def _setup_logging_from_conf(conf, project, version): journal = handlers.OSJournalHandler() log_root.addHandler(journal) + if conf.use_eventlog: + if platform.system() == 'Windows': + eventlog = logging.handlers.NTEventLogHandler(project) + log_root.addHandler(eventlog) + else: + raise RuntimeError(_("Windows Event Log is not available on this " + "platform.")) + # if None of the above are True, then fall back to standard out if not logpath and not conf.use_stderr and not conf.use_journal: # pass sys.stdout as a positional argument diff --git a/oslo_log/tests/unit/test_log.py b/oslo_log/tests/unit/test_log.py old mode 100644 new mode 100755 index fdaceef5..aa3b98cb --- a/oslo_log/tests/unit/test_log.py +++ b/oslo_log/tests/unit/test_log.py @@ -140,6 +140,25 @@ class CommonLoggerTestsMixIn(object): 'info', 'debug', 'log'): self.assertRaises(AttributeError, getattr, log, func) + @mock.patch('platform.system', return_value='Linux') + def test_eventlog_missing(self, platform_mock): + self.config(use_eventlog=True) + self.assertRaises(RuntimeError, + log._setup_logging_from_conf, + self.CONF, + 'test', + 'test') + + @mock.patch('platform.system', return_value='Windows') + @mock.patch('logging.handlers.NTEventLogHandler') + @mock.patch('oslo_log.log.getLogger') + def test_eventlog(self, loggers_mock, handler_mock, platform_mock): + self.config(use_eventlog=True) + log._setup_logging_from_conf(self.CONF, 'test', 'test') + handler_mock.assert_called_once_with('test') + mock_logger = loggers_mock.return_value.logger + mock_logger.addHandler.assert_any_call(handler_mock.return_value) + class LoggerTestCase(CommonLoggerTestsMixIn, test_base.BaseTestCase): def setUp(self): diff --git a/releasenotes/notes/windows-eventlog-2beb0a6010e342eb.yaml b/releasenotes/notes/windows-eventlog-2beb0a6010e342eb.yaml new file mode 100644 index 00000000..ccdfc6fd --- /dev/null +++ b/releasenotes/notes/windows-eventlog-2beb0a6010e342eb.yaml @@ -0,0 +1,4 @@ +features: + - | + Added Windows EventLog functionality to oslo.log. Set use_eventlog to true + in the service's configuration file to use it.