acredito que seja log

This commit is contained in:
Ricardo 2026-10-08 14:43:43 -03:00
parent 61f16b100b
commit ed597f8c61
3 changed files with 162 additions and 2 deletions

155
app/logger_config.py Normal file
View file

@ -0,0 +1,155 @@
import logging
import queue
from datetime import date, datetime, timedelta
from logging.handlers import QueueHandler, QueueListener
from pathlib import Path
import portalocker
from app.core.configs import settings
class ProcessSafeTimedFileHandler(logging.FileHandler):
"""
Handler de arquivo com:
- File locking (portalocker) para múltiplos workers/processos
- Rotação por dia à meia-noite (renomeia o anterior, cria novo)
- Cleanup automático de logs antigos
"""
def __init__(
self,
log_dir: str,
base_name: str = "api_detailed",
backup_count: int = 21,
encoding: str = "utf-8",
):
self.log_dir = Path(log_dir)
self.log_dir.mkdir(parents=True, exist_ok=True)
self.base_name = base_name
self.backup_count = backup_count
log_path = self.log_dir / f"{self.base_name}.log"
# Inicializa _current_date a partir da mtime do arquivo existente,
# para que reinícios da API após meia-noite ainda rotacionem o log
# do dia anterior em vez de continuar escrevendo nele.
if log_path.exists() and log_path.stat().st_size > 0:
self._current_date = datetime.fromtimestamp(log_path.stat().st_mtime).date()
else:
self._current_date = datetime.now().date()
super().__init__(str(log_path), mode="a", encoding=encoding, delay=True)
# Rotaciona imediatamente se o arquivo existente é de um dia anterior
today = datetime.now().date()
if today > self._current_date:
self._rotate(self._current_date)
self._current_date = today
self._cleanup()
def emit(self, record):
try:
msg = self.format(record)
lock_path = str(self.log_dir / f"{self.base_name}.lock")
with portalocker.Lock(lock_path, "a", timeout=5):
# Verifica se mudou o dia → rotaciona
today = datetime.now().date()
if today > self._current_date:
self._rotate(self._current_date)
self._current_date = today
self._cleanup()
# Escreve no arquivo (abre e fecha para não segurar o handle)
with open(self.baseFilename, "a", encoding=self.encoding) as f:
f.write(msg + self.terminator)
except Exception:
self.handleError(record)
def _rotate(self, old_date: date):
"""Renomeia api_detailed.log → api_detailed-2025-02-27.log"""
current_file = Path(self.baseFilename)
if current_file.exists() and current_file.stat().st_size > 0:
rotated = self.log_dir / f"{self.base_name}-{old_date.isoformat()}.log"
try:
if rotated.exists():
# Append se outro worker já rotacionou
with (
open(rotated, "a", encoding=self.encoding) as dst,
open(current_file, "r", encoding=self.encoding) as src,
):
dst.write(src.read())
current_file.unlink()
else:
current_file.rename(rotated)
except OSError:
pass # Outro worker já rotacionou
def _cleanup(self):
"""Remove logs mais antigos que backup_count dias."""
cutoff = datetime.now().date() - timedelta(days=self.backup_count)
for f in self.log_dir.glob(f"{self.base_name}-*.log"):
try:
date_str = f.stem.replace(f"{self.base_name}-", "")
file_date = datetime.strptime(date_str, "%Y-%m-%d").date()
if file_date < cutoff:
f.unlink()
except (ValueError, OSError):
pass
# Variável de módulo para manter o listener vivo
_queue_listener: QueueListener | None = None
def setup_logging():
global _queue_listener
logging.getLogger("pika").setLevel(logging.WARNING)
logging.getLogger("httpx").setLevel(logging.WARNING)
# --- 1. NÍVEL DE LOG VIA configs.json ---
level_name = settings.logs.log_level.upper()
log_level = getattr(logging, level_name, logging.INFO)
# --- 2. FORMATTER ---
formatter = logging.Formatter(
"%(asctime)s|%(levelname)s|%(name)s|%(message)s|%(filename)s|%(lineno)d",
datefmt="%Y-%m-%d %H:%M:%S",
)
# --- 3. HANDLER REAL (com file locking) ---
file_handler = ProcessSafeTimedFileHandler(
log_dir="logs",
base_name="api_detailed",
backup_count=settings.logs.max_dias,
)
file_handler.setFormatter(formatter)
# --- 4. QUEUE (não bloqueia a thread da request) ---
log_queue = queue.Queue(-1) # sem limite
queue_handler = QueueHandler(log_queue)
# --- 5. LISTENER (thread background que consome a fila) ---
if _queue_listener is not None:
_queue_listener.stop()
_queue_listener = QueueListener(log_queue, file_handler, respect_handler_level=True)
_queue_listener.start()
# --- 6. LOGGER RAIZ ---
root_logger = logging.getLogger()
root_logger.setLevel(log_level)
if root_logger.hasHandlers():
root_logger.handlers.clear()
root_logger.addHandler(queue_handler)
def shutdown_logging():
"""Chamar no shutdown da API para garantir flush."""
global _queue_listener
if _queue_listener:
_queue_listener.stop()
_queue_listener = None