allow changing log level

This commit is contained in:
dries.k
2022-01-29 17:02:39 +01:00
parent bda2822fed
commit 99aa90985f
8 changed files with 152 additions and 45 deletions

View File

@@ -1,39 +1,5 @@
import logging from gotify_tray import start_gui
import os
import sys
from PyQt6 import QtCore, QtGui, QtWidgets
from gotify_tray.__version__ import __title__
from gotify_tray.utils import verify_server
if __name__ == "__main__": if __name__ == "__main__":
title = __title__.replace(" ", "-") start_gui()
app = QtWidgets.QApplication(sys.argv)
app.setApplicationName(title)
app.setQuitOnLastWindowClosed(False)
app.setWindowIcon(QtGui.QIcon("gotify_tray/gui/images/gotify-small.png"))
app.setStyle("fusion")
logdir = QtCore.QStandardPaths.standardLocations(
QtCore.QStandardPaths.StandardLocation.AppDataLocation
)[0]
if not os.path.exists(logdir):
os.mkdir(logdir)
logging.basicConfig(
filename=os.path.join(logdir, f"{title}.log"),
format="%(levelname)s > %(name)s > %(asctime)s > %(message)s",
level=logging.ERROR,
)
# import from gui has to happen after 'setApplicationName' to make sure the correct cache directory is created
from gotify_tray.gui import MainWindow
window = MainWindow(app)
# prevent multiple instances
if (window.acquire_lock() or "--no-lock" in sys.argv) and verify_server():
window.init_ui()
sys.exit(app.exec())

View File

@@ -0,0 +1 @@
from .gui import start_gui

View File

@@ -1,4 +1,5 @@
DEFAULT_SETTINGS = { DEFAULT_SETTINGS = {
"logging/level": "Disabled",
"MainWindow/start_minimized": True, "MainWindow/start_minimized": True,
"MainWindow/theme": "default", "MainWindow/theme": "default",
"MainWidget/status_image/size": 28, "MainWidget/status_image/size": 28,

View File

@@ -1,39 +1,47 @@
import getpass import getpass
import logging import logging
import os import os
import sys
import tempfile import tempfile
from typing import List from typing import List
from gotify_tray import gotify from gotify_tray import gotify
from gotify_tray.__version__ import __title__
from gotify_tray.database import Downloader, Settings from gotify_tray.database import Downloader, Settings
from gotify_tray.tasks import ( from gotify_tray.tasks import (
DeleteAllMessagesTask,
DeleteApplicationMessagesTask, DeleteApplicationMessagesTask,
DeleteMessageTask, DeleteMessageTask,
DeleteAllMessagesTask,
GetApplicationMessagesTask, GetApplicationMessagesTask,
GetApplicationsTask, GetApplicationsTask,
GetMessagesTask, GetMessagesTask,
) )
from gotify_tray.utils import verify_server
from PyQt6 import QtCore, QtGui, QtWidgets from PyQt6 import QtCore, QtGui, QtWidgets
from ..__version__ import __title__ from ..__version__ import __title__
from .ApplicationModel import ( from .ApplicationModel import (
ApplicationItemDataRole,
ApplicationAllMessagesItem, ApplicationAllMessagesItem,
ApplicationItemDataRole,
ApplicationModel, ApplicationModel,
ApplicationModelItem, ApplicationModelItem,
) )
from .designs.widget_main import Ui_Form as Ui_Main from .designs.widget_main import Ui_Form as Ui_Main
from .themes import set_theme
from .MessagesModel import MessageItemDataRole, MessagesModel, MessagesModelItem from .MessagesModel import MessageItemDataRole, MessagesModel, MessagesModelItem
from .MessageWidget import MessageWidget from .MessageWidget import MessageWidget
from .SettingsDialog import SettingsDialog from .SettingsDialog import SettingsDialog
from .themes import set_theme
from .Tray import Tray from .Tray import Tray
settings = Settings("gotify-tray") settings = Settings("gotify-tray")
logger = logging.getLogger("logger") logger = logging.getLogger("logger")
downloader = Downloader() downloader = Downloader()
if (level := settings.value("logging/level", type=str)) != "Disabled":
logger.setLevel(level)
else:
logging.disable()
class MainWidget(QtWidgets.QWidget, Ui_Main): class MainWidget(QtWidgets.QWidget, Ui_Main):
def __init__( def __init__(
@@ -460,3 +468,33 @@ class MainWindow(QtWidgets.QMainWindow):
self.gotify_client.stop() self.gotify_client.stop()
super(MainWindow, self).closeEvent(e) super(MainWindow, self).closeEvent(e)
self.app.quit() self.app.quit()
def start_gui():
title = __title__.replace(" ", "-")
app = QtWidgets.QApplication(sys.argv)
app.setApplicationName(title)
app.setQuitOnLastWindowClosed(False)
app.setWindowIcon(QtGui.QIcon("gotify_tray/gui/images/gotify-small.png"))
app.setStyle("fusion")
logdir = QtCore.QStandardPaths.standardLocations(
QtCore.QStandardPaths.StandardLocation.AppDataLocation
)[0]
if not os.path.exists(logdir):
os.mkdir(logdir)
logging.basicConfig(
filename=os.path.join(logdir, f"{title}.log"),
format="%(levelname)s > %(name)s > %(asctime)s > %(message)s",
)
# import from gui has to happen after 'setApplicationName' to make sure the correct cache directory is created
from gotify_tray.gui import MainWindow
window = MainWindow(app)
# prevent multiple instances
if (window.acquire_lock() or "--no-lock" in sys.argv) and verify_server():
window.init_ui()
sys.exit(app.exec())

View File

@@ -1,3 +1,6 @@
import logging
import webbrowser
from gotify_tray.database import Settings from gotify_tray.database import Settings
from gotify_tray.utils import verify_server from gotify_tray.utils import verify_server
from PyQt6 import QtCore, QtGui, QtWidgets from PyQt6 import QtCore, QtGui, QtWidgets
@@ -5,6 +8,8 @@ from PyQt6 import QtCore, QtGui, QtWidgets
from .designs.widget_settings import Ui_Dialog from .designs.widget_settings import Ui_Dialog
from .themes import set_theme from .themes import set_theme
logger = logging.getLogger("logger")
settings = Settings("gotify-tray") settings = Settings("gotify-tray")
@@ -56,6 +61,18 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
settings.value("tray/notifications/duration_ms", type=int) settings.value("tray/notifications/duration_ms", type=int)
) )
# Logging
self.combo_logging.addItems(
[
logging.getLevelName(logging.ERROR),
logging.getLevelName(logging.WARNING),
logging.getLevelName(logging.INFO),
logging.getLevelName(logging.DEBUG),
"Disabled",
]
)
self.combo_logging.setCurrentText(settings.value("logging/level", type=str))
def set_font_labels(self): def set_font_labels(self):
self.label_font_message_title.setText( self.label_font_message_title.setText(
settings.value("MessageWidget/font/title", type=str) settings.value("MessageWidget/font/title", type=str)
@@ -133,6 +150,12 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
# Server info # Server info
self.pb_change_server_info.clicked.connect(self.change_server_info_callback) self.pb_change_server_info.clicked.connect(self.change_server_info_callback)
# Logging
self.combo_logging.currentTextChanged.connect(self.settings_changed_callback)
self.pb_open_log.clicked.connect(
lambda: webbrowser.open(logger.root.handlers[0].baseFilename)
)
def apply_settings(self): def apply_settings(self):
# Fonts # Fonts
settings.setValue( settings.setValue(
@@ -162,6 +185,15 @@ class SettingsDialog(QtWidgets.QDialog, Ui_Dialog):
settings.setValue("tray/notifications/priority", self.spin_priority.value()) settings.setValue("tray/notifications/priority", self.spin_priority.value())
settings.setValue("tray/notifications/duration_ms", self.spin_duration.value()) settings.setValue("tray/notifications/duration_ms", self.spin_duration.value())
# Logging
selected_level = self.combo_logging.currentText()
settings.setValue("logging/level", selected_level)
if selected_level == "Disabled":
logging.disable(logging.CRITICAL)
else:
logging.disable(logging.NOTSET)
logger.setLevel(selected_level)
self.settings_changed = False self.settings_changed = False
self.buttonBox.button( self.buttonBox.button(
QtWidgets.QDialogButtonBox.StandardButton.Apply QtWidgets.QDialogButtonBox.StandardButton.Apply

View File

@@ -1,2 +1,2 @@
from .MainWindow import MainWindow from .MainWindow import MainWindow, start_gui
from .ServerInfoDialog import ServerInfoDialog from .ServerInfoDialog import ServerInfoDialog

View File

@@ -1,6 +1,6 @@
# Form implementation generated from reading ui file 'gotify_tray/gui/designs\widget_settings.ui' # Form implementation generated from reading ui file 'gotify_tray/gui/designs\widget_settings.ui'
# #
# Created by: PyQt6 UI code generator 6.1.1 # Created by: PyQt6 UI code generator 6.2.1
# #
# WARNING: Any manual changes made to this file will be lost when pyuic6 is # WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing. # run again. Do not edit this file unless you know what you are doing.
@@ -12,7 +12,7 @@ from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object): class Ui_Dialog(object):
def setupUi(self, Dialog): def setupUi(self, Dialog):
Dialog.setObjectName("Dialog") Dialog.setObjectName("Dialog")
Dialog.resize(375, 540) Dialog.resize(375, 606)
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog) self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
self.verticalLayout.setObjectName("verticalLayout") self.verticalLayout.setObjectName("verticalLayout")
self.groupBox = QtWidgets.QGroupBox(Dialog) self.groupBox = QtWidgets.QGroupBox(Dialog)
@@ -119,6 +119,23 @@ class Ui_Dialog(object):
spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum) spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
self.gridLayout_3.addItem(spacerItem3, 0, 1, 1, 1) self.gridLayout_3.addItem(spacerItem3, 0, 1, 1, 1)
self.verticalLayout.addWidget(self.groupBox_4) self.verticalLayout.addWidget(self.groupBox_4)
self.groupBox_7 = QtWidgets.QGroupBox(Dialog)
self.groupBox_7.setObjectName("groupBox_7")
self.gridLayout_6 = QtWidgets.QGridLayout(self.groupBox_7)
self.gridLayout_6.setObjectName("gridLayout_6")
self.label_7 = QtWidgets.QLabel(self.groupBox_7)
self.label_7.setObjectName("label_7")
self.gridLayout_6.addWidget(self.label_7, 0, 0, 1, 1)
self.combo_logging = QtWidgets.QComboBox(self.groupBox_7)
self.combo_logging.setObjectName("combo_logging")
self.gridLayout_6.addWidget(self.combo_logging, 0, 1, 1, 1)
self.pb_open_log = QtWidgets.QPushButton(self.groupBox_7)
self.pb_open_log.setMaximumSize(QtCore.QSize(30, 16777215))
self.pb_open_log.setObjectName("pb_open_log")
self.gridLayout_6.addWidget(self.pb_open_log, 0, 2, 1, 1)
spacerItem4 = QtWidgets.QSpacerItem(190, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
self.gridLayout_6.addItem(spacerItem4, 0, 3, 1, 1)
self.verticalLayout.addWidget(self.groupBox_7)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog) self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setOrientation(QtCore.Qt.Orientation.Horizontal) self.buttonBox.setOrientation(QtCore.Qt.Orientation.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.StandardButton.Apply|QtWidgets.QDialogButtonBox.StandardButton.Cancel|QtWidgets.QDialogButtonBox.StandardButton.Ok) self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.StandardButton.Apply|QtWidgets.QDialogButtonBox.StandardButton.Cancel|QtWidgets.QDialogButtonBox.StandardButton.Ok)
@@ -126,8 +143,8 @@ class Ui_Dialog(object):
self.verticalLayout.addWidget(self.buttonBox) self.verticalLayout.addWidget(self.buttonBox)
self.retranslateUi(Dialog) self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept) self.buttonBox.accepted.connect(Dialog.accept) # type: ignore
self.buttonBox.rejected.connect(Dialog.reject) self.buttonBox.rejected.connect(Dialog.reject) # type: ignore
QtCore.QMetaObject.connectSlotsByName(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog)
Dialog.setTabOrder(self.pb_font_message_title, self.pb_font_message_date) Dialog.setTabOrder(self.pb_font_message_title, self.pb_font_message_date)
Dialog.setTabOrder(self.pb_font_message_date, self.pb_font_message_content) Dialog.setTabOrder(self.pb_font_message_date, self.pb_font_message_content)
@@ -163,6 +180,10 @@ class Ui_Dialog(object):
self.label_6.setText(_translate("Dialog", "ms")) self.label_6.setText(_translate("Dialog", "ms"))
self.groupBox_4.setTitle(_translate("Dialog", "Server info")) self.groupBox_4.setTitle(_translate("Dialog", "Server info"))
self.pb_change_server_info.setText(_translate("Dialog", "Change server info")) self.pb_change_server_info.setText(_translate("Dialog", "Change server info"))
self.groupBox_7.setTitle(_translate("Dialog", "Logging"))
self.label_7.setText(_translate("Dialog", "Level"))
self.pb_open_log.setToolTip(_translate("Dialog", "Open logfile"))
self.pb_open_log.setText(_translate("Dialog", "..."))
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -7,7 +7,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>375</width> <width>375</width>
<height>540</height> <height>606</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@@ -279,6 +279,54 @@
</layout> </layout>
</widget> </widget>
</item> </item>
<item>
<widget class="QGroupBox" name="groupBox_7">
<property name="title">
<string>Logging</string>
</property>
<layout class="QGridLayout" name="gridLayout_6">
<item row="0" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Level</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="combo_logging"/>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="pb_open_log">
<property name="maximumSize">
<size>
<width>30</width>
<height>16777215</height>
</size>
</property>
<property name="toolTip">
<string>Open logfile</string>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="3">
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>190</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item> <item>
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation"> <property name="orientation">