Implement logging

This commit is contained in:
BasioMeusPuga
2019-01-19 20:31:19 +05:30
parent 506c458544
commit a45e183914
10 changed files with 51 additions and 66 deletions

View File

@@ -23,8 +23,6 @@ import logging
import hashlib
import pathlib
from PyQt5 import QtWidgets, QtGui, QtCore
# This allows for the program to be launched from the
# dir where it's been copied instead of needing to be
# installed
@@ -32,20 +30,7 @@ install_dir = os.path.realpath(__file__)
install_dir = pathlib.Path(install_dir).parents[1]
sys.path.append(str(install_dir))
# Initialize logging
# This is outside the UI declaration to allow sharing
# the object across modules without explicit redeclaration
logger_filename = os.path.join(
QtCore.QStandardPaths.writableLocation(QtCore.QStandardPaths.AppDataLocation),
'Lector',
'Lector.log')
logging.basicConfig(
filename=logger_filename,
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.ERROR)
logger = logging.getLogger('lector.main')
from PyQt5 import QtWidgets, QtGui, QtCore
from lector import database
from lector import sorter
@@ -100,6 +85,10 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
Settings(self).read_settings() # This should populate all variables that need
# to be remembered across sessions
# Initialize logging
self.logger = init_logging(self.settings['log_level'])
self.logger.log(60, 'Application started')
# Initialize icon factory
self.QImageFactory = QImageFactory(self)
@@ -256,7 +245,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
# Get list of available parsers
self.available_parsers = '*.' + ' *.'.join(sorter.available_parsers)
logger.info('Available parsers: ' + self.available_parsers)
self.logger.info('Available parsers: ' + self.available_parsers)
# The Library tab gets no button
self.tabWidget.tabBar().setTabButton(
@@ -423,7 +412,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
if not file_paths:
return
logger.info(
self.logger.info(
'Attempting to open: ' + ', '.join(file_paths))
contents = sorter.BookSorter(
@@ -437,7 +426,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
# Notification feedback in case all books return nothing
if not contents:
logger.error('No parseable files found')
self.logger.error('No parseable files found')
return
successfully_opened = []
@@ -447,7 +436,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
file_data = contents[i]
Tab(file_data, self)
successfully_opened.append(file_data['path'])
logger.info(
self.logger.info(
'Successfully opened: ' + ', '.join(file_paths))
if self.settings['last_open_tab'] == 'library':
@@ -1049,6 +1038,21 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
QtWidgets.qApp.exit()
def init_logging(log_level):
location_prefix = QtCore.QStandardPaths.writableLocation(
QtCore.QStandardPaths.AppDataLocation)
os.makedirs(location_prefix, exist_ok=True)
logger_filename = os.path.join(location_prefix, 'Lector.log')
logging.basicConfig(
filename=logger_filename,
filemode='a',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%Y/%M/%d %H:%M:%S',
level=log_level)
logging.addLevelName(60, 'HammerTime') ## Messages that MUST be logged
return logging.getLogger('lector.main')
def main():
app = QtWidgets.QApplication(sys.argv)
app.setApplicationName('Lector') # This is needed for QStandardPaths
@@ -1063,8 +1067,7 @@ def main():
translations_out_string = '(Translations found)'
if not translations_found:
translations_out_string = '(No translations found)'
log_string = f'Locale: {QtCore.QLocale.system().name()}' + translations_out_string
logger.info(log_string)
print(f'Locale: {QtCore.QLocale.system().name()}' + translations_out_string)
form = MainUI()
form.show()
@@ -1073,5 +1076,4 @@ def main():
if __name__ == '__main__':
logger.info('Application start')
main()