Skip to content

Commit

Permalink
refactor(logger): use standard and root logger instances (#29)
Browse files Browse the repository at this point in the history
* refactor(logger): Ute the standard python function to get a logger instance

* refactor(loogger): Use root logger in logger file
  • Loading branch information
clementb49 authored Apr 28, 2024
1 parent 5c59975 commit a5b513c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
5 changes: 3 additions & 2 deletions src/configdialog.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import logging
import wx
from babel import Locale
from config import conf, LogLevelEnum
from localization import get_supported_locales, get_app_locale
from logger import get_app_logger, set_log_level
from logger import set_log_level

log = get_app_logger(__name__)
log = logging.getLogger(__name__)

LOG_LEVELS = {
# Translators: A label for the log level in the settings dialog
Expand Down
17 changes: 7 additions & 10 deletions src/localization.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import locale
import gettext
import locale
import logging
import wx
from typing import Optional
from pathlib import Path
from babel import Locale
from consts import APP_NAME, DEFAULT_LANG
from logger import get_app_logger

logger = get_app_logger(__name__)

log = logging.getLogger(__name__)
LOCALE_DIR = Path(__file__).parent / Path("res", "locale")


Expand All @@ -22,7 +21,7 @@ def get_supported_locales(domain: str = APP_NAME) -> list[Locale]:
if mo_file.exists():
supported_locales.append(detected_locale)
else:
logger.warning(
log.warning(
f"Translation compiled file not found for: {detected_locale.english_name}"
)
return supported_locales
Expand All @@ -39,13 +38,11 @@ def get_wx_locale(current_locale: Locale) -> wx.Locale:
"""Get the wxPython locale name from the babel locale"""
find_language = wx.Locale.FindLanguageInfo(current_locale.language)
if find_language:
logger.debug(
log.debug(
f"wxPython locale found for: {current_locale.english_name}({find_language.Language})"
)
return wx.Locale(find_language.Language)
logger.warning(
f"wxPython locale not found for: {current_locale.english_name}"
)
log.warning(f"wxPython locale not found for: {current_locale.english_name}")
return wx.Locale(wx.LANGUAGE_DEFAULT)


Expand All @@ -58,7 +55,7 @@ def setup_translation(locale: Locale) -> None:
fallback=True,
)
translation.install()
logger.debug(f"gettext Translation setup for: {locale.english_name}")
log.debug(f"gettext Translation setup for: {locale.english_name}")


def init_translation(language: Optional[str]) -> wx.Locale:
Expand Down
20 changes: 7 additions & 13 deletions src/logger.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging


def setup_logging(level: str):
def setup_logging(level: str) -> None:
"""Setup logging configuration"""
console_handler = logging.StreamHandler()
file_handler = logging.FileHandler("basiliskLLM.log", mode='w')
Expand All @@ -13,21 +13,15 @@ def setup_logging(level: str):
)


def set_log_level(level):
def set_log_level(level: str) -> None:
"""Change global log level to new level and update all loggers
:param level: new log level
"""
cur_level = logging.getLevelName(logging.getLogger().getEffectiveLevel())
cur_level = logging.getLevelName(logging.root.getEffectiveLevel())
if cur_level == level:
return
logging.getLogger().setLevel(level)
for handler in logging.getLogger().handlers:
logging.root.setLevel(level)
for handler in logging.root.handlers:
handler.setLevel(level)
new_level = logging.getLevelName(logging.getLogger().getEffectiveLevel())
logging.getLogger().debug(
f"Log level changed from {cur_level} to {new_level}"
)


def get_app_logger(name):
return logging.getLogger(name)
new_level = logging.getLevelName(logging.root.getEffectiveLevel())
logging.root.debug(f"Log level changed from {cur_level} to {new_level}")
5 changes: 3 additions & 2 deletions src/main.pyw
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import sys
import winsound
Expand All @@ -7,7 +8,7 @@ import wx
import wx.adv
import config

from logger import get_app_logger, setup_logging
from logger import setup_logging
from localization import init_translation
from consts import (
APP_NAME,
Expand All @@ -19,7 +20,7 @@ from account import initialize_accountManager

sys.path.append(os.path.join(os.path.dirname(__file__), ""))

log = get_app_logger(__name__)
log = logging.getLogger(__name__)

class MainApp(wx.App):

Expand Down

0 comments on commit a5b513c

Please sign in to comment.