Skip to content

Feat: Request Console Logging#30

Open
erancihan wants to merge 10 commits intodoganarif:mainfrom
erancihan:feat/with-console-logging
Open

Feat: Request Console Logging#30
erancihan wants to merge 10 commits intodoganarif:mainfrom
erancihan:feat/with-console-logging

Conversation

@erancihan
Copy link
Collaborator

@erancihan erancihan commented May 24, 2025

Changes

  • Added option for console logging
Screenshot 2025-05-24 at 14 55 34 Screenshot 2025-05-24 at 14 56 18

Summary by Sourcery

Introduce optional console logging for HTTP requests in the middleware, enabling colorized request output to the console.

New Features:

  • Add Config.LogRequestToConsole field with WithConsoleLogging option
  • Extend middleware.Wrap signature to pass console logging flag
  • Invoke utils.LogRequest when console logging is enabled

Enhancements:

  • Implement colorized request logging in internal/utils/logger.go
  • Include example application showcasing console logging usage

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented May 24, 2025

Reviewer's Guide

This PR adds a new console logging feature by introducing a configuration flag for console output, updating middleware and the Wrap function to pass and act on the new flag, implementing a colored logger utility, and providing an example demonstrating usage.

Sequence Diagram: Request Handling with New Console Logging

sequenceDiagram
    actor User as "HTTP Client"
    participant GVW as "govisual.Wrap (wrap.go)"
    participant MW as "middleware.Wrap (middleware.go)"
    participant Store as "store.Store"
    participant Logger as "utils.LogRequest (logger.go)"
    participant Console

    User->>+GVW: HTTP Request
    GVW->>GVW: Load Config (incl. LogRequestToConsole)
    GVW->>+MW: Wrap(handler, store, ..., config.LogRequestToConsole, pathMatcher)
    Note right of MW: Request processing begins
    MW-->>MW: Capture request details (method, path, etc.)
    MW-->>MW: Execute wrapped HTTP handler
    MW-->>MW: Capture response details (status code, etc.)
    MW-->>MW: Create reqLog: model.RequestLog
    alt If config.LogRequestToConsole is true
        MW->>+Logger: LogRequest(reqLog)
        Logger->>Console: Print formatted log message
        Logger-->>-MW: Return
    end
    MW->>+Store: Add(reqLog)
    Store-->>-MW: Return
    MW-->>-GVW: HTTP Response
    GVW-->>-User: HTTP Response
Loading

File-Level Changes

Change Details Files
Introduce console logging option in configuration
  • Add LogRequestToConsole field to Config struct
  • Provide WithConsoleLogging option function
  • Set default LogRequestToConsole to false in defaultConfig
options.go
Extend middleware to conditionally log to console
  • Update Wrap signature to accept logToConsole parameter
  • Invoke utils.LogRequest when logToConsole is enabled
internal/middleware/middleware.go
Propagate console logging flag through top-level Wrap
  • Pass config.LogRequestToConsole into middleware.Wrap call
wrap.go
Add colored console logger utility
  • Implement colorizeMethod, colorizeStatus, colorizeDuration functions
  • Create LogRequest to print timestamp, method, status, duration, and path
internal/utils/logger.go
Provide example demonstrating console logging
  • Include example main in test/example.go showing WithConsoleLogging usage
test/example.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @erancihan - I've reviewed your changes - here's some feedback:

  • The example in test/example.go uses r.PathValue and {param} routes with http.ServeMux, which isn’t supported—either switch to a router library or implement path param parsing.
  • Using time.Since(reqLog.Timestamp) for console logging may not accurately reflect handler duration; capture the start time before invoking the handler to compute precise execution time.
  • ANSI color codes may not render correctly on all platforms (e.g., Windows terminals); consider adding detection or a flag to disable color output.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟡 Testing: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@erancihan erancihan changed the title Feat: Console Logging Feat: Request Console Logging May 24, 2025
@erancihan
Copy link
Collaborator Author

this MR does too many things and is very troublesome to rebase on it's own.

will be splitting it to bits and pushing it that way

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