Skip to content

fix(backend): replace honoLogger with pino-based HTTP logger#277

Merged
test3207 merged 2 commits intomainfrom
fix/http-logger-json-format
Dec 28, 2025
Merged

fix(backend): replace honoLogger with pino-based HTTP logger#277
test3207 merged 2 commits intomainfrom
fix/http-logger-json-format

Conversation

@test3207
Copy link
Owner

Summary

Replace Hono's default honoLogger() middleware with a custom pino-based HTTP logger to output structured JSON logs.

Problem

The default honoLogger() outputs human-readable text format:

<-- GET /health
--> GET /health 200 2ms

This cannot be parsed by Grafana/Loki in production environments.

Solution

Created a custom HTTP logger middleware that uses the existing pino logger:

  • Outputs structured JSON with method, path, status, duration, traceId
  • Uses source: "http.request" and col1: "http" for log categorization
  • Development: pino-pretty renders human-readable output
  • Production: Raw JSON for Loki/Grafana parsing

Example Output

Development (pino-pretty):

[2025-12-28 19:34:05.092 +0800] INFO: GET /health 200 2ms
    service: "m3w-backend"
    source: "http.request"
    method: "GET"
    path: "/health"
    status: 200
    duration: 2
    traceId: "9577c11c-d242-4a41-a89a-8c4a73efe85a"

Production (JSON):

{"level":30,"time":1735389245092,"service":"m3w-backend","method":"GET","path":"/health","status":200,"duration":2,"traceId":"..."}

Changes

  • backend/src/lib/http-logger.ts - New HTTP logger middleware
  • backend/src/index.ts - Replace honoLogger() with custom middleware

Testing

  • npm test passes
  • Manual verification: HTTP requests logged in structured format
  • traceId properly generated and included

Closes #274

- Create custom HTTP logger middleware using existing pino logger
- Output structured JSON with method, path, status, duration, traceId
- Remove hono/logger import (no longer needed)
- Enables proper parsing by Grafana/Loki in production

Closes #274
Copilot AI review requested due to automatic review settings December 28, 2025 11:35
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 replaces Hono's default text-based logger middleware with a custom pino-based HTTP logger that outputs structured JSON logs suitable for parsing by Grafana/Loki in production environments. The implementation integrates with the existing trace middleware to include traceIds in HTTP request logs.

Key changes:

  • Created a new HTTP logger middleware using the existing pino logger infrastructure
  • Replaced honoLogger() with httpLogger() in the middleware chain
  • Logs include structured fields (method, path, status, duration, traceId) for observability

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
backend/src/lib/http-logger.ts New HTTP logger middleware that wraps requests and logs them with structured pino output, including status-based log levels
backend/src/index.ts Removed Hono's default logger import and replaced with custom httpLogger middleware

@github-actions
Copy link
Contributor

Coverage after merging fix/http-logger-json-format into main will be

72.72%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
100%100%100%100%
src/lib
   events.ts100%100%100%100%
   logger-client.ts35.65%28.77%48.48%39.86%101, 101, 101, 101, 101–104, 115, 120, 120–121, 121, 121, 125, 129, 205, 212–214, 214, 214–215, 221, 221, 221–222, 222, 222–223, 225, 247, 252, 257, 257, 257, 259, 259, 259, 261, 263, 263, 263–266, 266, 266, 272, 272, 272–274, 276, 288, 291–292, 292, 292–293, 295, 295, 295, 295–296, 296, 296–297, 301, 307, 307, 311, 311–312, 312–313, 313–314, 314–315, 315–316, 316, 323–324, 324, 324–325, 330, 330, 330, 332–334, 340, 340, 340–341, 344, 363–364, 371–372, 375–376, 381, 383–384, 389, 391–392, 411, 411, 411–414, 418, 426, 426, 426, 430, 437, 440–441, 441, 441, 447–448, 448, 448, 454–455, 455, 455, 460, 460, 460, 462–463, 466, 466, 469, 472–473, 476, 476, 54–56, 56, 56, 61, 61, 61–62, 66, 66, 66, 69–70, 72, 72, 72, 74, 74, 74, 74, 74, 74–77, 80, 80, 80–83, 87–88, 88, 88–90, 93–94
   shared.ts100%100%100%100%
src/lib/api
   config.ts75%62.50%100%85.71%17, 22, 22–23
src/lib/audio
   media-session.ts75.16%90.91%71.43%65.91%109–110, 143, 156–158, 165–167, 169, 176–178, 180, 188–190, 192, 199–201, 203, 219, 233, 247, 260, 268–269, 282, 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/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, 207–208, 208, 208–209, 217, 217–218, 218, 231, 236, 259, 280–283, 294, 306–309, 311, 313, 313, 313–314, 314, 314–315, 315, 315–316, 316, 316, 320, 322, 327, 57–59, 59, 59, 59, 59–60, 64–65, 72, 74, 94
   playlists.ts86.27%92.31%91.67%84.35%102, 121, 138, 155, 175, 189, 195–196, 215, 228, 234, 59, 65–70, 72, 89
   songs.ts90.48%75%100%95.24%104, 106, 113, 115, 143–144
src/lib/offline-proxy/utils
   auth.ts93.33%100%100%89.47%70–71
   index.ts100%100%100%100%
   sorting.ts100%100%100%100%
src/lib/storage
   storage-constants.ts100%100%100%100%
src/lib/utils
   audio-filter.ts100%100%100%100%
   defaults.ts100%100%100%100%
   format-duration.ts100%100%100%100%
   format.ts100%100%100%100%
   hash.ts88.24%100%100%85.19%62–63, 84–85
   uuid.ts90%87.50%83.33%93.75%31–32
src/locales
   i18n.ts95.45%87.50%100%97.37%23, 40–41

@test3207 test3207 merged commit e50dac5 into main Dec 28, 2025
4 checks passed
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.

fix: Backend HTTP logger outputs text instead of JSON in production

2 participants