diff --git a/.gitignore b/.gitignore index 68bc17f..d6f5b2e 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,4 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +debug diff --git a/main.py b/main.py index c51b046..dfb10c3 100755 --- a/main.py +++ b/main.py @@ -1,8 +1,11 @@ #!/usr/bin/env python + +from loguru import logger from py2048.game import Game +@logger.catch def main() -> None: Game().run() diff --git a/src/py2048/game.py b/src/py2048/game.py index e014f4a..7dc8ab2 100644 --- a/src/py2048/game.py +++ b/src/py2048/game.py @@ -2,14 +2,19 @@ import random import sys import pygame +from loguru import logger from .block import Block from .colors import COLORS from .config import Config +from .logger import setup_logger class Game: def __init__(self) -> None: + setup_logger() + logger.info("Initializing game") + pygame.init() self.screen: pygame.Surface = pygame.display.set_mode((Config.WIDTH, Config.HEIGHT)) pygame.display.set_caption("2048") diff --git a/src/py2048/logger.py b/src/py2048/logger.py new file mode 100644 index 0000000..b21592c --- /dev/null +++ b/src/py2048/logger.py @@ -0,0 +1,15 @@ +from pathlib import Path + +from loguru import logger + +BASE_PATH = Path(__file__).resolve().parent.parent.parent + + +def setup_logger() -> None: + logger.add( + BASE_PATH.joinpath(".logs", "game.log"), + format="{time} | {level} | {message}", + level="DEBUG" if BASE_PATH.joinpath("debug").exists() else "INFO", + rotation="1 MB", + compression="zip", + )