-
-
Save ngbrown/d38064a844426a00fdaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
# logging to DbgView with OutputDebugString | |
# from https://gist.github.com/ngbrown/d38064a844426a00fdaa and https://gist.github.com/wh13371/92df4715fc17eb74299d | |
import logging | |
import ctypes | |
# output "logging" messages to DbgView via OutputDebugString (Windows only!) | |
OutputDebugStringW = ctypes.windll.kernel32.OutputDebugStringW | |
OutputDebugStringW.argtypes = [ctypes.c_wchar_p] | |
class DbgViewHandler(logging.Handler): | |
def emit(self, record): | |
OutputDebugStringW(self.format(record)) | |
def config_logging(): | |
# format | |
fmt = logging.Formatter(fmt='%(levelname)-8s %(name)s:%(lineno)d %(funcName)s() - %(message)s', datefmt='%Y:%m:%d %H:%M:%S') | |
rootLogger = logging.getLogger('') | |
rootLogger.setLevel(logging.NOTSET) | |
# "OutputDebugString\DebugView" | |
ods = DbgViewHandler() | |
ods.setLevel(logging.NOTSET) | |
ods.setFormatter(fmt) | |
rootLogger.addHandler(ods) | |
# "Console" | |
con = logging.StreamHandler() | |
con.setLevel(logging.DEBUG) | |
con.setFormatter(fmt) | |
rootLogger.addHandler(con) | |
# in each file you want to use, do this: | |
import logging | |
log = logging.getLogger(__name__) | |
#log.addHandler(logging.NullHandler()) # include if you don't want a "No handlers could be found" message when config_logging() isn't called | |
def main(): | |
log.debug("debug message") | |
log.info("info message") | |
log.warn("warn message") | |
log.error("error message") | |
log.critical("critical message") | |
if __name__ == '__main__': | |
config_logging() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment