Skip to content

Commit

Permalink
💥 Update setup_logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Kajiih committed Dec 18, 2024
1 parent a74e7c8 commit 272a8e0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dev/readme_snippets/raw/features_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
prompt("Enter a number") # snippet: no-exec

# Simply setup well formatted logging in files and console
setup_logging()
setup_logging("app", log_dir="logs")
2 changes: 1 addition & 1 deletion src/kajihs_utils/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent

__app_name__ = "kajihs_utils"
__version__ = "0.3.3"
__version__ = "0.3.4"
__authors__ = ["Kajih"]
__author_emails__ = ["[email protected]"]
__repo_url__ = "https://github.com/Kajiih/kajihs_utils"
Expand Down
36 changes: 24 additions & 12 deletions src/kajihs_utils/loguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import inspect
import logging
import sys
from pathlib import Path
from typing import override

from loguru import logger

Check failure on line 9 in src/kajihs_utils/loguru.py

View workflow job for this annotation

GitHub Actions / build (3.12, ubuntu-latest)

Import from `loguru` is implicitly relative and will not work if this file is imported as a module   Use a relative import from `.loguru` instead   or specify the full module path: `kajihs_utils.loguru` (reportImplicitRelativeImport)

Check warning on line 9 in src/kajihs_utils/loguru.py

View workflow job for this annotation

GitHub Actions / build (3.12, ubuntu-latest)

"logger" is not exported from module "loguru" (reportPrivateLocalImportUsage)
Expand Down Expand Up @@ -43,31 +44,42 @@ def emit(self, record: logging.LogRecord) -> None:
logger.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage())

Check failure on line 44 in src/kajihs_utils/loguru.py

View workflow job for this annotation

GitHub Actions / build (3.12, ubuntu-latest)

"opt" is not a known attribute of module "loguru" (reportAttributeAccessIssue)


def setup_logging(file_name: str = "app") -> None:
"""Set up logging."""
def setup_logging(prefix: str = "app", /, log_dir: str = "logs") -> None:
"""
Set up beautiful loguru logging in files and console.
Redirects logging with Loguru, creates 2 logging files with and without
colors and log to console.
Args:
prefix: Prefix for the log files without extensions.
log_dir: Directory path to store log files.
"""
# Redirect logging with Loguru
logging.basicConfig(handlers=[InterceptHandler()], level=logging.WARNING, force=True)

logger.remove()

Check failure on line 62 in src/kajihs_utils/loguru.py

View workflow job for this annotation

GitHub Actions / build (3.12, ubuntu-latest)

"remove" is not a known attribute of module "loguru" (reportAttributeAccessIssue)

dir_path = Path(log_dir)
logger.add(

Check failure on line 65 in src/kajihs_utils/loguru.py

View workflow job for this annotation

GitHub Actions / build (3.12, ubuntu-latest)

"add" is not a known attribute of module "loguru" (reportAttributeAccessIssue)
f"logs/{file_name}.log", # Log to a file
level="DEBUG", # Minimum logging level
dir_path / f"{prefix}.log",
level="DEBUG",
format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}",
rotation="1 week", # Rotate logs weekly
compression="zip", # Compress rotated logs
rotation="1 week",
compression="zip",
)
logger.add(

Check failure on line 72 in src/kajihs_utils/loguru.py

View workflow job for this annotation

GitHub Actions / build (3.12, ubuntu-latest)

"add" is not a known attribute of module "loguru" (reportAttributeAccessIssue)
f"logs/{file_name}_color.log", # Log to a file
level="DEBUG", # Minimum logging level
dir_path / f"{prefix}.clog",
level="DEBUG",
# format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}",
rotation="1 week", # Rotate logs weekly
compression="zip", # Compress rotated logs
rotation="1 week",
compression="zip",
colorize=True,
)
logger.add(

Check failure on line 80 in src/kajihs_utils/loguru.py

View workflow job for this annotation

GitHub Actions / build (3.12, ubuntu-latest)

"add" is not a known attribute of module "loguru" (reportAttributeAccessIssue)
sys.stdout, # Log to the console
level="INFO", # Minimum logging level for the console
sys.stdout,
level="INFO",
# format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{message}</level>",
format="<level>{message}</level>",
)

0 comments on commit 272a8e0

Please sign in to comment.