Add Production Logging
Objective
Improve production diagnosability with purposeful, searchable events. Use metrics
to reveal problems, traces to locate latency and failures, and logs to explain
identifiers, decisions, state changes, warnings, and failures. Do not create
mechanical logging merely to ensure that every class declares a logger.
Scope modes
| Request |
Action |
| Analyze or audit |
Report useful coverage, duplication, sensitive-data risks, and infrastructure gaps without editing. |
| Plan |
Specify exact event locations, levels, messages, fields, omissions, tests, and acceptance criteria. |
| Implement |
Add or improve the smallest useful set of logs without changing business behavior. |
| Review |
Inspect the diff for diagnostic value, duplication, severity, stack traces, performance, and data exposure. |
Workflow
- Read repository instructions and preserve unrelated work.
- Identify the logging facade and implementation, configuration, appenders,
output format, environment-specific levels, and nearby coding conventions.
- Inspect centralized HTTP request logging, correlation or trace ID creation,
MDC propagation, asynchronous boundaries, and cleanup.
- Inspect global exception handling. Determine where unhandled exceptions and
stack traces are already logged before adding failure events elsewhere.
- Trace the requested business operations, state changes, rejected decisions,
external calls, retries, timeouts, fallbacks, and partial failures.
- Inspect corresponding legacy code when relevant. Preserve meaningful
operational, business, security, and audit events unless an approved decision
changes them.
- Identify credentials, tokens, secrets, personal data, document contents, and
request or response data that must be omitted or masked.
- Plan each event's exact location, level, stable message, safe fields, and
ownership before editing. Record why apparently plausible events should be
omitted.
- Add or improve only events that help an operator understand a meaningful
decision, transition, integration, abnormal condition, or failure.
- Run the smallest relevant tests and static checks. Review the diff for
business-behavior changes, accidental data exposure, duplicate events,
repeated stack traces, and avoidable runtime cost.
Technology selection
Prefer the repository's established logging facade, implementation,
configuration, and conventions. Do not add, replace, or upgrade logging
dependencies unless the user explicitly requests a logging-stack change.
When no production-capable logging stack exists, report the gap instead of
silently choosing a technology. Propose options based on the language and
framework, supported dependency versions, deployment platform, output and
ingestion requirements, structured-field support, correlation or tracing
integration, performance, and operational ownership.
When the user requests a new or replacement stack:
- Separate the application-facing logging API from its implementation,
formatting, transport, and storage concerns.
- Prefer technology already compatible with the framework and telemetry
pipeline over introducing a parallel ecosystem.
- Explain the selected option, material alternatives, and tradeoffs before
making broad cross-cutting changes.
- Add only the dependencies and configuration required for the requested
scope, then verify startup, production output, level controls, and failure
behavior.
Event ownership
Central HTTP layer
Use an existing or explicitly requested filter or interceptor for normal request
lifecycle information:
- HTTP method and canonical route or safe request URI
- response status and duration
- safe authenticated user or client identifier
- correlation or trace ID
- canonical
/rest/... versus legacy compatibility traffic
Do not repeat these fields independently in every controller. Do not introduce
new cross-cutting infrastructure during a narrowly scoped logging change unless
the user requests it; report the gap instead.
Global exception handler
Use centralized exception handling for consistent unexpected-exception logging,
safe request context, correlation ID, stack trace ownership, and safe HTTP
response mapping. Log a complete stack trace once where an exception becomes
unhandled. Do not log the same exception with a stack trace at every layer.
Controllers
Keep thin controllers free of routine lifecycle logs. Add a controller event
only for a meaningful decision owned by the HTTP boundary and not better placed
in a service, filter, security component, or exception handler. A controller
with no local logger is acceptable when centralized and service logging provide
sufficient observability.
Services
Add service events when they provide production value for:
- significant business commands and completed state transitions
- rejected operations and important validation outcomes
- outbound integration attempts and outcomes
- retries, timeouts, fallbacks, circuit breakers, and degraded behavior
- partial failures or suspicious but recoverable conditions
- idempotency, duplicate-detection, or conflict decisions
- failures not already captured centrally with sufficient context
Avoid logging expected success for high-volume read-only operations unless it is
an operational milestone or approved audit event.
Repositories
Do not log ordinary repository entry, exit, or successful queries. Rely on
database metrics and framework diagnostics for routine persistence activity.
Add an application event only for domain-relevant persistence outcomes, custom
repair behavior, or unusual fallback logic that cannot be understood at a
higher layer.
Levels
ERROR: Log an unexpected failure requiring investigation or intervention.
Include its stack trace once.
WARN: Log a recoverable abnormal condition, suspicious or rejected
operation, exhausted retry, or degraded behavior.
INFO: Log a meaningful business event, state transition, integration
outcome, or operational milestone useful under normal production levels.
DEBUG: Log investigation detail that is too noisy for normal production.
TRACE: Reserve for exceptional low-level troubleshooting; do not introduce
it routinely.
Choose severity from the event, not the method or class type. Follow stricter
project-specific level conventions when present.
Message and field rules
- Prefer stable event wording and parameterized or structured fields supported
by the current logging stack.
- Describe what happened instead of merely naming the execution location.
- Include safe, stable identifiers such as document ID, operation ID,
customer or client ID, user ID, and correlation ID when available.
- Use MDC for correlation or trace IDs when existing infrastructure supports it.
- Make success, rejection, degradation, and failure events distinguishable and
searchable.
- Avoid string concatenation when parameterized logging is available.
- Guard expensive diagnostic computations when the logging API does not defer
their evaluation.
- Do not dump complete objects, collections, documents, requests, responses, or
exception payloads.
- Avoid routine method-entry and method-exit events.
- Avoid duplicate events across filters, controllers, services, repositories,
integration clients, and exception handlers.
Sensitive data
Never log:
- passwords or credentials
- authentication or authorization headers
- access tokens, refresh tokens, session IDs, or API keys
- encryption keys or other secrets
- full personal data when an internal identifier is sufficient
- complete customs, invoice, waybill, or other sensitive document contents
- unfiltered request or response bodies
Apply the repository's masking policy when one exists. When uncertain, omit the
value and log only a safe identifier. Treat exception messages and third-party
payloads as potentially sensitive rather than automatically safe.
Verification
Verify behavior with focused tests that exercise success, rejection, recoverable
abnormal conditions, and unexpected failure as applicable. Capture emitted
events only when tests can do so without coupling to incidental formatting.
Check that:
- the right layer owns each event
- required identifiers appear and forbidden data does not
- expected failures do not produce repeated stack traces
- disabled diagnostic logging avoids expensive work
- changes do not alter return values, exceptions, transactions, retries, or
other business behavior
Handoff
Report:
- logs added, changed, removed, or re-leveled
- useful logs intentionally omitted and which layer already owns the context
- remaining request logging, correlation, metrics, tracing, dashboard, alerting,
or configuration gaps
- sensitive-data risks found and how they were handled
- tests and static checks run
1---2name: add-production-logging3description: Audit, plan, add, improve, or review production logging and observability for a selected class, package, migrated operation, module, migration stage, or complete application. Use when Codex needs to preserve meaningful legacy logs, add diagnostic context to business operations and integrations, remove duplicate or unsafe logging, or perform a logging and observability audit without changing business behavior.4---56# Add Production Logging78## Objective910Improve production diagnosability with purposeful, searchable events. Use metrics11to reveal problems, traces to locate latency and failures, and logs to explain12identifiers, decisions, state changes, warnings, and failures. Do not create13mechanical logging merely to ensure that every class declares a logger.1415## Scope modes1617| Request | Action |18|---|---|19| Analyze or audit | Report useful coverage, duplication, sensitive-data risks, and infrastructure gaps without editing. |20| Plan | Specify exact event locations, levels, messages, fields, omissions, tests, and acceptance criteria. |21| Implement | Add or improve the smallest useful set of logs without changing business behavior. |22| Review | Inspect the diff for diagnostic value, duplication, severity, stack traces, performance, and data exposure. |2324## Workflow25261. Read repository instructions and preserve unrelated work.272. Identify the logging facade and implementation, configuration, appenders,28 output format, environment-specific levels, and nearby coding conventions.293. Inspect centralized HTTP request logging, correlation or trace ID creation,30 MDC propagation, asynchronous boundaries, and cleanup.314. Inspect global exception handling. Determine where unhandled exceptions and32 stack traces are already logged before adding failure events elsewhere.335. Trace the requested business operations, state changes, rejected decisions,34 external calls, retries, timeouts, fallbacks, and partial failures.356. Inspect corresponding legacy code when relevant. Preserve meaningful36 operational, business, security, and audit events unless an approved decision37 changes them.387. Identify credentials, tokens, secrets, personal data, document contents, and39 request or response data that must be omitted or masked.408. Plan each event's exact location, level, stable message, safe fields, and41 ownership before editing. Record why apparently plausible events should be42 omitted.439. Add or improve only events that help an operator understand a meaningful44 decision, transition, integration, abnormal condition, or failure.4510. Run the smallest relevant tests and static checks. Review the diff for46 business-behavior changes, accidental data exposure, duplicate events,47 repeated stack traces, and avoidable runtime cost.4849## Technology selection5051Prefer the repository's established logging facade, implementation,52configuration, and conventions. Do not add, replace, or upgrade logging53dependencies unless the user explicitly requests a logging-stack change.5455When no production-capable logging stack exists, report the gap instead of56silently choosing a technology. Propose options based on the language and57framework, supported dependency versions, deployment platform, output and58ingestion requirements, structured-field support, correlation or tracing59integration, performance, and operational ownership.6061When the user requests a new or replacement stack:62631. Separate the application-facing logging API from its implementation,64 formatting, transport, and storage concerns.652. Prefer technology already compatible with the framework and telemetry66 pipeline over introducing a parallel ecosystem.673. Explain the selected option, material alternatives, and tradeoffs before68 making broad cross-cutting changes.694. Add only the dependencies and configuration required for the requested70 scope, then verify startup, production output, level controls, and failure71 behavior.7273## Event ownership7475### Central HTTP layer7677Use an existing or explicitly requested filter or interceptor for normal request78lifecycle information:7980- HTTP method and canonical route or safe request URI81- response status and duration82- safe authenticated user or client identifier83- correlation or trace ID84- canonical `/rest/...` versus legacy compatibility traffic8586Do not repeat these fields independently in every controller. Do not introduce87new cross-cutting infrastructure during a narrowly scoped logging change unless88the user requests it; report the gap instead.8990### Global exception handler9192Use centralized exception handling for consistent unexpected-exception logging,93safe request context, correlation ID, stack trace ownership, and safe HTTP94response mapping. Log a complete stack trace once where an exception becomes95unhandled. Do not log the same exception with a stack trace at every layer.9697### Controllers9899Keep thin controllers free of routine lifecycle logs. Add a controller event100only for a meaningful decision owned by the HTTP boundary and not better placed101in a service, filter, security component, or exception handler. A controller102with no local logger is acceptable when centralized and service logging provide103sufficient observability.104105### Services106107Add service events when they provide production value for:108109- significant business commands and completed state transitions110- rejected operations and important validation outcomes111- outbound integration attempts and outcomes112- retries, timeouts, fallbacks, circuit breakers, and degraded behavior113- partial failures or suspicious but recoverable conditions114- idempotency, duplicate-detection, or conflict decisions115- failures not already captured centrally with sufficient context116117Avoid logging expected success for high-volume read-only operations unless it is118an operational milestone or approved audit event.119120### Repositories121122Do not log ordinary repository entry, exit, or successful queries. Rely on123database metrics and framework diagnostics for routine persistence activity.124Add an application event only for domain-relevant persistence outcomes, custom125repair behavior, or unusual fallback logic that cannot be understood at a126higher layer.127128## Levels129130- `ERROR`: Log an unexpected failure requiring investigation or intervention.131 Include its stack trace once.132- `WARN`: Log a recoverable abnormal condition, suspicious or rejected133 operation, exhausted retry, or degraded behavior.134- `INFO`: Log a meaningful business event, state transition, integration135 outcome, or operational milestone useful under normal production levels.136- `DEBUG`: Log investigation detail that is too noisy for normal production.137- `TRACE`: Reserve for exceptional low-level troubleshooting; do not introduce138 it routinely.139140Choose severity from the event, not the method or class type. Follow stricter141project-specific level conventions when present.142143## Message and field rules144145- Prefer stable event wording and parameterized or structured fields supported146 by the current logging stack.147- Describe what happened instead of merely naming the execution location.148- Include safe, stable identifiers such as document ID, operation ID,149 customer or client ID, user ID, and correlation ID when available.150- Use MDC for correlation or trace IDs when existing infrastructure supports it.151- Make success, rejection, degradation, and failure events distinguishable and152 searchable.153- Avoid string concatenation when parameterized logging is available.154- Guard expensive diagnostic computations when the logging API does not defer155 their evaluation.156- Do not dump complete objects, collections, documents, requests, responses, or157 exception payloads.158- Avoid routine method-entry and method-exit events.159- Avoid duplicate events across filters, controllers, services, repositories,160 integration clients, and exception handlers.161162## Sensitive data163164Never log:165166- passwords or credentials167- authentication or authorization headers168- access tokens, refresh tokens, session IDs, or API keys169- encryption keys or other secrets170- full personal data when an internal identifier is sufficient171- complete customs, invoice, waybill, or other sensitive document contents172- unfiltered request or response bodies173174Apply the repository's masking policy when one exists. When uncertain, omit the175value and log only a safe identifier. Treat exception messages and third-party176payloads as potentially sensitive rather than automatically safe.177178## Verification179180Verify behavior with focused tests that exercise success, rejection, recoverable181abnormal conditions, and unexpected failure as applicable. Capture emitted182events only when tests can do so without coupling to incidental formatting.183Check that:184185- the right layer owns each event186- required identifiers appear and forbidden data does not187- expected failures do not produce repeated stack traces188- disabled diagnostic logging avoids expensive work189- changes do not alter return values, exceptions, transactions, retries, or190 other business behavior191192## Handoff193194Report:195196- logs added, changed, removed, or re-leveled197- useful logs intentionally omitted and which layer already owns the context198- remaining request logging, correlation, metrics, tracing, dashboard, alerting,199 or configuration gaps200- sensitive-data risks found and how they were handled201- tests and static checks run