Skip to content

replace gin logger for requests #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2025
Merged

Conversation

iuwqyir
Copy link
Collaborator

@iuwqyir iuwqyir commented Jun 9, 2025

TL;DR

Replace Gin's default logger with a custom zerolog-based logger middleware.

What changed?

  • Created a new Logger() middleware function in internal/middleware/logger.go that uses zerolog for structured logging
  • Modified cmd/api.go to use the custom logger middleware instead of Gin's default logger
  • The new logger captures request details including path, status code, method, client IP, latency, and any error messages

How to test?

  1. Run the API server
  2. Make some HTTP requests to various endpoints
  3. Verify that the logs are now structured and contain all the expected fields (path, status, method, IP, latency, etc.)
  4. Check that the log format matches zerolog's output format

Why make this change?

The custom zerolog-based logger provides more structured logging compared to Gin's default logger. This makes logs easier to parse, filter, and analyze, especially in production environments. Using zerolog consistently throughout the application also ensures a uniform logging format.

Summary by CodeRabbit

  • New Features

    • Introduced enhanced request logging for the API, providing more detailed and structured logs for incoming HTTP requests.
  • Refactor

    • Replaced the default request logger with a custom logging middleware for improved log clarity and consistency.

Copy link

coderabbitai bot commented Jun 9, 2025

Walkthrough

The changes introduce a custom Gin middleware for logging HTTP requests using zerolog, replacing the default Gin logger in the API server setup. The new middleware logs detailed request information, including latency and errors, at the debug level for each incoming request.

Changes

File(s) Change Summary
cmd/api.go Switched Gin's default logger middleware to a custom logger from the middleware package.
internal/middleware/logger.go Added a new Logger middleware using zerolog for structured HTTP request logging.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant GinRouter
    participant LoggerMiddleware
    participant Handler

    Client->>GinRouter: Sends HTTP request
    GinRouter->>LoggerMiddleware: Passes request
    LoggerMiddleware->>Handler: Forwards request after logging start
    Handler-->>LoggerMiddleware: Handles request, returns response
    LoggerMiddleware-->>GinRouter: Logs details, passes response
    GinRouter-->>Client: Sends HTTP response
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Collaborator Author

iuwqyir commented Jun 9, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@iuwqyir iuwqyir marked this pull request as ready for review June 9, 2025 20:52
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 03325a3 and 4c22ffb.

📒 Files selected for processing (2)
  • cmd/api.go (1 hunks)
  • internal/middleware/logger.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
cmd/api.go (1)
internal/middleware/logger.go (1)
  • Logger (11-50)
🔇 Additional comments (2)
internal/middleware/logger.go (1)

3-8: LGTM on imports and package structure.

The imports are appropriate and the package structure follows Go conventions.

cmd/api.go (1)

51-51:

❓ Verification inconclusive

Verify log level configuration for production compatibility.

The replacement of gin.Logger() with middleware.Logger() is implemented correctly, but there's an important operational consideration: the custom middleware logs at debug level while Gin's default logger typically logs at info level.

Please verify that the application's log level configuration will allow debug-level logs to be visible in production, or consider updating the custom middleware to use info level logging.


🏁 Script executed:

#!/bin/bash
# Description: Check for log level configuration in the codebase
# Expected: Find configuration that sets log level, verify if debug logs will be visible

echo "Searching for log level configuration..."
rg -i "level|loglevel|log_level" --type go -A 2 -B 2

echo -e "\nSearching for zerolog level configuration..."
rg "zerolog.*level|level.*zerolog" --type go -A 2 -B 2

echo -e "\nSearching for environment variable usage for logging..."
rg "LOG_LEVEL|LOGLEVEL" --type go -A 2 -B 2

Length of output: 3065


Ensure HTTP logs are visible with the new middleware

The switch from gin.Logger() to your custom middleware.Logger() means request logs are emitted at debug level, but by default the global log level is set to WARN (see internal/log/logger.go). If config.Cfg.Log.Level (backed by the --log-level flag in cmd/root.go and configs/config.go) isn’t explicitly set to debug, those logs will be suppressed.

Please verify one of the following:

  • You’re configuring log.level to debug in production so that HTTP access logs remain visible.
  • Or update middleware.Logger() to emit at info level instead, matching Gin’s default behavior.

Relevant locations:

  • cmd/api.go (L51): replaced gin.Logger()middleware.Logger()
  • internal/log/logger.go: default zerolog.WarnLevel
  • cmd/root.go & configs/config.go: definition and binding of log.level

@iuwqyir iuwqyir merged commit a5ed360 into main Jun 13, 2025
6 checks passed
@iuwqyir iuwqyir deleted the 06-09-replace_gin_logger_for_requests branch June 13, 2025 17:32
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.

1 participant