Skip to main content

Python logging via Event Tracing for Windows

Project description

Python logging via Event Tracing for Windows (ETW)

build docs

PyEtw is a Python package that allows you to use Pythons standard logging facility with Event Tracing for Windows (ETW).

PyEtw implements logging.Handler and overloads emit() to handle the logging.LogRecord objects. The logging.LogRecord object is then converted to a Windows event record, and written via the API EventWriteTransfer as an ETW TraceLogging event.

Using ETW for Python logging allows you to leverage the many tools available for the Windows ETW ecosystem, such as MGTEK TraceView Plus.

Features

  • Log Python logging.LogRecord via Event Tracing for Windows.
  • Includes standard metadata in ETW records, such as ETW provider, time-stamp, process and thread ID, and log-level.
  • Includes Python specific logging.LogRecord metadata in ETW records, such as Python module, function name, filename and line-number, and log message.
  • Automatic provider ID (GUID) generation via provider name hash.
  • Native WIN32 implementation with no dependecies.

Logging ETW events via Python

To log ETW events via the Python logging module you can write:

import logging
import pyetw

logging.basicConfig(level=logging.DEBUG, handlers=(pyetw.LoggerHandler(),))
logging.info("Hello from PyEtw!")

Note the parameter handlers to basicConfig(). By specifing the pyetw.LoggerHandler(), the log records are written as ETW TraceLogging events.

To record and view the traces, you can use any ETW tracing tool.

ETW Provider GUID hash

PyEtw uses the standard TraceLogging hashing algorithm to derive the provider GUID from the trace provider name, which is set to the Python logger name by default.

If your tracing tool is not able to generate the provider GUID from the provider name, you can use the guid property of the EventProvider class to obtain the GUID. For instance, to get the provider GUID for the root logger, enter the following command:

python -c "import pyetw;print(pyetw.EventProvider('root').guid)"

Recording ETW Traces

Here is an example that records the events of the root logger to a trace file using Tracelog, which is included in the Windows SDK

C:\Temp> tracelog -start mytrace -guid *root

Note that the specified GUID must match the name of the Python logger. Prefix the logger name with a star to have tracelog create a GUID hash of the logger name.

After you are done running the Python example from above, run

C:\Temp> tracelog -stop mytrace

When the trace is stopped, you will have a file LogFile.Etl that contains a single trace record.

To view the recorded trace, you can write

C:\Temp> tracefmt -displayonly LogFile.Etl

Installing PyEtw

You can install the PyEtw package from PyPI using pip:

pip install pyetw

In order to use the PyEtw package, you need Python 3.6 or higher.

The source code for the PyEtw package can be found at GitHub at https://github.com/mgtek/pyetw.

Documentation

You can find the PyEtw user's guide at https://pyetw.readthedocs.io/.

Examples

You can find Python examples using PyEtw in the pyetw GitHub repository at https://github.com/mgtek/pyetw/tree/main/examples.

Getting help

For issues with PyEtw, please visit the pyetw issue tracker.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyetw-1.1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

pyetw-1.1.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file pyetw-1.1.0.tar.gz.

File metadata

  • Download URL: pyetw-1.1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for pyetw-1.1.0.tar.gz
Algorithm Hash digest
SHA256 aee1a6e0798f1a1c05d5cfd389ca8f9d38b4c01c8272168576ffbd7e28b77dcb
MD5 5a335927872e08930b7f3dec84dd6afc
BLAKE2b-256 07b9c0ec015f2b2bfa0c5661792026f10e47da8c041f73c4a09bae59c5f492a6

See more details on using hashes here.

File details

Details for the file pyetw-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyetw-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for pyetw-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e2d1de12a914c89b41be055f574b4b666e0133cb90b89d07e2963a88abc5efa
MD5 78c0b92e23e346a180df5b2cd6d34e19
BLAKE2b-256 e7366bd45a72761abde23ccfd47933d4fdfbaecf9e889488c1848920ddfc459e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page