Last active
October 23, 2024 04:38
-
-
Save thbaumann/d473f60ad0649160d08dbbbac0035c78 to your computer and use it in GitHub Desktop.
central logging of qgis logs of different users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt5.QtSql import QSqlDatabase, QSqlQuery | |
from PyQt5.QtCore import Qt, QTimer, QDateTime | |
from qgis.core import QgsApplication, Qgis | |
from qgis.utils import plugins | |
import os | |
import traceback | |
# Variable to hold the connection | |
message_log_connection = QgsApplication.messageLog() | |
qgis_version = Qgis.QGIS_VERSION.split('-')[0] | |
# Disconnect if already connected | |
try: | |
message_log_connection.messageReceived.disconnect(connection_pointer) | |
except: | |
pass #print("error") | |
# Define the connection parameters | |
conn_info = { | |
'host': 'my_server_adress', | |
'port': '5432', | |
'dbname': 'qgis_log', | |
'user': 'qgis_log', | |
'password': 'qgis_log' | |
} | |
# Create a list to hold the log messages | |
log_messages = [] | |
# Get the username from an environment variable | |
username = os.getenv('USERNAME') # Replace 'USERNAME' with your actual environment variable name | |
# Define the log levels | |
levels = ['Info', 'Warning', 'Critical', 'Success'] | |
timestamp_script_run = QDateTime.currentDateTime().toString(Qt.ISODate) | |
# Function to escape single quotes in a string | |
def escape_quotes(value): | |
return value.replace("'", "''") | |
# Function to handle new log messages | |
def logMessage(message, tag, level): | |
timestamp = QDateTime.currentDateTime().toString(Qt.ISODate) | |
plugin_name = message.split("::")[0] # Adjust this line based on the actual format of your log messages | |
#print(message) | |
log_entry = { | |
'timestamp': timestamp, | |
'username': username, | |
'qgis_version': qgis_version, | |
'plugin_name': escape_quotes(plugin_name), | |
'message': escape_quotes(message), | |
'tag': escape_quotes(tag), | |
'level': levels[level] # Convert the level to its string representation | |
} | |
log_messages.append(log_entry) | |
# Function to write log messages to PostgreSQL and clear the list | |
def writeLogsToDatabase(): | |
#print(log_messages) | |
if log_messages: | |
try: | |
# Initialize the database connection | |
db = QSqlDatabase.addDatabase('QPSQL') | |
db.setHostName(conn_info['host']) | |
db.setPort(int(conn_info['port'])) | |
db.setDatabaseName(conn_info['dbname']) | |
db.setUserName(conn_info['user']) | |
db.setPassword(conn_info['password']) | |
if not db.open(): | |
print("Failed to create connection with given parameters.") | |
return | |
#else: | |
#print("Connection to the database succeeded.") | |
query = QSqlQuery(db) | |
logs_to_insert = log_messages.copy() | |
log_messages.clear() | |
for log in logs_to_insert: | |
#print(log) | |
insert_query = """ | |
INSERT INTO qgis_log_table (timestamp, username, qgis_version, plugin_name, message, tag, level) | |
VALUES (CURRENT_TIMESTAMP, :username, :qgis_version, :plugin_name, :message, :tag, :level) | |
""" | |
query.prepare(insert_query) | |
query.bindValue(":username", log['username']) | |
query.bindValue(":qgis_version", log['qgis_version']) | |
query.bindValue(":plugin_name", log['plugin_name']) | |
query.bindValue(":message", log['message']) | |
query.bindValue(":tag", log['tag']) | |
query.bindValue(":level", log['level']) | |
# Print the SQL statement for debugging | |
#formatted_query = insert_query.replace(':username', f"'{log['username']}'").replace(':qgis_version', f"'{log['qgis_version']}'").replace(':plugin_name', f"'{log['plugin_name']}'").replace(':message', f"'{log['message']}'").replace(':tag', f"'{log['tag']}'").replace(':level', f"'{log['level']}'") | |
#print("Executing SQL:", formatted_query) | |
if not query.exec_(): | |
print("Failed to execute the insert query.") | |
#print("Error:", query.lastError().text()) | |
#else: | |
#print("Insert query executed successfully.") | |
#log_messages.clear() | |
db.close() | |
except Exception as e: | |
print(f"Failed to save log messages to the database: {e}") | |
traceback.print_exc() | |
pass | |
try: | |
connection_pointer = message_log_connection.messageReceived.connect(logMessage) | |
except: | |
print("unable to connect to signal") | |
# Connect the function to the messageReceived signal using the variable | |
#connection_pointer = message_log_connection.messageReceived.connect(logMessage) | |
# Set up a timer to write logs to the database every 5 seconds (5000 milliseconds) for testing | |
timer = QTimer() | |
timer.timeout.connect(writeLogsToDatabase) | |
timer.start(300000) | |
# To simulate a log message for testing | |
#message_log_connection.logMessage("ExamplePlugin::This is a test message", "Test", Qgis.Info) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment