10

I run Python processing algorithms externally, and I want to get the log output I see when I run it from the QGIS 3 GUI at the log tab as shown in the screenshot:

enter image description here

I want to get the full grey log output when Result is returned only.

How do I do that?

An example follows:

import os
import sys
import gdal
gdal.UseExceptions() # Allow GDAL to throw Python Exceptions
from qgis.core import (
 QgsApplication,
 QgsProcessingFeedback,
 QgsMessageLog)
from qgis.analysis import QgsNativeAlgorithms
QgsApplication.setPrefixPath(os.path.join("C:", os.sep, "OSGeo4W64", "apps", "qgis"), True)
qgs = QgsApplication([], False)
qgs.initQgis()
sys.path.append(os.path.join("C:", os.sep, "OSGeo4W64", "apps", "qgis", "python", "plugins"))
import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
def pca(input_raster_list):
 params = {
 'input': input_raster_list,
 'rescale': [0,0],
 'percent': 99,
 '-n': True,
 '-f': False,
 'output': 'C:\\Users\\...',
 'GRASS_REGION_PARAMETER': None,
 'GRASS_REGION_CELLSIZE_PARAMETER': 0
 }
 feedback = QgsProcessingFeedback()
 res = processing.run("grass7:i.pca", params, feedback=feedback)
 print(res)
 return
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Feb 1, 2019 at 17:57
3
  • 1
    Can you post a small code sample showing how you are calling the algorithms? Commented Feb 1, 2019 at 18:57
  • Can the question be reopened now that more detail and specifications are included? It was supposed to be a generic question on how to get the logs from QGIS, but maybe now it is more clear. Commented Feb 3, 2019 at 21:04
  • 1
    Even "generic" questions about code should always include a code snippet that illustrates what you have tried and where you are stuck. Commented Feb 3, 2019 at 21:38

1 Answer 1

13

You can subclass QgsProcessingFeedback to implement your own custom logging logic. E.g.

class MyFeedBack(QgsProcessingFeedback):
 def setProgressText(self, text):
 print(text)
 def pushInfo(self, info):
 print(info)
 def pushCommandInfo(self, info):
 print(info)
 def pushDebugInfo(self, info):
 print(info)
 def pushConsoleInfo(self, info):
 print(info)
 def reportError(self, error, fatalError=False):
 print(error)

This one will just print everything to the console, but you could modify the logic to write to a file, etc.

Then, whenever you call processing.run, make sure you pass an instance of your subclass as the feedback argument:

res = processing.run("grass7:i.pca", params, feedback=MyFeedBack())
Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
answered Feb 3, 2019 at 23:17
1
  • 2
    Thank you once more Nyall! You are right that code is needed and necessary for any help. The solution is helpful and insightful. Commented Feb 4, 2019 at 15:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.