feat(game): setup logger

This commit is contained in:
Kristofers Solo 2023-12-28 18:13:08 +02:00
parent 144cf771f0
commit c40ec42ff1
4 changed files with 24 additions and 0 deletions

1
.gitignore vendored
View File

@ -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

View File

@ -1,8 +1,11 @@
#!/usr/bin/env python
from loguru import logger
from py2048.game import Game
@logger.catch
def main() -> None:
Game().run()

View File

@ -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")

15
src/py2048/logger.py Normal file
View File

@ -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",
)