generated from seyLu/python-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LOGGER] Separate logger instance by module and fix force remove bug (#…
…184) * feat (logger): separate logger instance by module and fix force remove bug * fix: types
- Loading branch information
Showing
8 changed files
with
126 additions
and
70 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
import os | ||
from logging import Logger | ||
from logging.config import fileConfig | ||
|
||
import rich | ||
|
||
from ghlabel.config import is_ghlabel_debug_mode | ||
|
||
GHLABEL_LOGS_DIR: str = os.path.join("logs") | ||
|
||
|
||
class GhlabelLogger: | ||
def __init__(self) -> None: | ||
self.logger: GhlabelLogger | ||
|
||
if not os.path.isdir(GHLABEL_LOGS_DIR): | ||
os.makedirs(GHLABEL_LOGS_DIR) | ||
fileConfig(os.path.join(os.path.dirname(__file__), "logging.ini")) | ||
|
||
def init(self, module_name: str) -> "GhlabelLogger": | ||
"""NOTE: pass in __name__ as module name""" | ||
logger: Logger = logging.getLogger(module_name) | ||
|
||
if is_ghlabel_debug_mode(): | ||
logger.setLevel(level=logging.DEBUG) | ||
else: | ||
logger.setLevel(level=logging.ERROR) | ||
|
||
self.logger = logger # type: ignore[assignment] | ||
return self.logger | ||
|
||
def exception(self, ex: Exception) -> None: | ||
rich.print( | ||
f"\nSomething went wrong. See logs ([blue underline]{GHLABEL_LOGS_DIR}[/blue underline]) for more details.\n" | ||
) | ||
rich.print(" [[red]Exception[/red]]:", str(ex)) | ||
self.logger.exception(ex) | ||
|
||
def error(self, message: str) -> None: | ||
self.logger.error(message) | ||
|
||
def warning(self, message: str) -> None: | ||
self.logger.warning(message) | ||
|
||
def info(self, message: str) -> None: | ||
self.logger.info(message) | ||
|
||
|
||
ghlabel_logger: GhlabelLogger = GhlabelLogger() |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
g_GHLABEL_DEBUG_MODE = False | ||
|
||
|
||
def set_ghlabel_debug_mode(is_debug: bool) -> None: | ||
global g_GHLABEL_DEBUG_MODE # noqa: PLW0603 | ||
g_GHLABEL_DEBUG_MODE = is_debug | ||
|
||
|
||
def is_ghlabel_debug_mode() -> bool: | ||
global g_GHLABEL_DEBUG_MODE # noqa: PLW0602 | ||
return g_GHLABEL_DEBUG_MODE |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
keys=root | ||
|
||
[logger_root] | ||
level=DEBUG | ||
level=INFO | ||
handlers=screen,file | ||
|
||
[formatters] | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
import logging | ||
import os | ||
import platform | ||
import subprocess | ||
import sys | ||
from logging.config import fileConfig | ||
from pathlib import Path | ||
|
||
from dotenv import load_dotenv | ||
from dotenv import find_dotenv, load_dotenv | ||
|
||
load_dotenv() | ||
Path("logs").mkdir(exist_ok=True) | ||
fileConfig(os.path.join(os.path.dirname(__file__), "../logging.ini")) | ||
from ghlabel.__logger__ import GhlabelLogger, ghlabel_logger | ||
from ghlabel.config import is_ghlabel_debug_mode | ||
|
||
logger: GhlabelLogger = ghlabel_logger.init(__name__) | ||
load_dotenv(find_dotenv(usecwd=True)) | ||
|
||
STATUS_OK: int = 200 | ||
|
||
|
||
def validate_env(env: str) -> str: | ||
_env: str | None = os.getenv(env) | ||
if not _env: | ||
logging.error(f"{env} environment variable not set.") | ||
logger.error(f"{env} environment variable not set.") | ||
sys.exit() | ||
return _env | ||
|
||
|
||
def clear_screen() -> None: | ||
if platform.system() == "Windows": | ||
subprocess.run("cls", shell=True, check=False) # noqa: S607, S602 | ||
else: | ||
subprocess.run("clear", shell=True, check=False) # noqa: S607, S602 | ||
if not is_ghlabel_debug_mode(): | ||
if platform.system() == "Windows": | ||
subprocess.run("cls", shell=True, check=False) # noqa: S607, S602 | ||
else: | ||
subprocess.run("clear", shell=True, check=False) # noqa: S607, S602 |
Oops, something went wrong.