Skip to content

Commit 286c193

Browse files
committed
fix: logging
1 parent c3a7829 commit 286c193

File tree

2 files changed

+11
-39
lines changed

2 files changed

+11
-39
lines changed

alembic/env.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import asyncio
2+
import importlib
23
import os
34
import sys
45
from logging.config import fileConfig
5-
import importlib
66
from pathlib import Path
77

88
# Add the project root directory to the Python path
@@ -20,7 +20,9 @@
2020
src_path = Path(__file__).parent.parent / "api" / "src"
2121
for path in src_path.rglob("*.py"):
2222
if path.name != "__init__.py":
23-
module_path = str(path.relative_to(Path(__file__).parent.parent)).replace(os.sep, ".")[:-3]
23+
module_path = str(path.relative_to(Path(__file__).parent.parent)).replace(
24+
os.sep, "."
25+
)[:-3]
2426
try:
2527
importlib.import_module(module_path)
2628
except Exception as e:

api/core/logging.py

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,18 @@
11
import logging
22
import sys
3-
from datetime import datetime
43

54
from api.core.config import settings
65

76

8-
class SimpleFormatter(logging.Formatter):
9-
"""Simple formatter with colored output for console."""
10-
11-
COLORS = {
12-
"DEBUG": "\033[36m", # Cyan
13-
"INFO": "\033[32m", # Green
14-
"WARNING": "\033[33m", # Yellow
15-
"ERROR": "\033[31m", # Red
16-
"RESET": "\033[0m", # Reset
17-
}
18-
19-
def format(self, record: logging.LogRecord) -> str:
20-
color = self.COLORS.get(record.levelname, "")
21-
reset = self.COLORS["RESET"]
22-
23-
# Format: [time] [level] [module:line] message
24-
return (
25-
f"{color}[{datetime.now().strftime('%H:%M:%S')}] "
26-
f"[{record.levelname}] [{record.name}:{record.lineno}] "
27-
f"{record.getMessage()}{reset}"
28-
)
29-
30-
317
def setup_logging() -> None:
328
"""Set up logging configuration."""
33-
# Create and configure console handler
34-
console_handler = logging.StreamHandler(sys.stdout)
35-
console_handler.setFormatter(SimpleFormatter())
36-
37-
# Configure root logger
38-
root_logger = logging.getLogger()
39-
root_logger.setLevel(logging.DEBUG if settings.DEBUG else logging.INFO)
40-
root_logger.addHandler(console_handler)
41-
42-
# Configure uvicorn loggers
43-
for logger_name in ("uvicorn", "uvicorn.access", "uvicorn.error"):
44-
logger = logging.getLogger(logger_name)
45-
logger.handlers = [console_handler]
9+
format_string = "[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s"
10+
logging.basicConfig(
11+
level=logging.DEBUG if settings.DEBUG else logging.INFO,
12+
format=format_string,
13+
datefmt="%H:%M:%S",
14+
stream=sys.stdout,
15+
)
4616

4717

4818
def get_logger(name: str) -> logging.Logger:

0 commit comments

Comments
 (0)