Skip to content

Add comprehensive structured logging and fix CI Docker build step#1

Merged
mayank1008-tech merged 2 commits into
mainfrom
copilot/add-structured-logging-to-services
Mar 15, 2026
Merged

Add comprehensive structured logging and fix CI Docker build step#1
mayank1008-tech merged 2 commits into
mainfrom
copilot/add-structured-logging-to-services

Conversation

Copilot AI commented Mar 15, 2026

Copy link
Copy Markdown
Contributor

Replaces ad-hoc System.out.println calls with production-grade SLF4J structured logging across all layers, and removes the slow Docker image build job from CI.

CI Fix

  • Removed build-docker job from .github/workflows/ci.yml — was blocking pipeline without value for unit-test-only workflows

Logging Infrastructure

  • logback.xml: Console + rolling file appender (100MB/day, 30-day retention), DEBUG for com.example.ledgersystem, WARN for Hibernate SQL
  • logback-test.xml: Console-only, DEBUG for app packages, WARN root

Service Layer

  • AccountServiceImpl — transfer/deposit/balance/statement: INFO on entry/exit, WARN on insufficient funds/unauthorized/duplicate, DEBUG for cache hits/misses, ERROR for missing accounts
  • AdminServiceImpl — audit: INFO start/result, WARN on broken chain or data modification, DEBUG per-entry
  • RateLimitingService — DEBUG on bucket key resolution and config creation
  • UserDetailsServiceImpl — DEBUG on lookup, WARN on not found
  • DataSeeder — replaced all System.out.println; INFO seed completion with account IDs

Controllers

  • AccountController, AuthController, AdminController — INFO on every API call with request params; WARN on rate-limit exceeded and rejected auth attempts

Security / JWT

  • AuthTokenFilter, AuthEntryPointJwt, JwtUtils — converted from manual LoggerFactory to @Slf4j; replaced System.out.println("Validate"); JWT exceptions downgraded from ERROR to WARN; auth failures log request path

Utilities & Exception Handling

  • AuthUtils — DEBUG on security context reads
  • HashUtils — static SLF4J logger; ERROR on NoSuchAlgorithmException
  • MyGlobalExceptionHandler — WARN log on every exception handler with message context

Log level strategy

Level Usage
DEBUG Cache state, JWT validation steps, internal flow
INFO Business events: transfers, logins, audits, seeds
WARN Insufficient funds, rate limits, duplicate tx, auth failures
ERROR Missing system accounts, unexpected exceptions

Example transfer log sequence:

INFO  Transfer initiated: fromAccount=abc-123, toAccount=xyz-789, amount=500.00, reference=ref-001, user=usr-456
DEBUG Sender account found: name=Alice, balance=1500.00, isCentralVault=false
DEBUG Cache invalidated for accounts: fromAccount=abc-123, toAccount=xyz-789
INFO  Transfer completed: reference=ref-001, amount=500.00, fromNewBalance=1000.00, toNewBalance=2500.00
Original prompt

Add Comprehensive Structured Logging

Overview

Add professional structured logging using SLF4J and Logback to all services, controllers, filters, and critical methods in the LedgerSystem project. This will enable production-ready debugging, audit trails, and operational visibility.

What Needs to Be Done

1. Create Logback Configuration

  • Create src/main/resources/logback.xml with:
    • Console appender for development
    • File appender with rolling policy (30 days retention, 100MB per day)
    • Different log levels for different packages
    • Proper formatting: timestamp, thread, level, logger name, message

2. Add Logging to All Service Classes

Add @Slf4j annotation and implement logging in:

  • AccountServiceImpl: Log all transfer, deposit, withdraw, getBalance operations with parameters and results
  • AdminServiceImpl: Log audit operations showing what was checked
  • RateLimitingService: Log rate limit hits and bucket creation
  • UserDetailsServiceImpl: Log user lookups and authentication attempts
  • DataSeeder: Log data initialization progress

3. Add Logging to All Controllers

Add @Slf4j annotation and implement logging in:

  • AccountController: Log all API calls (transfer, deposit, withdraw, statement, balance) with request params and responses
  • AuthController: Log signin, signup, signout attempts
  • AdminController: Log admin operations (if exists)
  • Include user context (userId, accountId) in logs

4. Add Logging to Filters and Security Classes

  • AuthTokenFilter: Log JWT extraction, validation, and authentication
  • AuthEntryPointJwt: Log authentication failures
  • JwtUtils: Log token generation and validation

5. Add Logging to Utilities and Other Critical Classes

  • AuthUtils: Log user extraction from security context
  • HashUtils: Log hash generation for audit trail integrity checks
  • Exception Handlers: Log exception details for debugging

Logging Strategy

Log Levels Used:

  • DEBUG: Internal state, cache hits/misses, detailed flow (development)
  • INFO: Important business events (transfers, logins, audits), successful operations
  • WARN: Unusual situations (rate limits hit, retry attempts, unauthorized attempts)
  • ERROR: Failures that need attention (exceptions, business logic violations)

Log Format:

timestamp [thread] LEVEL logger - message with structured parameters
2026-03-15 10:30:45 [main] INFO com.example.ledgersystem.service.AccountService - Transfer initiated: from=abc-123, to=xyz-789, amount=500.00

Context Information to Include:

  • User IDs (for audit trails)
  • Account IDs (for transaction tracking)
  • Transaction amounts and references (for compliance)
  • Execution times (for performance)
  • Error details (for debugging)
  • Request parameters (for troubleshooting)

File Structure Changes:

src/
├── main/
│   ├── java/com/example/ledgersystem/
│   │   ├── service/
│   │   │   ├── AccountServiceImpl.java (ADD LOGGING)
│   │   │   ├── AdminServiceImpl.java (ADD LOGGING)
│   │   │   ├── RateLimitingService.java (ADD LOGGING)
│   │   │   └── UserDetailsServiceImpl.java (ADD LOGGING)
│   │   ├── controller/
│   │   │   ├── AccountController.java (ADD LOGGING)
│   │   │   ├── AuthController.java (ADD LOGGING)
│   │   │   └── AdminController.java (if exists, ADD LOGGING)
│   │   ├── security/
│   │   │   ├── jwt/
│   │   │   │   ├── AuthTokenFilter.java (ADD LOGGING)
│   │   │   │   ├── AuthEntryPointJwt.java (ADD LOGGING)
│   │   │   │   └── JwtUtils.java (ADD LOGGING)
│   │   │   └── Services/
│   │   │       └── UserDetailsServiceImpl.java (ADD LOGGING)
│   │   ├── utils/
│   │   │   ├── AuthUtils.java (ADD LOGGING)
│   │   │   └── HashUtils.java (ADD LOGGING)
│   │   ├── exceptions/
│   │   │   └── MyGlobalExceptionHandler.java (ADD LOGGING)
│   │   └── DataSeeder.java (ADD LOGGING)
│   └── resources/
│       └── logback.xml (CREATE NEW)
└── test/
    └── resources/
        └── logback-test.xml (CREATE NEW for testing)

Example Logging in Methods

Transfer Operation:

[INFO] Transfer initiated: fromAccount=abc-123, toAccount=xyz-789, amount=500.00, reference=ref-001, user=user-123
[DEBUG] Checking if duplicate transaction...
[DEBUG] Sender account found: name=Alice, balance=1500.00
[DEBUG] Receiver account found: name=Bob, balance=2000.00
[INFO] Transfer completed successfully: amount=500.00, fromBalance=1000.00, toBalance=2500.00

Failed Transfer:

[INFO] Transfer initiated: fromAccount=abc-123, toAccount=xyz-789, amount=5000.00, reference=ref-002, user=user-123
[WARN] Insufficient funds: requested=5000.00, available=1500.00
[ERROR] Transfer failed: InsufficientFundsException, user=user-123

Authentication:

[DEBUG] JWT token found in request: path=/api/transfer
[INFO] Valid JWT received for user: alice
[DEBUG] User authenticated: alice with authorities=[ROLE_USER]

Rate Limiting:

[DEBUG] Rate limit check: userId=user-123,...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

*This pull request was created from Copilot chat.*
>

<!-- START COPILOT CODING AGENT TIPS -->
---

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. [Learn more about Advanced Security.](https://gh.io/cca-advanced-security)

…s, controllers, filters and utilities; fix CI workflow

Co-authored-by: mayank1008-tech <245725096+mayank1008-tech@users.noreply.github.com>
Copilot AI changed the title [WIP] Add comprehensive structured logging with SLF4J and Logback Add comprehensive structured logging and fix CI Docker build step Mar 15, 2026
Copilot AI requested a review from mayank1008-tech March 15, 2026 20:45
@mayank1008-tech mayank1008-tech marked this pull request as ready for review March 15, 2026 20:53
@mayank1008-tech mayank1008-tech merged commit 49e8e1f into main Mar 15, 2026
2 checks passed
@mayank1008-tech mayank1008-tech deleted the copilot/add-structured-logging-to-services branch March 15, 2026 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants