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:
Ricardo 2026-10-08 14:48:35 -03:00
parent ed597f8c61
commit b0a677a685
3 changed files with 15 additions and 9 deletions

2
.gitignore vendored
View file

@ -4,4 +4,4 @@ __pycache__/
*.pyc *.pyc
*.pyo *.pyo
*.log *.log
config.json config.jsonlogs/

View file

@ -136,7 +136,10 @@ def setup_logging():
if _queue_listener is not None: if _queue_listener is not None:
_queue_listener.stop() _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() _queue_listener.start()
# --- 6. LOGGER RAIZ --- # --- 6. LOGGER RAIZ ---

17
main.py
View file

@ -1,24 +1,27 @@
import json import json
import logging import logging
from contextlib import asynccontextmanager
import uvicorn import uvicorn
from fastapi import FastAPI from fastapi import FastAPI
from app.core.configs import settings from app.core.configs import settings
from app.logger_config import setup_logging, shutdown_logging
from app.v1.api import api_router 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 = 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() @asynccontextmanager
handler.setFormatter(logging.Formatter('%(asctime)s | %(levelname)s | %(name)s | %(message)s')) async def lifespan(app: FastAPI):
logger.addHandler(handler) yield
shutdown_logging() # garante o flush da fila de log no shutdown
app = FastAPI( app = FastAPI(
title = 'API - Podium', title = 'API - Podium',
version= '0.0.1' version= '0.0.1',
lifespan=lifespan
) )
app.include_router(api_router, prefix='/v1') app.include_router(api_router, prefix='/v1')