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

40
main.py
View File

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

View File

@ -1,5 +1,8 @@
# from .config import get_config
# from .io import read_genome
# from .training import train
from .log import log
__all__ = ["log"]
#
# __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 .log import log
__all__ = ["Game2048", "play"]
__all__ = ["Game2048", "play", "log"]

View File

@ -1,10 +1,12 @@
import random
import numpy as np
from loguru import logger
from utils import Config, Direction
from .log import log
@log.catch
def play() -> None:
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 .color import ColorScheme
from .config import Config
from .enums import Direction
BASE_PATH = Path(__file__).resolve().parent.parent.parent.parent
from .log import log
from .path import BASE_PATH
__all__ = [
"Board",
@ -19,4 +17,5 @@ __all__ = [
"Screen",
"BASE_PATH",
"Config",
"log",
]

View File

@ -15,3 +15,5 @@ class Config:
board = Board()
header = Header()
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