Ativa o logger em arquivo na inicialização da API
Chama setup_logging() no main.py e faz o logger api_podium propagar pro root, pra que as mensagens cheguem no arquivo em logs/. Adiciona saída no console no QueueListener e o shutdown_logging() no lifespan do FastAPI. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
ed597f8c61
commit
b0a677a685
3 changed files with 15 additions and 9 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -4,4 +4,4 @@ __pycache__/
|
|||
*.pyc
|
||||
*.pyo
|
||||
*.log
|
||||
config.json
|
||||
config.jsonlogs/
|
||||
|
|
|
|||
|
|
@ -136,7 +136,10 @@ def setup_logging():
|
|||
if _queue_listener is not None:
|
||||
_queue_listener.stop()
|
||||
|
||||
_queue_listener = QueueListener(log_queue, file_handler, respect_handler_level=True)
|
||||
console_handler = logging.StreamHandler()
|
||||
console_handler.setFormatter(formatter)
|
||||
|
||||
_queue_listener = QueueListener(log_queue, file_handler, console_handler, respect_handler_level=True)
|
||||
_queue_listener.start()
|
||||
|
||||
# --- 6. LOGGER RAIZ ---
|
||||
|
|
|
|||
17
main.py
17
main.py
|
|
@ -1,24 +1,27 @@
|
|||
import json
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
|
||||
from app.core.configs import settings
|
||||
from app.logger_config import setup_logging, shutdown_logging
|
||||
from app.v1.api import api_router
|
||||
|
||||
# Logger próprio da API (o logging.info direto usa o logger root, que fica em WARNING e não mostra INFO)
|
||||
# Configura o logger root (arquivo em logs/ + console); o logger da API propaga pra ele
|
||||
setup_logging()
|
||||
logger = logging.getLogger('api_podium')
|
||||
logger.setLevel(logging.INFO)
|
||||
logger.propagate = False # não repassa pro root, senão a mensagem pode sair duplicada
|
||||
|
||||
handler = logging.StreamHandler()
|
||||
handler.setFormatter(logging.Formatter('%(asctime)s | %(levelname)s | %(name)s | %(message)s'))
|
||||
logger.addHandler(handler)
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
yield
|
||||
shutdown_logging() # garante o flush da fila de log no shutdown
|
||||
|
||||
app = FastAPI(
|
||||
title = 'API - Podium',
|
||||
version= '0.0.1'
|
||||
version= '0.0.1',
|
||||
lifespan=lifespan
|
||||
)
|
||||
|
||||
app.include_router(api_router, prefix='/v1')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue