Skip to content

Commit 3d06503

Browse files
committed
Update NTEventLogHandler by review comments
1 parent 14a9b86 commit 3d06503

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

Lib/logging/handlers.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,14 +1156,15 @@ def __init__(self, appname, dllname=None, logtype="Application"):
11561156
pass
11571157
self.deftype = _winapi.EVENTLOG_ERROR_TYPE
11581158
self.typemap = {
1159-
logging.DEBUG : _winapi.EVENTLOG_INFORMATION_TYPE,
1160-
logging.INFO : _winapi.EVENTLOG_INFORMATION_TYPE,
1161-
logging.WARNING : _winapi.EVENTLOG_WARNING_TYPE,
1162-
logging.ERROR : _winapi.EVENTLOG_ERROR_TYPE,
1159+
logging.DEBUG: _winapi.EVENTLOG_INFORMATION_TYPE,
1160+
logging.INFO: _winapi.EVENTLOG_INFORMATION_TYPE,
1161+
logging.WARNING: _winapi.EVENTLOG_WARNING_TYPE,
1162+
logging.ERROR: _winapi.EVENTLOG_ERROR_TYPE,
11631163
logging.CRITICAL: _winapi.EVENTLOG_ERROR_TYPE,
11641164
}
11651165

1166-
def _add_source_to_registry(self, appname, dllname, logtype):
1166+
@staticmethod
1167+
def _add_source_to_registry(appname, dllname, logtype):
11671168
import winreg
11681169

11691170
key_path = f"SYSTEM\\CurrentControlSet\\Services\\EventLog\\{logtype}\\{appname}"
@@ -1212,22 +1213,21 @@ def emit(self, record):
12121213
Determine the message ID, event category and event type. Then
12131214
log the message in the NT event log.
12141215
"""
1215-
if self._winapi:
1216-
try:
1217-
id = self.getMessageID(record)
1218-
cat = self.getEventCategory(record)
1219-
type = self.getEventType(record)
1220-
msg = self.format(record)
1221-
1222-
# Get a handle to the event log
1223-
handle = self._winapi.RegisterEventSource(None, self.appname)
1224-
if handle != self._winapi.INVALID_HANDLE_VALUE:
1225-
try:
1226-
self._winapi.ReportEvent(handle, type, cat, id, [msg])
1227-
finally:
1228-
self._winapi.DeregisterEventSource(handle)
1229-
except Exception:
1230-
self.handleError(record)
1216+
try:
1217+
id = self.getMessageID(record)
1218+
cat = self.getEventCategory(record)
1219+
type = self.getEventType(record)
1220+
msg = self.format(record)
1221+
1222+
# Get a handle to the event log
1223+
handle = self._winapi.RegisterEventSource(None, self.appname)
1224+
if handle != self._winapi.INVALID_HANDLE_VALUE:
1225+
try:
1226+
self._winapi.ReportEvent(handle, type, cat, id, [msg])
1227+
finally:
1228+
self._winapi.DeregisterEventSource(handle)
1229+
except Exception:
1230+
self.handleError(record)
12311231

12321232
def close(self):
12331233
"""

Lib/test/test_logging.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7221,14 +7221,14 @@ def test_basic(self):
72217221
self.assertTrue(found, msg=msg)
72227222

72237223
@unittest.skipUnless(sys.platform == "win32", "Windows required for this test")
7224-
def test_updated_implementation(self):
7225-
h = logging.handlers.NTEventLogHandler('test_updated')
7224+
def test_without_pywin32(self):
7225+
h = logging.handlers.NTEventLogHandler('python_test')
72267226
self.addCleanup(h.close)
72277227

72287228
# Verify that the handler uses _winapi module
72297229
self.assertIsNotNone(h._winapi, "_winapi module should be available")
72307230

7231-
r = logging.makeLogRecord({'msg': 'Test Updated Implementation'})
7231+
r = logging.makeLogRecord({'msg': 'Hello!'})
72327232
h.emit(r)
72337233

72347234

0 commit comments

Comments
 (0)