How to create Windows event log with Python

  event-log, python, pywin32, windows

I have an issue with writing event log to Windows Event Viewer. I want to register a custom Event Log under Applications and Services Logs menu but I have no idea to do it in Python (I am using PyWin32 in this case). Basically we can register a custom log with Powershell’s New-EventLog but I want to do it in Python.

These are some attemps I have done:

  • Create an event log with Powershell’s New-EventLog:

New-EventLog -LogName <Log Name> -Source <Source>

  • In Python, open a handler with registered Log Name:
import win32evtlog

hand = win32evtlog.OpenEventLog(None, 'CustomRegisteredEventLog')
# then write to Event Log
win32evtlog.ReportEvent(hand, win32evtlog.EVENTLOG_INFORMATION_TYPE, 0, 0, None, ['some message'], None)

Those steps above actually works, the log is successfully submitted to event log, but I still need to register the Event Log with Python. Could anyone guide me on how to do this on Python?

Source: Windows Questions

LEAVE A COMMENT