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
This commit is contained in:
Daniel Vincze 2018-10-31 16:44:01 +02:00
parent 5cd02483c6
commit 74e8e48a95
4 changed files with 34 additions and 0 deletions

View File

@ -108,6 +108,9 @@ generic_log_opts = [
default=False, default=False,
help='Log output to standard error. ' help='Log output to standard error. '
+ _IGNORE_MESSAGE), + _IGNORE_MESSAGE),
cfg.BoolOpt('use_eventlog',
default=False,
help='Log output to Windows Event Log.'),
] ]
log_opts = [ log_opts = [

View File

@ -361,6 +361,14 @@ def _setup_logging_from_conf(conf, project, version):
journal = handlers.OSJournalHandler() journal = handlers.OSJournalHandler()
log_root.addHandler(journal) 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 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: if not logpath and not conf.use_stderr and not conf.use_journal:
# pass sys.stdout as a positional argument # pass sys.stdout as a positional argument

19
oslo_log/tests/unit/test_log.py Normal file → Executable file
View File

@ -140,6 +140,25 @@ class CommonLoggerTestsMixIn(object):
'info', 'debug', 'log'): 'info', 'debug', 'log'):
self.assertRaises(AttributeError, getattr, log, func) 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): class LoggerTestCase(CommonLoggerTestsMixIn, test_base.BaseTestCase):
def setUp(self): def setUp(self):

View File

@ -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.