Welcome, guest | Sign In | My Account | Store | Cart

This is a custom logging.Handler class that lets you use standard logging calls to output messages to SysInternals' DebugView utility.

Python, 20 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# DbgViewHandler for Python's standard logging module
# For information on DebugView see http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx

import logging
from win32api import OutputDebugString

class DbgViewHandler(logging.Handler):
    def __init__(self):
        logging.Handler.__init__(self)
        
    def emit(self, record):
        OutputDebugString(self.format(record))

        
if __name__ == '__main__':     
    
    # example
    log = logging.getLogger()
    log.addHandler(DbgViewHandler())
    log.warn('test a warning message')   

DebugView is a windows utility from the SysInternals suite that provides a console to capture debug output form application and kernel components running in windows. It has a number of features including filtering, pattern-matched highlighting and the ability to capture log messages from remote machines on network. Full information is available here: http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx

Writing to DebugView from Python is easy with the Pywin32 extensions (via the win32api.OutputDebugString function), and here is a Handler class that integrates it with the standard logging module.

1 comment

Gabriel Genellina 14 years, 1 month ago  # | flag

Very useful!

I'd just remove the __init__ method, as it adds nothing (and actually has a wrong signature: the Handler class takes an optional argument).

One may avoid the dependency with pywin32 by using ctypes; just replace the import line with:

import ctypes
OutputDebugString = ctypes.windll.kernel32.OutputDebugStringA
OutputDebugString.argtypes = [ctypes.c_char_p]