Refactor logging setup across multiple modules to use loguru with consistent configuration

- Updated logging initialization in MessageCalendar, admin_edit_prof, elsa_main, graph, iconLine, searchPage, and richtext modules to use loguru.
- Changed log rotation and retention settings for log files to improve log management.
- Replaced logger.debug/info calls with log.debug/info for consistency.
- Fixed a typo in the searchPage UI and updated related references in the UI files.
- Removed unused imports and cleaned up code for better readability.
This commit is contained in:
2025-05-13 15:49:52 +02:00
parent 4a3a95623a
commit f7c499ea6e
32 changed files with 412 additions and 491 deletions

View File

@@ -24,18 +24,14 @@ from src.logic import ApparatData, BookData, Prof, Apparat, ELSA
from src.logic.constants import SEMAP_MEDIA_ACCOUNTS
from .semester import Semester
from string import ascii_lowercase as lower, digits, punctuation
import loguru
import sys
from loguru import logger as log
logger = log
logger.remove()
logger.add("logs/application.log", rotation="1 week", retention="1 month", enqueue=True)
log.add(
"logs/database.log",
)
log = loguru.logger
log.remove()
log.add(sys.stdout)
log.add("logs/application.log", rotation="1 MB", retention="10 days")
# logger.add(sys.stderr, format="{time} {level} {message}", level="INFO")
logger.add(sys.stdout)
ascii_lowercase = lower + digits + punctuation
@@ -58,7 +54,7 @@ class Database:
if db_path is None:
self.db_path = self.database.path + self.database.name
self.db_path = self.db_path.replace("~", str(Path.home()))
logger.debug(self.db_path)
log.debug(self.db_path)
else:
self.db_path = db_path
self.checkDatabaseStatus()
@@ -69,10 +65,10 @@ class Database:
path = os.path.abspath(path)
if not os.path.exists(path):
# create path
# logger.debug(path)
# log.debug(path)
os.makedirs(path)
if self.get_db_contents() == []:
logger.critical("Database does not exist, creating tables")
log.critical("Database does not exist, creating tables")
self.create_tables()
self.insertSubjects()
@@ -155,12 +151,12 @@ class Database:
"""
conn = self.connect()
cursor = conn.cursor()
logger.debug(f"Inserting {params} into database with query {query}")
log.debug(f"Inserting {params} into database with query {query}")
cursor.execute(query, params)
conn.commit()
self.close_connection(conn)
@logger.catch
@log.catch
def query_db(
self,
query: str,
@@ -195,7 +191,7 @@ class Database:
# log_message = f"Querying database with query {query}"
if "INTO user" in query:
log_message = f"Querying database with query {query}"
# logger.debug(f"DB Query: {log_message}")
# log.debug(f"DB Query: {log_message}")
log.debug(log_message)
try:
cursor.execute(query, args)
@@ -203,7 +199,7 @@ class Database:
conn.commit()
self.close_connection(conn)
except sql.OperationalError as e:
logger.error(f"Error in query: {e}")
log.error(f"Error in query: {e}")
return None
return (rv[0] if rv else None) if one else rv
@@ -219,7 +215,7 @@ class Database:
app_id (str): The apparat id where the book should be added to
prof_id (str): The id of the professor where the book should be added to.
"""
logger.info(f"Adding book {bookdata.signature} to database")
log.info(f"Adding book {bookdata.signature} to database")
if app_id is None or prof_id is None:
raise ValueError("Apparate ID or Prof ID is None")
conn = self.connect()
@@ -227,12 +223,12 @@ class Database:
t_query = (
f"SELECT bookdata FROM media WHERE app_id={app_id} AND prof_id={prof_id}"
)
logger.debug(t_query)
# # logger.debug(t_query)
log.debug(t_query)
# # log.debug(t_query)
result = cursor.execute(t_query).fetchall()
result = [BookData().from_string(i[0]) for i in result]
if bookdata in result:
# logger.debug("Bookdata already in database")
# log.debug("Bookdata already in database")
# check if the book was deleted in the apparat
query = (
"SELECT deleted FROM media WHERE app_id=? AND prof_id=? AND bookdata=?"
@@ -240,7 +236,7 @@ class Database:
params = (app_id, prof_id, json.dumps(asdict(bookdata), ensure_ascii=False))
result = cursor.execute(query, params).fetchone()
if result[0] == 1:
# logger.debug("Book was deleted, updating bookdata")
# log.debug("Book was deleted, updating bookdata")
query = "UPDATE media SET deleted=0 WHERE app_id=? AND prof_id=? AND bookdata=?"
params = (
app_id,
@@ -258,7 +254,7 @@ class Database:
params = (converted, app_id, prof_id, 0)
cursor.execute(query, params)
logMessage = f"Added book with signature {bookdata.signature} to database, data: {converted}"
logger.info(logMessage)
log.info(logMessage)
conn.commit()
self.close_connection(conn)
@@ -327,7 +323,7 @@ class Database:
list[tuple[BookData, int]]: A list of tuples containing the wrapped Metadata and the id of the book
"""
rdata = self.query_db("SELECT * FROM media WHERE deleted=0")
# logger.debug(rdata, len(rdata))
# log.debug(rdata, len(rdata))
mode = 0
if len(data) == 1:
if "signature" in data.keys():
@@ -355,7 +351,7 @@ class Database:
and data["title"] in bookdata.title
):
ret.append((bookdata, app_id, prof_id))
# logger.debug(ret)
# log.debug(ret)
return ret
def setAvailability(self, book_id: str, available: str):
@@ -516,7 +512,7 @@ class Database:
delete=False, dir=tempdir_path, mode="wb", suffix=f".{filetype}"
)
file.write(blob)
# logger.debug("file created")
# log.debug("file created")
return file.name
def getFiles(self, app_id: Union[str, int], prof_id: int) -> list[tuple]:
@@ -544,7 +540,7 @@ class Database:
return [i[0] for i in data]
def insertSubjects(self):
# logger.debug("Inserting subjects")
# log.debug("Inserting subjects")
subjects = [
"Biologie",
"Chemie",
@@ -660,7 +656,7 @@ class Database:
Args:
message_id (str): the id of the message
"""
logger.debug(f"Deleting message with id {message_id}")
log.debug(f"Deleting message with id {message_id}")
self.query_db("DELETE FROM messages WHERE id=?", (message_id,))
# Prof data
@@ -827,7 +823,7 @@ class Database:
)
numbers = [i[0] for i in numbers]
numbers.sort()
logger.info(f"Currently used apparat numbers: {numbers}")
log.info(f"Currently used apparat numbers: {numbers}")
return numbers
def setNewSemesterDate(self, app_id: Union[str, int], newDate, dauerapp=False):
@@ -881,22 +877,22 @@ class Database:
Returns:
Optional[int]: the id of the apparat
"""
logger.debug(apparat)
log.debug(apparat)
app = apparat.apparat
prof = apparat.prof
present_prof = self.getProfByName(prof.name())
prof_id = present_prof.id
logger.debug(present_prof)
log.debug(present_prof)
app_id = self.getApparatId(app.name)
if app_id:
return AppPresentError(app_id)
if not prof_id:
logger.debug("prof id not present, creating prof with data", prof)
log.debug("prof id not present, creating prof with data", prof)
prof_id = self.createProf(prof)
logger.debug(prof_id)
log.debug(prof_id)
query = f"INSERT OR IGNORE INTO semesterapparat (appnr, name, erstellsemester, dauer, prof_id, fach,deletion_status,konto) VALUES ('{app.appnr}', '{app.name}', '{app.created_semester}', '{app.eternal}', {prof_id}, '{app.subject}', '{0}', '{SEMAP_MEDIA_ACCOUNTS[app.appnr]}')"
logger.debug(query)
log.debug(query)
self.query_db(query)
return None
@@ -914,7 +910,7 @@ class Database:
)
ret = []
for i in data:
logger.debug(i)
log.debug(i)
ret.append(Apparat().from_tuple(i))
return ret
@@ -996,7 +992,7 @@ class Database:
app_id (Union[str, int]): the id of the apparat
semester (str): the semester the apparat should be deleted from
"""
logger.info(f"Deleting apparat with id {app_id} in semester {semester}")
log.info(f"Deleting apparat with id {app_id} in semester {semester}")
self.query_db(
"UPDATE semesterapparat SET deletion_status=1, deleted_date=? WHERE appnr=?",
(semester, app_id),
@@ -1054,7 +1050,7 @@ class Database:
apparat_data.apparat.apparat_id_adis,
apparat_data.apparat.appnr,
)
logger.debug(f"Updating apparat with query {query} and params {params}")
log.debug(f"Updating apparat with query {query} and params {params}")
self.query_db(query, params)
def checkApparatExists(self, app_name: str):
@@ -1106,7 +1102,7 @@ class Database:
Returns:
list: the result of the query
"""
logger.debug(f"Query: {query}")
log.debug(f"Query: {query}")
conn = self.connect()
cursor = conn.cursor()
result = cursor.execute(query).fetchall()
@@ -1119,7 +1115,7 @@ class Database:
result_a = tuple(result_a)
result[result.index(orig_value)] = result_a
self.close_connection(conn)
logger.debug(f"Query result: {result}")
log.debug(f"Query result: {result}")
return result
if "deletable" in kwargs.keys():
@@ -1134,9 +1130,9 @@ class Database:
kwargs["dauer"] = kwargs["dauer"].replace("Ja", "1").replace("Nein", "0")
query = "SELECT * FROM semesterapparat WHERE "
for key, value in kwargs.items() if kwargs.items() is not None else {}:
# logger.debug(key, value)
# log.debug(key, value)
query += f"{key}='{value}' AND "
# logger.debug(query)
# log.debug(query)
# remove deletesemester part from normal query, as this will be added to the database upon deleting the apparat
if "deletesemester" in kwargs.keys():
query = query.replace(
@@ -1152,24 +1148,24 @@ class Database:
query = query.replace(
f"endsemester='{kwargs['endsemester']}' AND ", "xyz"
)
# logger.debug("replaced")
# log.debug("replaced")
query = query.replace(
"xyz",
f"(erstellsemester='{kwargs['endsemester']}' OR verlängerung_bis='{kwargs['endsemester']}') AND ",
)
# remove all x="" parts from the query where x is a key in kwargs
logger.info(f"Query before: {query}")
log.info(f"Query before: {query}")
query = query.strip()
query = query[:-4]
logger.info(f"Query after: {query}")
log.info(f"Query after: {query}")
# check if query ends with lowercase letter or a '. if not, remove last symbol and try again
while query[-1] not in ascii_lowercase and query[-1] != "'":
query = query[:-1]
query = query.strip()
# logger.debug(query)
# log.debug(query)
res = __query(query)
# logger.debug(res)
# log.debug(res)
return res
# Admin data
@@ -1457,7 +1453,7 @@ class Database:
blob = self.query_db(
"SELECT fileblob FROM elsa_files WHERE filename=?", (filename,), one=True
)[0]
# logger.debug(blob)
# log.debug(blob)
tempdir = self.database.temp
tempdir = tempdir.replace("~", str(Path.home()))
tempdir_path = Path(tempdir)
@@ -1467,7 +1463,7 @@ class Database:
delete=False, dir=tempdir_path, mode="wb", suffix=f".{filetype}"
)
file.write(blob)
# logger.debug("file created")
# log.debug("file created")
return file.name
def getElsaApparats(self) -> ELSA:
@@ -1517,7 +1513,7 @@ class Database:
###
def createProf(self, profdata: Prof):
logger.debug(profdata)
log.debug(profdata)
conn = self.connect()
cursor = conn.cursor()
fname = profdata.firstname
@@ -1528,7 +1524,7 @@ class Database:
title = profdata.title
query = f"INSERT INTO prof (fname, lname, fullname, mail, telnr,titel) VALUES ('{fname}','{lname}','{fullname}','{mail}','{telnr}','{title}')"
logger.debug(query)
log.debug(query)
cursor.execute(query)
conn.commit()
@@ -1573,7 +1569,7 @@ class Database:
else:
fullname = profdata.name()
query = f"SELECT id FROM prof WHERE fullname = '{fullname}'"
logger.debug(query)
log.debug(query)
cursor.execute(query)
result = cursor.fetchone()
@@ -1591,7 +1587,7 @@ class Database:
conn = self.connect()
cursor = conn.cursor()
query = f"SELECT * FROM prof WHERE fullname = '{fullname}'"
logger.debug(query)
log.debug(query)
result = cursor.execute(query).fetchone()
if result:
@@ -1612,7 +1608,7 @@ class Database:
query = f"SELECT prof_id from semesterapparat WHERE appnr = '{apprarat_id}' and deletion_status = 0"
data = self.query_db(query)
if data:
logger.info("Prof ID: " + str(data[0][0]))
log.info("Prof ID: " + str(data[0][0]))
return data[0][0]
else:
return None