french_locator_filter.toolbelt.log_handler module

class french_locator_filter.toolbelt.log_handler.PlgLogger(level=0)[source]

Bases: Handler

Python logging handler supercharged with QGIS useful methods.

static log(message: str, application: str = 'French Locator Filter', log_level: qgis.core.Qgis.MessageLevel | Literal[0, 1, 2, 3, 4] = qgis.core.Qgis.MessageLevel.Info, push: bool = False, duration: int | None = None, button: bool = False, button_text: str | None = None, button_more_text: str | None = None, button_connect: Callable | None = None, parent_location: qgis.PyQt.QtWidgets.QWidget | None = None)[source]

Send messages to QGIS messages windows and to the user as a message bar. Plugin name is used as title. If debug mode is disabled, only warnings (1) and errors (2) or with push are sent.

Parameters:
  • message (str) – message to display

  • application (str, optional) – name of the application sending the message. Defaults to __about__.__title__

  • log_level (Union[Qgis.MessageLevel, Literal[0, 1, 2, 3, 4]], optional) – message level. Possible values: any values of enum Qgis.MessageLevel. For legacy purposes, it’s also possible to pass corresponding integers but it’s not recommended anymore. Legacy values: 0 (info), 1 (warning), 2 (critical), 3 (success), 4 (none - grey). Defaults to Qgis.MessageLevel(0) (info)

  • push (bool, optional) – also display the message in the QGIS message bar in addition to the log, defaults to False

  • duration (int, optional) – duration of the message in seconds. If not set, the duration is calculated from the log level: (log_level + 1) * 3. seconds. If set to 0, then the message must be manually dismissed by the user. Defaults to None.

  • button (bool, optional) – display a button in the message bar. Defaults to False.

  • button_text (str, optional) – text label of the button. Defaults to None.

  • button_more_text (str, optional) – text to display within the QgsMessageOutput

  • button_connect (Callable, optional) – function to be called when the button is pressed. If not set, a simple dialog (QgsMessageOutput) is used to dislay the message. Defaults to None.

  • parent_location (Widget, optional) – parent location widget. If not set, QGIS canvas message bar is used to push message, otherwise if a QgsMessageBar is available in parent_location it is used instead. Defaults to None.

Example:

# using enums from Qgis:
# Qgis.Info, Qgis.MessageLevel.Warning, Qgis.MessageLevel.Critical, Qgis.MessageLevel.Success, Qgis.MessageLevel.NoLevel
from qgis.core import Qgis

log(message="Plugin loaded - INFO", log_level=Qgis.MessageLevel.Info, push=False)
log(
    message="Something went wrong but it's not blocking",
    log_level=Qgis.MessageLevel.Warning
)
log(
    message="Plugin failed to load - CRITICAL",
    log_level=Qgis.MessageLevel(2),
    push=True
)

# LEGACY - using integers:
log(message="Plugin loaded - INFO", log_level=0, push=False)
log(message="Plugin loaded - WARNING", log_level=Qgis.MessageLevel.Warning, push=1, duration=5)
log(message="Plugin loaded - ERROR", log_level=2, push=1, duration=0)
log(
    message="Plugin loaded - SUCCESS",
    log_level=Qgis.MessageLevel.Success,
    push=1,
    duration=10,
    button=True
)
log(
    message="Plugin loaded",
    log_level=2,
    push=1,
    duration=0
    button=True,
    button_label=self.tr("See details"),
    button_more_text=detailed_error_message
)
log(message="Plugin loaded - TEST", log_level=Qgis.MessageLevel.NoLevel, push=0)