source: sasview/src/sas/qtgui/Utilities/SasviewLogger.py

ESS_GUI
Last change on this file was e4335ae, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 6 years ago

SASVIEW-957 logging changes (#158)

  • rework XStream to continue to write to stdout/stderr alongside redirection; make logging level setting consistent; make log configuration more consistent
  • rm XStream; QT signal in QtHandler? logging handler; only logs in Log Explorer (no stdout/stderr)
  • no need to change handler level
  • use QTextBrowser.append to facilitate auto-scrolling in the Log Explorer
  • modify logger unit test to reflect changes (passes)
  • Property mode set to 100644
File size: 801 bytes
Line 
1import os
2import sys
3import logging
4
5from PyQt5.QtCore import *
6
7
8class QtHandler(QObject, logging.Handler):
9    """
10    Version of logging handler "emitting" the message to custom stdout()
11    """
12    messageWritten = pyqtSignal(str)
13
14    def __init__(self):
15        QObject.__init__(self)
16        logging.Handler.__init__(self)
17
18    def emit(self, record):
19        record = self.format(record)
20        if record:
21            self.messageWritten.emit('%s\n'%record)
22
23
24def setup_qt_logging():
25    # Define the default logger
26    logger = logging.getLogger()
27
28    # Add the qt-signal logger
29    handler = QtHandler()
30    handler.setFormatter(logging.Formatter(
31        fmt="%(asctime)s - %(levelname)s: %(message)s",
32        datefmt="%H:%M:%S"
33    ))
34    logger.addHandler(handler)
35
36    return handler
Note: See TracBrowser for help on using the repository browser.