Structured Logging: Logs That Actually Help You Debug
March 12, 2026 · 6 min · 1077 words · Rob Washington
Table of Contents
Your logs are probably useless. Not because you’re not logging, but because you’re logging wrong. Here’s how to make logs that actually help when things break.
importstructlog# Configure once at startupstructlog.configure(processors=[structlog.processors.TimeStamper(fmt="iso"),structlog.processors.add_log_level,structlog.processors.JSONRenderer()],wrapper_class=structlog.BoundLogger,context_class=dict,logger_factory=structlog.PrintLoggerFactory(),)log=structlog.get_logger()# Usagelog.info("user_login",user_id="u_789",ip="192.168.1.100")log.error("payment_failed",user_id="u_789",error="card_declined",amount=99.99)# Bind context for the requestlog=log.bind(request_id="req_123",user_id="u_789")log.info("order_created",order_id="ord_456")# Includes request_id and user_id
packagemainimport("os""github.com/rs/zerolog""github.com/rs/zerolog/log")funcmain(){zerolog.TimeFieldFormat=zerolog.TimeFormatUnixlog.Logger=zerolog.New(os.Stdout).With().Timestamp().Logger()// Usagelog.Info().Str("user_id","u_789").Str("ip","192.168.1.100").Msg("user_login")log.Error().Str("user_id","u_789").Str("error","card_declined").Float64("amount",99.99).Msg("payment_failed")// With contextreqLog:=log.With().Str("request_id","req_123").Str("user_id","u_789").Logger()reqLog.Info().Str("order_id","ord_456").Msg("order_created")}
# Python middleware exampleimportuuidfromcontextvarsimportContextVarrequest_context:ContextVar[dict]=ContextVar('request_context',default={})deflogging_middleware(request,call_next):ctx={'request_id':str(uuid.uuid4()),'user_id':request.user.idifrequest.userelseNone,'path':request.path,'method':request.method,}request_context.set(ctx)response=call_next(request)returnresponse# In your logger config, include contextdefadd_request_context(logger,method_name,event_dict):ctx=request_context.get()event_dict.update(ctx)returnevent_dict
Now every log in that request automatically includes request_id, user_id, etc.
Something unexpected but handled: retries, fallbacks
error
Something failed and needs attention
fatal
Application cannot continue
1
2
3
4
5
6
# Good level choiceslog.debug("cache_lookup",key="user:123",hit=True)log.info("order_created",order_id="ord_456",total=99.99)log.warn("rate_limit_approaching",current=95,limit=100)log.error("payment_failed",error="timeout",retry_count=3)log.fatal("database_connection_failed",host="db.example.com")