Skip to content

Instantly share code, notes, and snippets.

@prrao87
Last active March 6, 2023 14:24
Show Gist options
  • Save prrao87/8fe20449c8807028966a9ac5e626b40e to your computer and use it in GitHub Desktop.
Save prrao87/8fe20449c8807028966a9ac5e626b40e to your computer and use it in GitHub Desktop.
# Create a logger with rotating file handler and formatting per loguru
def get_logger(filename: str):
import sys
from loguru import logger
from pathlib import Path
Path("logs").mkdir(parents=True, exist_ok=True)
logger.remove() # Remove base logger settings and overwrite with custom settings
fmt = "{time:HH:mm:ss:SSS} -- {level} -- {message}"
# Add rotating file handler with desired level of logging
filename = f"{filename}.log" if not filename.endswith(".log") else filename
logger.add(Path("logs") / filename, level="INFO", format=fmt, rotation="50 MB")
# Add console log with desired level of logging
logger.add(sys.stderr, level="DEBUG", format=fmt)
return logger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment