Structured Logging
The dcc_backend_common.logger module provides structured, consistent logging across all services using structlog.
Logging Standards
For general logging best practices and standards, see the Python Coding Standards - Logging section.
Overview
The module provides:
init_logger(): Initialize the logging system (call once at startup)get_logger(): Get a structured logger instance for any moduleget_usage_logger(): Get the pinnedusagelogger for usage/audit eventsUSAGE_LOGGER_NAME: The name of that logger ("usage") — use it to filter in OpenSearchEVENT_TYPES: The closed vocabulary allowed in theeventfieldFocusedTracebackFormatter: Advanced traceback rendering for development that highlights user codeDevTracebackStyle: Configuration options for traceback styles in development mode- Automatic context: Timestamps, module names, function names, and line numbers (plus
request_idwhen the logging middleware is installed) - Environment-aware rendering: JSON in production (fluentbit compatible), colored console in development
One Pipeline for Everything
init_logger() installs a single handler on the root logger. structlog events and stdlib records (uvicorn, third-party libraries, Python warnings) are rendered by the same pipeline, so every line has the same shape:
- Handlers attached by uvicorn /
fastapi-cliare cleared so records flow through the root handler. uvicorn.accessis disabled entirely (propagate = False) — per-request 200 lines are noise for fluentbit/OpenSearch, and the logging middleware reports the failures that matter.- Chatty libraries (
httpx,httpx2,httpcore,openai,urllib3,aiohttp) are capped atWARNING; override per app withinit_logger(library_log_levels={"httpx": "DEBUG"}). logging.captureWarnings(True)routesDeprecationWarning& friends through the pipeline instead of raw stderr.
Serving
Run with plain uvicorn, not fastapi run. The fastapi run Rich banner and handlers bypass the JSON pipeline:
uvicorn my_app.app:app --host 0.0.0.0 --port 8090 --no-access-logInstallation
The logger module is part of the dcc-backend-common package:
uv add dcc-backend-commonQuick Start
Initialize the logger once at your application's entry point:
# app.py
from contextlib import asynccontextmanager
from dcc_backend_common.logger import init_logger, get_logger
from fastapi import FastAPI
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan handler."""
init_logger(app_name="my-app") # Initialize once at startup
logger = get_logger(__name__)
logger.info("Application started")
yield
logger.info("Application shutting down")
app = FastAPI(lifespan=lifespan)Configuration
The logger is configured via environment variables:
| Variable | Default | Description |
|---|---|---|
LOG_LEVEL | INFO | Logging level for application diagnostics (DEBUG, INFO, WARNING, ERROR, CRITICAL). Does not affect usage events |
IS_PROD | - | Required. Set to true for JSON output (fluentbit compatible); init_logger() raises if it is missing |
DEV_TRACEBACK_STYLE | focused | Traceback style in development: focused (user code locals) or rich (all locals) |
LOGGER_USER_CODE_PATHS | dcc_backend_common, src/, app/, tests/ | Comma-separated paths to identify user code for detailed tracebacks |
Development vs Production
- Development (
IS_PRODnottrue): Colored console output for readability.- Traceback Styles:
focused(default): Shows the full Rich traceback but only displays local variables for frames identified as "user code" (based onLOGGER_USER_CODE_PATHS). This reduces noise from library internals.rich: Shows the full Rich traceback with local variables displayed for all frames, including third-party libraries. This is more verbose but useful for deep debugging.
- Traceback Styles:
- Production (
IS_PROD=true): One JSON line per record for log aggregation tools like fluentbit. Tracebacks are rendered into theexceptionstring field.eventkeeps only the closed vocabulary (EVENT_TYPES); any other text moves tomessage. Field types are enforced (see Field Types).
Recommended levels
LOG_LEVEL=debug on test stages, LOG_LEVEL=info in production.
Field Types
All apps share one OpenSearch index, so the production pipeline enforces the logging contract before rendering:
levelis one ofdebug,info,warning,error,critical(warn,fatal,traceand custom stdlib levels are mapped).- Custom fields are flat scalars. A dict, list, tuple or set under
keyis written as a JSON string tokey_jsonandkeyis dropped, so the string never clashes with an object that other apps or older versions still send underkey(OpenSearch rejects the whole line on a type clash). Other objects becomestr()under their own key (repr()if__str__fails);NaN/Infinityfloats become"nan"/"inf". Logstatus=503, notresponse={"status": 503}. error=excbecomesstr(exc)pluserror_type(the class name);exception=excbecomes the traceback string.status_codemust be an integer andduration_sa finite float (integers are emitted as floats). Lossless conversions are applied ("200"→200); anything else moves tostatus_code_raw/duration_s_rawas a string. Other reserved fields (message,error,path,request_id, ...) are coerced to strings. A caller-suppliedtimestamp,levelorloggeris overwritten with the real value.
Usage Events
Usage/audit events go through a separate, pinned logger named usage. init_logger() sets that logger to INFO explicitly, so its events survive any LOG_LEVEL — even LOG_LEVEL=WARNING.
from dcc_backend_common.logger import get_usage_logger
usage_logger = get_usage_logger()
usage_logger.info("app_event", action="translator.translate", pseudonym_id=..., chars=1200)Two producers already use it:
| Producer | Event | Emitted by |
|---|---|---|
UsageTrackingService.log_event() | app_event | Usage Tracking |
BaseAgent (every LLM call) | llm_call | LLM Agent |
In OpenSearch, filter on logger: "usage" to separate business events from diagnostics. The constant USAGE_LOGGER_NAME holds that name if you need it in code.
Keep diagnostics out
Do not route ordinary application logging through the usage logger — it is never silenced by LOG_LEVEL.
Usage
Getting a Logger
Use get_logger() in any module to get a structured logger:
from dcc_backend_common.logger import get_logger
# Pass the module name for context
logger = get_logger(__name__)
def process_translation(request: TranslationRequest) -> TranslationResult:
logger.info(
"Processing translation request",
source_lang=request.source_lang,
target_lang=request.target_lang,
text_length=len(request.text),
)
try:
result = translate(request)
logger.info("Translation completed", result_length=len(result.text))
return result
except TranslationError as e:
logger.exception(
"Translation failed",
error=str(e),
error_type=type(e).__name__,
translation_id=request.id, # request_id is reserved for the X-Request-ID UUID
)
raiseAutomatic Context
The logger automatically adds context to every log entry:
| Field | Description |
|---|---|
timestamp | ISO-8601 formatted timestamp (UTC) |
app | Service name from init_logger(app_name=...) (production) |
logger | The logger name passed to get_logger() |
module | The module name where the log was called |
func_name | The function name |
lineno | The line number |
level | One of debug, info, warning, error, critical |
message | Human-readable text (free-text event strings are moved here in production) |
event | Closed vocabulary (EVENT_TYPES), only for typed events |
request_id | Unique UUID for request tracing — only present when the logging middleware is installed |
Example JSON output in production:
{
"timestamp": "2026-11-20T08:15:30.123456Z",
"level": "info",
"app": "translator-backend",
"logger": "myapp.services.translator",
"request_id": "550e8400-e29b-41d4-a716-446655440000",
"module": "translator",
"func_name": "translate",
"lineno": 42,
"message": "Translation completed",
"result_length": 256
}Integration with FastAPI
Request Logging Middleware
Do not hand-roll request logging — the package ships a middleware that binds a request_id to every log line emitted while handling a request:
from dcc_backend_common.fastapi_logging_middleware import add_logging_middleware
add_logging_middleware(app)See Logging Middleware for details.
API Reference
DevTracebackStyle
Enum defining the available traceback styles for development mode.
| Value | Description |
|---|---|
FOCUSED | Rich traceback + focused locals for user code only (default) |
RICH | Default Rich traceback with full locals for all frames (verbose) |
FocusedTracebackFormatter
A custom exception formatter used in development mode.
- Shows detailed tracebacks for user code (based on
LOGGER_USER_CODE_PATHS). - Keeps library code traces dense and minimal.
- Appends a specific "Local variables in your code" section for user frames.
init_logger()
Initialize the logger configuration. Must be called once at application startup.
from dcc_backend_common.logger import init_logger
init_logger(app_name="my-app")Parameters:
app_name(str | None): Stamped asappon every production line.library_log_levels(dict[str, int | str] | None): Per-library level overrides, merged over theWARNINGdefault for chatty libraries.extra_event_types(set[str] | None): App-specific additions toEVENT_TYPES.
Environment Variables:
IS_PROD(required): "true" for production (JSON output), "false" for development (console output).LOG_LEVEL: Logging level (default: "INFO").DEV_TRACEBACK_STYLE: Traceback style in dev mode ("focused" or "rich").LOGGER_USER_CODE_PATHS: Comma-separated paths to consider as user code.
get_logger(name)
Get a structured logger instance.
from dcc_backend_common.logger import get_logger
logger = get_logger(__name__) # Pass module name for context
logger = get_logger() # Or get anonymous loggerParameters:
name(str | None): Optional name for the logger, typically__name__
Returns:
BoundLogger: A structlog bound logger instance
get_usage_logger()
Get the logger for usage/audit events. Events logged here are always emitted (INFO and up), regardless of LOG_LEVEL.
from dcc_backend_common.logger import get_usage_logger
usage_logger = get_usage_logger()
usage_logger.info("llm_call", input_tokens=812, output_tokens=134)Returns:
BoundLogger: A structlog bound logger namedusage
USAGE_LOGGER_NAME
Module-level constant holding the usage logger's name ("usage"). Use it when filtering log records or configuring downstream tooling.
EVENT_TYPES
frozenset of the event types allowed in the event field: app_event, llm_call, request_finished, request_failed, health check failed, health check recovered. Extend it per app with init_logger(extra_event_types={...}); in production any other event string moves to message.
Source Code
The full implementation is available on GitHub: dcc_backend_common/logger/logger.py
