Skip to content

Commit a942b4a

Browse files
committed
feat: Add configurable log filter to suppress raw JSON payloads in DEBUG mode
Introduces SUPPRESS_FUNCTION_LOGS env flag that attaches a pattern-based filter to the log handler, suppressing verbose JSON dumps (requests, responses, mutated message lists) while keeping meaningful operational events. Filter is applied to the handler rather than the logger so it correctly intercepts records propagated from child loggers.
1 parent 5b2af41 commit a942b4a

3 files changed

Lines changed: 781 additions & 1 deletion

File tree

app/logging_config.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@
66
import logging
77
import sys
88

9-
from app.settings import LOGGING_LEVEL
9+
from app.settings import LOGGING_LEVEL, SUPPRESS_FUNCTION_LOGS
10+
11+
12+
_SUPPRESS_PATTERNS = ("Updated messages", "Initial response", "Updated response", "OpenAI response", "Sending messages to OpenAI")
13+
14+
15+
class _SuppressFunctionResponseFilter(logging.Filter):
16+
def filter(self, record):
17+
msg = record.getMessage()
18+
return not any(pattern in msg for pattern in _SUPPRESS_PATTERNS)
1019

1120

1221
def setup_logging(level: int = LOGGING_LEVEL) -> logging.Logger:
@@ -27,7 +36,13 @@ def setup_logging(level: int = LOGGING_LEVEL) -> logging.Logger:
2736
handlers=[logging.StreamHandler(sys.stdout)], # This logs to console
2837
)
2938

39+
if SUPPRESS_FUNCTION_LOGS:
40+
for handler in logging.getLogger().handlers:
41+
handler.addFilter(_SuppressFunctionResponseFilter())
42+
3043
logging.getLogger("httpx").setLevel(logging.WARNING)
44+
logging.getLogger("openai._base_client").setLevel(logging.WARNING)
45+
logging.getLogger("httpcore").setLevel(logging.WARNING)
3146

3247
return logging.getLogger(__name__)
3348

app/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
# General settings
1616
LOGGING_LEVEL = int(os.getenv("LOGGING_LEVEL", str(logging.INFO)))
17+
SUPPRESS_FUNCTION_LOGS = os.getenv("SUPPRESS_FUNCTION_LOGS", "false").lower() == "true"
1718

1819

1920
# LLM/OpenAI API settings

0 commit comments

Comments
 (0)