Skip to content

Conversation

@yhakbar
Copy link
Collaborator

@yhakbar yhakbar commented Mar 3, 2025

  • fix: Doing some cleanup
  • feat: Actually use interface for logger

Summary by CodeRabbit

  • Refactor
    • Overhauled the logging architecture to use a more flexible interface, enhancing consistency and clarity in log outputs.
    • Improved logging integration for both production and testing environments.
    • Streamlined how logging is handled across components, optimizing resource management and performance.

@coderabbitai
Copy link

coderabbitai bot commented Mar 3, 2025

Walkthrough

This update restructures the logging system by replacing an existing struct-based logger with a more flexible interface-based design. A new implementation, slogLogger, now fulfills the logger interface, and corresponding method receivers along with the NewLogger function have been updated. Similarly, a test-specific logger (testLogger) is introduced. In several modules—including completion, definition, hover, state, and main—the logger parameter is changed from a pointer type to a value type, standardizing how the logger is passed around across the codebase.

Changes

Files Summary of Changes
internal/logger/logger.go Changed Logger from a struct to an interface; added slogLogger implementation with an extra closer field; updated NewLogger return type and method receivers (Close, Debug, Info, Error) to use slogLogger.
internal/testutils/testutils.go Introduced a new testLogger type wrapping slog.Logger with a closer; updated NewTestLogger to return *testLogger; added logging methods (Close, Debug, Info, Error) that delegate to the underlying logger.
internal/tg/* (.go files: completion, definition, hover, state) Updated multiple function signatures to accept the logger by value instead of a pointer, affecting functions such as GetCompletions, GetDefinitionTargetWithContext, GetHoverTargetWithContext, and several methods in the State struct.
main.go Modified the signatures of handleMessage and writeResponse to accept the logger as a value rather than a pointer, ensuring consistency in logger parameter handling across the application.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant LogIF as logger.Logger
    participant Impl as slogLogger
    participant Underlying as slog.Logger

    App->>LogIF: Call Info/Debug/Error/Close
    Note right of LogIF: LogIF is an interface
    LogIF->>Impl: Delegate call to slogLogger implementation
    Impl->>Underlying: Forward call to underlying slog.Logger
    Underlying-->>Impl: Return logging output
    Impl-->>LogIF: Operation complete
Loading

Poem

In our code, a new logger sings,
An interface now that flexes its wings.
slogLogger steps up with a graceful might,
And even test logs join the light.
Pointers retreat as values take the stage,
Logging refreshed, our code turns a page.
Cheers to clean lines and logs on the rise!

✨ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

@yhakbar yhakbar mentioned this pull request Mar 3, 2025
@yhakbar yhakbar merged commit 0c464b6 into main Mar 3, 2025
4 of 5 checks passed
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: 0

🧹 Nitpick comments (3)
internal/testutils/testutils.go (1)

21-35: Consider returning logger.Logger from this constructor.
Returning the concrete type here works fine, but returning the interface can sometimes make tests more flexible and decoupled from the concrete logger implementation.

-func NewTestLogger(t *testing.T) *testLogger {
+func NewTestLogger(t *testing.T) logger.Logger {
internal/logger/logger.go (2)

29-39: Consider returning the Logger interface from NewLogger.
Exposing only the interface improves abstraction and keeps code open for alternate implementations.

-func NewLogger(filename string) *slogLogger {
+func NewLogger(filename string) Logger {

54-58: Likewise, returning Logger here maintains consistency.
Returning an interface further decouples your calling code from the internal struct.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7b38151 and 4c33fe2.

📒 Files selected for processing (7)
  • internal/logger/logger.go (3 hunks)
  • internal/testutils/testutils.go (3 hunks)
  • internal/tg/completion/completion.go (1 hunks)
  • internal/tg/definition/definition.go (1 hunks)
  • internal/tg/hover/hover.go (1 hunks)
  • internal/tg/state.go (6 hunks)
  • main.go (2 hunks)
🔇 Additional comments (21)
internal/tg/hover/hover.go (1)

23-23: Smooth change to use logger interface!

You've transitioned from using a pointer to the logger (*logger.Logger) to using the interface value directly (logger.Logger). This move makes the code more flexible since it can now work with any implementation of the logger interface.

internal/tg/definition/definition.go (1)

24-24: Nice and consistent logger parameter update

This change aligns with the broader effort to use the logger interface throughout the codebase. Using the interface value directly (logger.Logger) instead of a pointer gives you more flexibility with different logger implementations while keeping the actual logging behavior the same.

internal/tg/completion/completion.go (1)

13-13: Good job keeping consistency with logger interface

Just like in the other files, you've updated the parameter type from a pointer to the interface value. This makes the code more modular and easier to test since you can now plug in different logger implementations as needed.

main.go (3)

48-48: Clean interface transition for handleMessage

Changing from *logger.Logger to logger.Logger is a good move that aligns with the interface-based approach you're implementing. This makes your code more flexible without changing its behavior.


210-210: Consistent logger parameter update

Nice work maintaining consistency with the logger interface changes throughout the codebase. This change to writeResponse completes the transition to using the logger interface directly.


19-19:

✅ Verification successful

Verify logger initialization with new interface

While not part of the explicit changes, make sure the NewLogger function here is updated to return the new interface implementation. This should work fine if the changes to the logger package are implemented correctly.


🏁 Script executed:

#!/bin/bash
# Check the logger implementation and NewLogger function
cat internal/logger/logger.go | grep -A 20 "func NewLogger"

Length of output: 570


Heads-up: Logger Initialization Verified

The NewLogger function in internal/logger/logger.go now initializes the logger using the new interface implementation. The slog-based configuration—with its appropriate text and JSON handlers—confirms that the updated logger package changes are in effect.

  • The function correctly returns a *slogLogger, aligning with the new interface expectations.
  • Both cases (empty filename and file logging) demonstrate the proper setup.

Overall, nothing further is needed here.

internal/testutils/testutils.go (3)

13-19: Looks solid for ensuring interface compliance.
This assignment to _ is a neat way to verify that testLogger indeed fulfills the logger.Logger interface at compile time.


67-74: Clean and safe resource cleanup.
Checking for a non-nil closer before calling Close() is a good practice.


76-89: Straightforward delegated logging methods.
These methods neatly wrap the underlying slog calls. Looks good.

internal/logger/logger.go (6)

10-16: Good interface binding for slogLogger.
This confirms slogLogger satisfies Logger at compile time, avoiding surprises later.


18-23: Interface design covers essential logging needs.
Defining Close, Debug, Info, and Error is clean and covers typical logging usage.


61-67: Clear, minimal resource cleanup.
The conditional close helps avoid nil-pointer panics.


70-72: Nicely delegated debug logging.
No extra overhead or hidden complexities—very straightforward.


75-77: Well-structured info logging.
Consistent usage of the embedded slog logger.


80-82: Straightforward error logging.
Matches the rest of the logging pattern.

internal/tg/state.go (6)

28-36: Switching to an interface parameter is a welcome simplification.
Passing around logger.Logger here helps maintain a clean layering.


38-46: Same updates for UpdateDocument.
Also a straightforward improvement by using the interface type.


48-72: updateState nicely adopts the interface too.
Keeps the pattern consistent—no immediate issues spotted.


74-139: Hover method transitions smoothly to the new interface.
No logic concerns; calls remain the same, just with the interface param.


153-229: Definition also fits well with the interface.
Looks like the debugging logic remains intact, just with an updated param type.


247-259: TextDocumentCompletion completes the logger interface switch.
Having all these methods rely on logger.Logger is a good consistency move.

@yhakbar yhakbar deleted the fix/use-interface-for-logger branch March 3, 2025 13:28
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