feat(log): setup logger for each module

This commit is contained in:
Kristofers Solo 2024-01-03 20:23:03 +02:00
parent b95a46e263
commit 8691bcfcc9
12 changed files with 91 additions and 28 deletions

34
main.py
View File

@ -1,12 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
import argparse import argparse
from game import play
# from ai import train
from loguru import logger from loguru import logger
from utils import BASE_PATH from utils import BASE_PATH, Config
def pos_int(string: str) -> int: def pos_int(string: str) -> int:
@ -60,34 +56,34 @@ group2.add_argument(
help="Run app with GUI", help="Run app with GUI",
) )
logger.add(
def setup_logger(debug_level: str) -> None: BASE_PATH / ".logs" / "2048.log",
logger.add(
BASE_PATH / ".logs" / "game.log",
format="{time} | {level} | {message}", format="{time} | {level} | {message}",
level=debug_level.upper(), level="WARNING",
rotation="10 MB", rotation="10 MB",
compression="zip", compression="zip",
) )
@logger.catch @logger.catch
def main(args: argparse.ArgumentParser) -> None: def main(args: argparse.ArgumentParser) -> None:
if args.debug: if args.debug:
setup_logger("debug") Config.log_level = "debug"
elif args.verbose: elif args.verbose:
setup_logger("debug") Config.log_level = "info"
else:
setup_logger("warning") import ai
import game
import gui
if args.train is not None: if args.train is not None:
# train(args.train) # train(args.train)
logger.debug("Train") ai.log.debug("Train")
elif args.noui: elif args.noui:
logger.debug("Run game in CLI") game.log.debug("Run game in CLI")
play() game.play()
else: else:
logger.debug("Run app") gui.log.debug("Run app")
# play() # play()

View File

@ -1,5 +1,8 @@
# from .config import get_config # from .config import get_config
# from .io import read_genome # from .io import read_genome
# from .training import train # from .training import train
from .log import log
__all__ = ["log"]
# #
# __all__ = ["train", "read_genome", "get_config"] # __all__ = ["train", "read_genome", "get_config"]

13
src/ai/log.py Normal file
View File

@ -0,0 +1,13 @@
from loguru import logger
from utils import BASE_PATH, Config
log = logger.bind(name="ai")
log.add(
BASE_PATH / ".logs" / "ai.log",
format="{time} | {level} | {message}",
level=Config.log_level.upper(),
rotation="10 MB",
compression="zip",
filter=lambda record: record["extra"]["name"] == "ai",
)

View File

@ -1,3 +1,4 @@
from .game import Game2048, play from .game import Game2048, play
from .log import log
__all__ = ["Game2048", "play"] __all__ = ["Game2048", "play", "log"]

View File

@ -1,10 +1,12 @@
import random import random
import numpy as np import numpy as np
from loguru import logger
from utils import Config, Direction from utils import Config, Direction
from .log import log
@log.catch
def play() -> None: def play() -> None:
game = Game2048() game = Game2048()

13
src/game/log.py Normal file
View File

@ -0,0 +1,13 @@
from loguru import logger
from utils import BASE_PATH, Config
log = logger.bind(name="game")
log.add(
BASE_PATH / ".logs" / "game.log",
format="{time} | {level} | {message}",
level=Config.log_level.upper(),
rotation="10 MB",
compression="zip",
filter=lambda record: record["extra"]["name"] == "game",
)

3
src/gui/__init__.py Normal file
View File

@ -0,0 +1,3 @@
from .log import log
__all__ = ["log"]

13
src/gui/log.py Normal file
View File

@ -0,0 +1,13 @@
from loguru import logger
from utils import BASE_PATH, Config
log = logger.bind(name="gui")
log.add(
BASE_PATH / ".logs" / "gui.log",
format="{time} | {level} | {message}",
level=Config.log_level.upper(),
rotation="10 MB",
compression="zip",
filter=lambda record: record["extra"]["name"] == "gui",
)

View File

@ -1,11 +1,9 @@
from pathlib import Path
from .collections import Board, Font, Header, Position, Screen, Size, Tile from .collections import Board, Font, Header, Position, Screen, Size, Tile
from .color import ColorScheme from .color import ColorScheme
from .config import Config from .config import Config
from .enums import Direction from .enums import Direction
from .log import log
BASE_PATH = Path(__file__).resolve().parent.parent.parent.parent from .path import BASE_PATH
__all__ = [ __all__ = [
"Board", "Board",
@ -19,4 +17,5 @@ __all__ = [
"Screen", "Screen",
"BASE_PATH", "BASE_PATH",
"Config", "Config",
"log",
] ]

View File

@ -15,3 +15,5 @@ class Config:
board = Board() board = Board()
header = Header() header = Header()
screen = Screen() screen = Screen()
log_level = "warning"

15
src/utils/log.py Normal file
View File

@ -0,0 +1,15 @@
from loguru import logger
from .config import Config
from .path import BASE_PATH
log = logger.bind(name="utils")
log.add(
BASE_PATH / ".logs" / "utils.log",
format="{time} | {level} | {message}",
level=Config.log_level.upper(),
rotation="10 MB",
compression="zip",
filter=lambda record: record["extra"]["name"] == "utils",
)

3
src/utils/path.py Normal file
View File

@ -0,0 +1,3 @@
from pathlib import Path
BASE_PATH = Path(__file__).resolve().parent.parent.parent