Redirect python logging in debug mode (#642)

This is handy for debugging what Glean and other low-level libraries
used by mozregression are doing.
This commit is contained in:
William Lachance 2020-05-22 09:42:17 -04:00 коммит произвёл GitHub
Родитель e31e5f5e29
Коммит 5b810bb465
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 23 добавлений и 0 удалений

Просмотреть файл

@ -4,6 +4,8 @@ from mozlog.structuredlog import StructuredLogger, set_default_logger
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QApplication
from mozregression.log import init_python_redirect_logger
from .check_release import CheckRelease
from .crash_reporter import CrashReporter
from .global_prefs import set_default_prefs
@ -13,7 +15,9 @@ from .mainwindow import MainWindow
def main():
logger = StructuredLogger("mozregression-gui")
init_python_redirect_logger(logger)
set_default_logger(logger)
# Create a Qt application
log_model = LogModel()
logger.add_handler(log_model)

Просмотреть файл

@ -4,6 +4,7 @@ Logging and outputting configuration and utilities.
from __future__ import absolute_import
import logging
import sys
import time
@ -22,6 +23,22 @@ def _format_seconds(total):
return "%2d:%05.2f" % (minutes, seconds)
def init_python_redirect_logger(logger):
"""Create a pipe to the main log, at debug level
This is so we can see logs from third party libraries (like Glean)
"""
class RedirectHandler(logging.StreamHandler):
def emit(self, record):
logger.debug("{}: {}".format(record.name, str(record.msg) % record.args))
rh = RedirectHandler()
rh.setLevel(logging.DEBUG)
logging.root.addHandler(rh)
logging.root.setLevel(logging.DEBUG)
def init_logger(debug=True, allow_color=ALLOW_COLOR, output=None):
"""
Initialize the mozlog logger. Must be called once before using logs.
@ -56,6 +73,8 @@ def init_logger(debug=True, allow_color=ALLOW_COLOR, output=None):
handler = LogLevelFilter(StreamHandler(output, format_log), "debug" if debug else "info")
logger.add_handler(handler)
init_python_redirect_logger(logger)
set_default_logger(logger)
return logger