refactor(backend): migrate to structured logging with createLogger#267
Merged
refactor(backend): migrate to structured logging with createLogger#267
Conversation
- 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
Contributor
There was a problem hiding this comment.
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.warnto acceptErrorLogParamsfor 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
createLoggerfunction
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)generateTraceId()usingcrypto.randomUUID()for system logs without request contextcreateLogger(c?: Context)to auto-generate traceId when no Context providedRequestLogger.warnto acceptErrorLogParams(for logging warnings with error objects)Route Handlers (5 files)
routes/songs.ts- Song search, CRUD, streaming logsroutes/playlists.ts- Playlist management logsroutes/libraries.ts- Library management logsroutes/player.ts- Playback state logsroutes/user.ts- User preferences logsService Layer (4 files)
services/library.service.ts- Library operationsservices/song.service.ts- Song operationsservices/player.service.ts- Player state operationsservices/upload.service.ts- Upload processingInfrastructure (5 files)
lib/prisma.ts- Database connection logslib/redis.ts- Redis connection logslib/minio-client.ts- MinIO storage logslib/jwt.ts- JWT token logslib/auth-middleware.ts- Authentication logsStartup/Shutdown (
index.ts)startupTraceIdfor correlated startup logsDemo Module (2 files)
lib/demo/storage-tracker.ts- Storage tracking logslib/demo/reset-service.ts- Reset service logsTest Updates (4 files)
createLoggerexportLog Structure
All logs now follow this unified structure:
Testing
Related
Closes #249
Parent Epic: #190