Skip to content

Commit

Permalink
Merge pull request #814 from opengisch/logging
Browse files Browse the repository at this point in the history
Add persistent logging functionality
  • Loading branch information
domi4484 authored Aug 14, 2023
2 parents a8802b7 + eb2e66e commit 972fe0f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
__pycache__
/QgisModelBaker/libili2db/bin
/QgisModelBaker/libs/
/QgisModelBaker/logs/
*.orig
i18n
.DS_Store
Expand Down
13 changes: 13 additions & 0 deletions QgisModelBaker/gui/panel/log_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
***************************************************************************/
"""

import logging

from PyQt5.QtWidgets import QGridLayout
from qgis.core import Qgis
from qgis.gui import QgsMessageBar
Expand Down Expand Up @@ -52,6 +54,17 @@ def print_info(self, text, text_color=LogColor.COLOR_INFO):
self.txtStdout.setTextColor(QColor(text_color))
self.txtStdout.append(text)

if text_color == LogColor.COLOR_INFO:
logging.info(text)
elif text_color == LogColor.COLOR_SUCCESS:
logging.info(text)
elif text_color == LogColor.COLOR_WARNING:
logging.warning(text)
elif text_color == LogColor.COLOR_FAIL:
logging.error(text)
elif text_color == LogColor.COLOR_TOPPING:
logging.info(text)

def on_stderr(self, text):
color_log_text(text, self.txtStdout)

Expand Down
20 changes: 17 additions & 3 deletions QgisModelBaker/gui/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* *
***************************************************************************/
"""
import logging
import os

from PyQt5.QtGui import QColor, QGuiApplication
Expand Down Expand Up @@ -321,9 +322,12 @@ def _run(self, edited_command=None):
self.progress_bar.setTextVisible(False)
self._disable_controls(True)
validator = ilivalidator.Validator()
if validator:
validator.tool = self.current_configuration.tool
validator.configuration = self.current_configuration

validator.stdout.connect(self._validator_stdout)
validator.stderr.connect(self._validator_stderr)

validator.tool = self.current_configuration.tool
validator.configuration = self.current_configuration

validator.configuration.ilimodels = ""
validator.configuration.dataset = ""
Expand Down Expand Up @@ -630,3 +634,13 @@ def _select_validator_config_file(self):
)
if filename:
self.validator_config_file_line_edit.setText(filename)

def _validator_stdout(self, txt):
lines = txt.strip().split("\n")
for line in lines:
logging.info(line)

def _validator_stderr(self, txt):
lines = txt.strip().split("\n")
for line in lines:
logging.error(line)
50 changes: 49 additions & 1 deletion QgisModelBaker/qgismodelbaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import configparser
import datetime
import locale
import logging
import logging.handlers
import os
import pathlib
import webbrowser
Expand All @@ -27,15 +29,18 @@
from qgis.core import QgsProject
from qgis.PyQt.QtCore import (
QCoreApplication,
QDir,
QEvent,
QFileInfo,
QLocale,
QObject,
QSettings,
QStandardPaths,
Qt,
QTranslator,
QUrl,
)
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtGui import QDesktopServices, QIcon
from qgis.PyQt.QtWidgets import QAction, QMessageBox
from qgis.utils import available_plugins

Expand Down Expand Up @@ -67,6 +72,7 @@ def __init__(self, iface):
self.__validate_action = None
self.__topping_wizard_action = None
self.__configure_action = None
self.__show_logs_folder_action = None
self.__help_action = None
self.__about_action = None
self.__dataset_selector_action = None
Expand All @@ -92,6 +98,9 @@ def __init__(self, iface):
settings.beginGroup("QgisModelBaker/ili2db")
self.ili2db_configuration.restore(settings)

self.logsDirectory = "{}/logs".format(basepath)
self._initLogger()

self.event_filter = DropFileFilter(self)

def register_event_filter(self):
Expand Down Expand Up @@ -156,6 +165,7 @@ def initGui(self):
self.__configure_action = QAction(self.tr("Settings"), None)
self.__infoseparator = QAction(None)
self.__infoseparator.setSeparator(True)
self.__show_logs_folder_action = QAction(self.tr("Show log folder"), None)
self.__help_action = QAction(self.tr("Help"), None)
self.__about_action = QAction(self.tr("About"), None)

Expand All @@ -172,6 +182,7 @@ def initGui(self):
self.show_workflow_wizard_dialog
)
self.__topping_wizard_action.triggered.connect(self.show_topping_wizard_dialog)
self.__show_logs_folder_action.triggered.connect(self.show_logs_folder)
self.__help_action.triggered.connect(self.show_help_documentation)
self.__about_action.triggered.connect(self.show_about_dialog)

Expand All @@ -194,6 +205,9 @@ def initGui(self):
self.tr("Model Baker"), self.__configure_action
)
self.iface.addPluginToDatabaseMenu(self.tr("Model Baker"), self.__infoseparator)
self.iface.addPluginToDatabaseMenu(
self.tr("Model Baker"), self.__show_logs_folder_action
)
self.iface.addPluginToDatabaseMenu(self.tr("Model Baker"), self.__help_action)
self.iface.addPluginToDatabaseMenu(self.tr("Model Baker"), self.__about_action)

Expand Down Expand Up @@ -236,6 +250,9 @@ def unload(self):
)
self.iface.removePluginDatabaseMenu(self.tr("Model Baker"), self.__help_action)
self.iface.removePluginDatabaseMenu(self.tr("Model Baker"), self.__about_action)
self.iface.removePluginDatabaseMenu(
self.tr("Model Baker"), self.__show_logs_folder_action
)
self.toolbar.removeAction(self.__dataset_selector_action)

self.iface.layerTreeView().currentLayerChanged.disconnect(
Expand All @@ -247,6 +264,7 @@ def unload(self):
del self.__configure_action
del self.__help_action
del self.__about_action
del self.__show_logs_folder_action
del self.__dataset_selector_action
del self.__dataset_selector
del self.__topping_wizard_action
Expand Down Expand Up @@ -329,6 +347,9 @@ def show_options_dialog(self):
settings.beginGroup("QgisModelBaker/ili2db")
self.ili2db_configuration.save(settings)

def show_logs_folder(self):
QDesktopServices.openUrl(QUrl.fromLocalFile(self.logsDirectory))

def show_help_documentation(self):
os_language = QLocale(QSettings().value("locale/userLocale")).name()[:2]
if os_language in ["es", "de"]:
Expand Down Expand Up @@ -458,6 +479,33 @@ def _set_dropped_file_configuration(self):
),
)

def _initLogger(self):
directory = QDir(self.logsDirectory)
if not directory.exists():
directory.mkpath(self.logsDirectory)

if directory.exists():
logfile = QFileInfo(directory, "ModelBaker.log")

# Handler for files rotation, create one log per day
rotationHandler = logging.handlers.TimedRotatingFileHandler(
logfile.filePath(), when="midnight", backupCount=10
)

# Configure logging
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(levelname)-7s %(message)s",
handlers=[rotationHandler],
)
else:
logging.error(
"Can't create log files directory '{}'.".format(self.logsDirectory)
)

logging.info("")
logging.info("Starting Model Baker plugin version {}".format(self.__version__))


class DropFileFilter(QObject):
def __init__(self, parent=None):
Expand Down

0 comments on commit 972fe0f

Please sign in to comment.