Skip to content

refactor(backend): migrate to structured logging with createLogger#267

Merged
test3207 merged 1 commit intomainfrom
refactor/backend-structured-logging
Dec 27, 2025
Merged

refactor(backend): migrate to structured logging with createLogger#267
test3207 merged 1 commit intomainfrom
refactor/backend-structured-logging

Conversation

@test3207
Copy link
Owner

Summary

Migrate all backend logging to the unified structured format defined in #249. This ensures all logs follow a consistent structure with proper traceId for Loki/Grafana observability.

Changes

Logger Core (backend/src/lib/logger.ts)

  • Add generateTraceId() using crypto.randomUUID() for system logs without request context
  • Update createLogger(c?: Context) to auto-generate traceId when no Context provided
  • Update RequestLogger.warn to accept ErrorLogParams (for logging warnings with error objects)

Route Handlers (5 files)

  • routes/songs.ts - Song search, CRUD, streaming logs
  • routes/playlists.ts - Playlist management logs
  • routes/libraries.ts - Library management logs
  • routes/player.ts - Playback state logs
  • routes/user.ts - User preferences logs

Service Layer (4 files)

  • services/library.service.ts - Library operations
  • services/song.service.ts - Song operations
  • services/player.service.ts - Player state operations
  • services/upload.service.ts - Upload processing

Infrastructure (5 files)

  • lib/prisma.ts - Database connection logs
  • lib/redis.ts - Redis connection logs
  • lib/minio-client.ts - MinIO storage logs
  • lib/jwt.ts - JWT token logs
  • lib/auth-middleware.ts - Authentication logs

Startup/Shutdown (index.ts)

  • Shared startupTraceId for correlated startup logs

Demo Module (2 files)

  • lib/demo/storage-tracker.ts - Storage tracking logs
  • lib/demo/reset-service.ts - Reset service logs

Test Updates (4 files)

  • Update logger mocks to include createLogger export

Log Structure

All logs now follow this unified structure:

log.info({
  source: 'filename.functionName',  // e.g., 'songs.search'
  col1: 'category',                 // system, auth, library, playlist, song, player, upload, user, demo
  col2: 'action',                   // login, create, delete, update, search, etc.
  col3: 'entityId',                 // optional: songId, libraryId, playlistId
  raw: { ... },                     // optional: debug data
  message: 'Human readable message'
});

Testing

  • ✅ TypeScript: No errors
  • ✅ ESLint: No errors
  • ✅ Backend Tests: 160/160 passed
  • ✅ Frontend Tests: 411/411 passed
  • ✅ Build: Success

Related

Closes #249

Parent Epic: #190

- Add generateTraceId() for system logs without request context
- Update createLogger() to auto-generate traceId when no Context
- Migrate all route handlers to use createLogger(c) pattern
- Migrate all services to use createLogger() for system-level logs
- Migrate infrastructure modules (prisma, redis, minio, jwt, auth)
- Migrate startup/shutdown logs with shared traceId
- Update RequestLogger.warn to accept ErrorLogParams
- Update test mocks to include createLogger export

All logs now follow unified structure:
- source: 'filename.functionName'
- col1: business category (system, auth, library, etc.)
- col2: action (login, create, delete, etc.)
- col3: entity ID (optional)
- raw: debug data (optional)
- message: human-readable text

Closes #249
Copilot AI review requested due to automatic review settings December 27, 2025 06:01
@github-actions
Copy link
Contributor

Coverage after merging refactor/backend-structured-logging into main will be

60.62%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
100%100%100%100%
src/lib
   events.ts100%100%100%100%
   logger-client.ts40.67%33.33%48.39%46.49%129, 136–138, 138, 138–139, 145, 145, 145–146, 146, 146–147, 149, 171, 176, 182–183, 185, 190, 190, 190, 192, 192, 192, 194, 196, 196, 196–199, 199, 199, 205, 205, 205–207, 209, 221, 224–225, 225, 225–226, 228, 228, 228, 228–229, 229, 229–230, 234, 236, 242, 242, 246, 246–247, 247–248, 248–249, 249–250, 250–251, 251, 258–259, 259, 259–260, 265, 265, 265, 267–269, 275, 275, 275–276, 279, 298–299, 306–307, 310–311, 316, 318–319, 324, 326–327, 346, 346, 346–349, 353, 361, 361, 361, 365, 372, 375–376, 376, 376, 382–383, 383, 383, 389–390, 390, 390, 395, 395, 395, 397–398, 401, 401, 404, 407–408, 41, 411, 411, 46, 46–47, 47, 47, 51, 55
   shared.ts100%100%100%100%
src/lib/api
   config.ts75%62.50%100%85.71%17, 22, 22–23
   multi-region.ts2.21%0%0%3.81%103–104, 104, 104, 106, 109–110, 110, 110, 114, 116–119, 121–122, 126, 126, 126, 126, 126–127, 129–130, 130, 130, 132–133, 143, 145–146, 148–150, 154, 156, 156, 156, 158–159, 161, 172, 172, 172–173, 176, 179, 179, 179–180, 182–183, 187, 189, 189, 189–191, 191, 191–194, 196, 203, 203, 203, 203, 203–205, 208, 208, 208–209, 211–212, 216–218, 220, 220, 220–223, 229–230, 233, 240, 240, 240, 244, 244, 244–245, 247, 255, 266, 266, 266, 269, 269, 269–271, 275, 277, 279–281, 283, 283, 283, 283, 283–285, 289–290, 292, 296, 304–305, 50, 50, 50, 52, 56, 56, 56–57, 57, 57–59, 61, 65, 65, 65, 68, 68, 68, 68, 68, 70, 77, 85, 85, 85, 87–89, 89, 89, 91–92, 92, 92, 94
src/lib/audio
   media-session.ts74.84%90.91%71.43%65.12%107–108, 126, 137–139, 146–148, 150, 157–159, 161, 169–171, 173, 180–182, 184, 200, 214, 228, 241, 249–250, 263, 44–45, 78, 88–89, 95
   queue.ts86.15%82.54%92%86.92%210–211, 223, 225, 225, 225–226, 228, 249–252, 303, 313, 313, 313–314, 316, 37–38, 54–55, 72, 76–77
src/lib/cache
   metadata-cache.ts1%0%0%1.47%101–103, 106, 106, 108, 108, 123–124, 127–129, 131, 131, 131–133, 136–137, 139–140, 153–154, 157, 160, 163, 170–171, 173–174, 183–186, 188–189, 201–204, 204, 204–205, 207, 207, 207–209, 212, 214–215, 24–25, 25, 25, 32, 40–42, 44–45, 54–56, 58–59, 68–70, 72–73, 82–84, 86–87, 97–99
   response-cache.ts0%0%0%0%100, 32–33, 33, 33–34, 38–39, 39, 39, 41, 44, 46–47, 50, 50, 50, 50, 50–51, 54, 57, 57, 57, 57, 59, 59, 59–61, 61, 61–62, 64, 67, 67, 67–69, 69, 69–70, 72, 75, 75, 75–77, 77, 77, 79, 79, 79, 79, 79–81, 83–84, 84, 84, 86, 89, 92, 97
src/lib/offline-proxy/routes
   libraries.ts42.23%24.32%57.14%51.69%127–128, 213, 253, 266–268, 271, 273, 273, 273–274, 283, 283, 283, 283, 283–284, 293–294, 296, 296, 296–297, 301, 304–305, 308, 310, 310, 310, 312, 316, 316–317, 317, 321, 325, 328, 331, 331, 331–332, 343, 346, 349, 349, 349, 349, 349–350, 352, 357, 361, 364, 366, 369, 369–370, 370–371, 371–372, 372–373, 373–374, 374, 374, 374–375, 375–376, 376, 378, 378, 378, 378, 381, 381, 393–394, 397, 402, 402, 402–404, 404, 410, 420–421, 427–430, 430, 430, 432, 434, 434, 434, 434, 434–435, 445, 448, 450, 455, 66, 95, 95–96
   player.ts47.26%35%57.14%55.70%102, 138, 150–151, 154, 159, 161, 165, 167, 167, 167–169, 171, 171, 171–172, 180, 180–181, 181, 195, 200–201,

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR migrates backend logging from the legacy pino logger syntax to a unified structured format using createLogger(). The migration implements the specification from issue #249 to enable better observability with traceId correlation for Loki/Grafana.

Key Changes:

  • Enhanced logger core with generateTraceId() function and auto-generated traceIds for system logs
  • Updated RequestLogger.warn to accept ErrorLogParams for error object logging
  • Migrated 5 route handlers, 4 service modules, 5 infrastructure modules, startup/shutdown code, and 2 demo modules to structured logging format
  • Updated test mocks to export createLogger function

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
backend/src/lib/logger.ts Added generateTraceId(), updated createLogger() to auto-generate traceId when no Context provided, extended warn() to accept ErrorLogParams
backend/src/routes/songs.ts Migrated to createLogger(c) with structured log format for search, CRUD, and streaming operations
backend/src/routes/playlists.ts Migrated playlist management logs to structured format
backend/src/routes/libraries.ts Migrated library operations and upload processing logs
backend/src/routes/player.ts Migrated playback state logs to structured format
backend/src/routes/user.ts Migrated user preferences logs
backend/src/services/library.service.ts Migrated file and song creation logs
backend/src/services/song.service.ts Migrated file deletion logs
backend/src/services/player.service.ts Migrated playback seed, preferences, and progress logs
backend/src/services/upload.service.ts Migrated upload processing, metadata extraction, and cleanup logs
backend/src/lib/prisma.ts Migrated database connection logs with createLogger()
backend/src/lib/redis.ts Migrated Redis connection logs
backend/src/lib/minio-client.ts Migrated MinIO initialization logs
backend/src/lib/jwt.ts Migrated JWT configuration warning logs
backend/src/lib/auth-middleware.ts Migrated authentication verification logs
backend/src/index.ts Migrated startup/shutdown logs with shared startupTraceId for correlation
backend/src/lib/demo/storage-tracker.ts Migrated demo storage tracking logs
backend/src/lib/demo/reset-service.ts Migrated demo reset service logs with resetTraceId correlation
backend/src/test/routes/*.test.ts Updated logger mocks to export createLogger function

@test3207 test3207 merged commit bb67929 into main Dec 27, 2025
10 checks passed
@test3207 test3207 deleted the refactor/backend-structured-logging branch December 27, 2025 07:20
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.

Backend structured logging with traceId

1 participant