Ejemplo n.º 1
0
    def _log(self, message: str, level: LoggerLevel) -> None:
        """
        Print message to QGIS log, depending on release or debug mode.

        Print either to the QGIS error console, if in release mode,
        (https://qgis.org/pyqgis/master/core/QgsLogger.html#qgis.core.QgsLogger)
        or to the QGIS GUI message logger (Log Message Panel)
        (https://qgis.org/pyqgis/master/core/QgsMessageLog.html#qgis.core.QgsMessageLog).
        Always use the above thread-safe classess to print out messages.
        """
        message = str(message)
        # Get caller info
        callerframerecord = inspect.stack()[1]
        frame = callerframerecord[0]
        info = inspect.getframeinfo(frame)
        # prepare level message
        msg = 'file=%s, func=%s, line=%s: %s' % (
            info.filename,
            info.function,
            str(info.lineno),
            message)
        # Print to Log Message Panel
        # (https://qgis.org/pyqgis/master/core/QgsMessageLog.html#qgis.core.QgsMessageLog.logMessage)
        if self.debug_flag:
            if level == self.DEBUG:
                QgsMessageLog.logMessage(msg, self.tag, level=Qgis.Info)
            elif level == self.INFO:
                QgsMessageLog.logMessage(msg, self.tag, level=Qgis.Success)
            elif level == self.ERROR:
                QgsMessageLog.logMessage(msg, self.tag, level=Qgis.Critical)
            elif level == self.WARNING:
                QgsMessageLog.logMessage(msg, self.tag, level=Qgis.Warning)
            else:
                raise Exception('Invalid log level: %s' % level)
        # Print to QGIS Python console
        else:
            # Do not use QgsLogger.fatal(msg), as it will crash QGIS process
            if level == self.ERROR:
                QgsLogger.critical(msg)
            elif level == self.WARNING:
                QgsLogger.warning(msg)
            elif level in (self.DEBUG, self.INFO):
                QgsLogger.debug(
                    msg=message, file=info.filename,
                    function=info.function, line=info.lineno)
        # always print to QGIS log file
        # must configure the QGIS_LOG_FILE environment variable
        QgsLogger.logMessageToFile("Picterra plugin - " + msg)
Ejemplo n.º 2
0
def write_qgs_log_to_file(message, tag, level):
    logfile = QgsLogger.logFile()
    if logfile is not None:
        QgsLogger.logMessageToFile('{}: {}({}): {} '.format('%s'%(returnunicode(get_date_time())), returnunicode(tag), returnunicode(level), '%s'%(returnunicode(message))))