application-logging

A set of Java rules for application logging with SLF4J and Logback, producing structured JSON that logging systems can search and aggregate.

In plain words
What is it for?
Use it when adding session, activity, debug, or error logs to a Java application.
Why use it?
It keeps logs consistent and avoids exposing unsafe or poorly encoded user-provided data.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/e-gov/cursor-prompts/application-logging
Clone the repo
git clone --depth 1 https://github.com/e-gov/cursor-prompts
Per session 2,320 This file is loaded in full into every session.
When invoked 2,320 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.02320 $0.02320
Opus 5 $0.01160 $0.01160
Sonnet 5 $0.00464 $0.00464
Haiku 4.5 $0.00232 $0.00232

Measured 2d ago against content hash 434ebf52ba3a, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

application-logging scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules/java-common/application-logging.mdc · 162 lines

How it starts

The opening of the file, as written. The whole thing — 162 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Java Application Logging Standards (JSON/Logstash Format)

Context

  • Apply this rule when implementing logging (Session, Activity, Debug, Error) in Java applications using the SLF4j API and configured with Logback and net.logstash.logback.encoder.LogstashEncoder.
  • This rule complements, but does not replace, mdc:rules/java-common/audit-logging.mdc, which covers specific security audit events. General principles here (like forbidden data) apply unless overridden by the audit rule.
  • For comprehensive security best practices, see [security.mdc](mdc:rules/common/security.mdc).
  • The goal is consistent, informative, and secure application-level logging in a structured JSON format suitable for log aggregation systems (like ELK stack).

Requirements

  1. Logging Facade: Use the SLF4j API for all logging statements.
  2. Encoding: Logback configuration ensures UTF-8 (default for LogstashEncoder).
  3. Language: Use English for log messages and field names where possible. The log message itself goes into the message field in the JSON output.
  4. User Input Handling:
    • Encode non-printable characters and line separators in user-provided data if they are included directly in log messages. LogstashEncoder generally handles JSON encoding for standard fields.
    • Be mindful of log injection if constructing message strings dynamically with user input. Use parameterized logging (log.info("User {} action", userInput)).
  5. Correlation ID: Implement and consistently use a unique correlation ID via MDC's requestId key. This MUST be populated for all logs within a request scope, typically set up in a web filter or interceptor.
  6. Log Levels, Categories & Locations:
    • Request Boundaries (Filter/Interceptor Recommended):
      • Log request start at INFO level, including requestId, clientIp, HTTP method, path.
      • Log request completion at INFO level, including requestId, status code, duration.
    • Controllers:
      • Log validation errors at WARN level.
      • (Optional) Log entry to complex controller methods at DEBUG level.
      • Log unhandled exceptions caught by @ControllerAdvice at ERROR level.
    • Services:
      • Log INFO (Activity Log) for significant business events (e.g., object creation/update, state transitions) and interactions with external systems. Include relevant context (e.g., objectId, eventType via MDC).
      • Log INFO (Session Log) for authentication/authorization events (logins, logouts, permission changes - excluding forbidden data).
      • Log WARN for handled business exceptions or known error conditions.
      • Log ERROR for unexpected exceptions during service execution.
      • Use DEBUG judiciously for detailed internal logic flow tracing in complex methods.
    • Other Components: Limit logging in repositories/DAOs primarily to ERROR for specific data access failures. Use framework-level SQL logging if needed.
  7. Contextual Fields (via MDC): Populate MDC for relevant context. The configured LogstashEncoder automatically includes standard fields. Ensure the following are consistently populated at the appropriate layers:
    • requestId: (Mandatory) Unique ID for the request/transaction. Typically set in a filter/interceptor.
    • sessionId: Session identifier, if available. Set in filter/interceptor.
    • userId: Identifier of the acting user (WHO). Set in filter/interceptor after authentication.
    • clientIp: Source IP address of the request (WHENCE). Set in filter/interceptor.
    • applicationName: Identifier for the application/service (WHERE). Set globally or via MDC.
    • (Recommended) Consider adding these fields via MDC within services/controllers for richer context where relevant:
      • eventType: Standardized identifier for the business event (e.g., USER_LOGIN, ORDER_CREATED).
      • objectId: Identifier of the primary business object involved.
      • eventResult: Outcome of a specific operation (SUCCESS, FAILURE, VALIDATION_ERROR).
      • traceId / spanId: Distributed tracing identifiers when OpenTelemetry is enabled (helps correlate logs with traces).
      • Note: If using OpenTelemetry, prefer automatic MDC population via OTel instrumentation or a Logback OTel appender; otherwise, set these via a web filter/interceptor so they are included in logs.
  8. Standard JSON Fields: Be aware that LogstashEncoder automatically adds fields like:
    • @timestamp: Event timestamp (WHEN). ISO 8601 format.
    • @version: Logstash schema version (usually 1).
    • message: The formatted log message string.
    • logger_name: The name of the SLF4j logger.
    • thread_name: Name of the logging thread.
    • level: Log level (e.g., "INFO").
    • level_value: Numeric log level.
    • stack_trace: Included for logs with exceptions at ERROR level.
  9. Forbidden Data: Strictly avoid logging data listed in the "Forbidden Data" section below, whether in the message field or any custom MDC fields.
  10. Log Format: The format is JSON, generated by net.logstash.logback.encoder.LogstashEncoder based on the Logback configuration (logback-spring.xml). No specific PatternLayout is needed for the main application logs.
  11. Documentation: Document service-specific log events, important custom MDC fields used, and any deviations from this standard.
  12. Background Process Logging: Log all background process activities (scheduled jobs, batch processing, data synchronization) using appropriate log levels (INFO for normal operation, WARN for issues, ERROR for failures).
  13. Success/Failure Logging: Log both successful and failed operations using appropriate log levels (INFO for success, WARN for handled errors, ERROR for unexpected failures).

Read the full file on GitHub · 162 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 162 lines · 2,320 tokens per session scan A 434ebf52ba3a

Subscribe to this mod's changes

application-logging is a cursor rule published in the GitHub repository e-gov/cursor-prompts (34 stars, last pushed 4mo ago), licensed MIT. It adds 2,320 tokens to every session, about $0.0116 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.