Skip to content
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

feat(log): support customization of log json marshal #18429

Merged
merged 13 commits into from
Nov 12, 2023

Conversation

lilasxie
Copy link
Contributor

@lilasxie lilasxie commented Nov 10, 2023

Description

support customization json marshal of the log sdk


Author Checklist

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • run make lint and make test
  • confirmed all CI checks have passed

Summary by CodeRabbit

  • New Features
    • Introduced support for customization of log JSON marshaling, enhancing flexibility.
  • Refactor
    • Updated logger initialization to incorporate new JSON marshaling options.
  • Documentation
    • Updated CHANGELOG to reflect new JSON marshaling customization feature.

@lilasxie lilasxie requested a review from a team as a code owner November 10, 2023 08:05
Copy link
Contributor

coderabbitai bot commented Nov 10, 2023

Walkthrough
# Walkthrough

The recent changes aim to enhance the flexibility of JSON marshaling within the logging package. This is achieved by introducing a new field `JSONMarshal` to the `Config` struct, along with a corresponding function to set this field. The `zerolog.InterfaceMarshalFunc` is now conditionally set based on the presence of `logCfg.JSONMarshal`, allowing for custom JSON marshaling of various types. Additionally, the `init` function and `NewLogger` function have been modified to incorporate the new JSON marshaling logic.

# Changes

| File(s)               | Change Summary                                                                                                      |
| --------------------- | -------------------------------------------------------------------------------------------------------------------- |
| log/CHANGELOG.md      | Added support for customization of log JSON marshaling through pull request #18429.                                 |
| log/logger.go         | Introduced a new conditional block to set the `zerolog.InterfaceMarshalFunc` based on `logCfg.JSONMarshal`. Modified the `init` and `NewLogger` functions to incorporate the new JSON marshaling logic. |
| log/options.go        | Added a new field `JSONMarshal` to the `Config` struct and a corresponding function `JSONMarshalOption` to set this field. Imported the "encoding/json" package and assigned `json.Marshal` to the `JSONMarshal` field in the `defaultConfig` variable. |

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

@github-actions github-actions bot added the C:log label Nov 10, 2023
Copy link
Contributor

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7208905 and a3ae7b2.
Files selected for processing (3)
  • log/CHANGELOG.md (1 hunks)
  • log/logger.go (1 hunks)
  • log/options.go (2 hunks)
Additional comments: 5
log/CHANGELOG.md (1)
  • 23-24: The changelog entry is clear and concise, providing a link to the pull request for more details. Ensure that the link is correct and leads to the correct pull request.
log/logger.go (1)
  • 74-90: The new conditional block allows for custom JSON marshaling of various types including json.Marshaler, encoding.TextMarshaler, and fmt.Stringer. This enhances the flexibility of JSON marshaling within the logger. However, it's important to ensure that the logCfg.JSONMarshal function is thoroughly tested to handle all these types correctly.
log/options.go (3)
  • 18-18: The default JSON marshaler is set to json.Marshal. This is a sensible default, but it's important to ensure that all types that will be logged are compatible with this marshaler. If a type implements the json.Marshaler interface, that will be used instead.

  • 29-29: The JSONMarshal field is added to the Config struct. This allows for custom JSON marshaling functions to be used. This is a good addition for flexibility, but it's important to ensure that the provided function is safe and performs as expected.

  • 95-99: The JSONMarshalOption function is added. This function allows for the JSONMarshal field in the Config struct to be set. This is a good addition for flexibility, but it's important to ensure that the provided function is safe and performs as expected.

Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

It makes sense to me. Few nits.

log/CHANGELOG.md Outdated Show resolved Hide resolved
log/options.go Outdated Show resolved Hide resolved
log/options.go Outdated Show resolved Hide resolved
log/logger.go Outdated Show resolved Hide resolved
@julienrbrt julienrbrt changed the title feat: support customization of log json marshal feat(log): support customization of log json marshal Nov 10, 2023
Co-authored-by: Julien Robert <julien@rbrt.fr>
Copy link
Contributor

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between a3ae7b2 and 9c77618.
Files selected for processing (1)
  • log/CHANGELOG.md (1 hunks)
Files skipped from review due to trivial changes (1)
  • log/CHANGELOG.md

Co-authored-by: Julien Robert <julien@rbrt.fr>
Copy link
Contributor

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9c77618 and 0ef59d0.
Files selected for processing (1)
  • log/options.go (2 hunks)
Additional comments: 3
log/options.go (3)
  • 18-18: The default JSON marshaler is set to json.Marshal. This is a good default as it is the standard way to marshal JSON in Go. However, it's important to note that this could be overridden by the user, potentially leading to unexpected behavior if the provided function does not properly marshal to JSON.

  • 29-29: The JSONMarshal field has been added to the Config struct. This allows for customization of the JSON marshaling function. This is a good addition as it allows for more flexibility in how JSON is marshaled.

  • 95-99: The JSONMarshalOption function has been added to set the JSONMarshal field in the Config struct. This is a good addition as it allows for the JSONMarshal field to be set in a consistent manner with the other options.

log/options.go Outdated Show resolved Hide resolved
Copy link
Contributor

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0ef59d0 and e95f6f4.
Files selected for processing (2)
  • log/logger.go (2 hunks)
  • log/options.go (2 hunks)
Additional comments: 4
log/options.go (3)
  • 10-19: The defaultConfig has been updated to include JSONMarshal: json.Marshal. This is a sensible default as it uses the standard library's JSON marshaling function. Ensure that this default behavior is documented.

  • 22-30: A new field JSONMarshal has been added to the Config struct. This allows for custom JSON marshaling functions to be used. This is a good addition as it increases the flexibility of the logger.

  • 94-99: The JSONMarshalOption function has been added. This function allows for the JSONMarshal field in the Config struct to be set. This is a good addition as it allows for the use of custom JSON marshaling functions.

log/logger.go (1)
  • 59-81: The new logic for setting the jsonMarshaler and updating the zerolog.InterfaceMarshalFunc is sound. However, the init function mentioned in the previous comment is not present in the provided code. If it exists elsewhere, consider removing it as suggested by the previous reviewer, since the initialization is now handled here.

log/logger.go Outdated Show resolved Hide resolved
Copy link
Contributor

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e95f6f4 and ee15a68.
Files selected for processing (1)
  • log/logger.go (2 hunks)
Additional comments: 2
log/logger.go (2)
  • 64-66: The jsonMarshaler is being set based on the logCfg.JSONMarshal. If logCfg.JSONMarshal is nil, json.Marshal is used as a default. This is a good practice as it provides a sensible default and allows for customization.

  • 67-75: The InterfaceMarshalFunc is being set to handle different types of interfaces. This is a good practice as it allows for different types of interfaces to be marshaled correctly.

log/logger.go Outdated Show resolved Hide resolved
Copy link
Contributor

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between ee15a68 and 757f332.
Files selected for processing (1)
  • log/logger.go (3 hunks)
Additional comments: 1
log/logger.go (1)
  • 52-65: The WithJSONMarshal function allows the user to specify a custom JSON marshaling function. This is a good addition for flexibility. However, it's important to note that this function changes the global InterfaceMarshalFunc of zerolog, which could affect other parts of the application that also use zerolog. Make sure to document this side effect clearly.

log/logger.go Outdated Show resolved Hide resolved
log/logger.go Outdated Show resolved Hide resolved
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Copy link
Contributor

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 757f332 and 1233ff2.
Files selected for processing (1)
  • log/logger.go (3 hunks)
Additional comments: 2
log/logger.go (2)
  • 13-18: The previous comment about InterfaceMarshalFunc being set in the init function is still valid. Consider moving this to a function that is explicitly called by the user.

  • 52-65: The new WithJSONMarshal function allows for custom JSON marshaling. Ensure that the marshaler function passed as an argument is robust and handles all necessary types. Also, consider adding error handling within this function to handle potential failures of the marshaler function.

Copy link
Contributor

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 1233ff2 and 9fcb000.
Files selected for processing (1)
  • log/logger.go (3 hunks)
Additional comments: 2
log/logger.go (2)
  • 13-18: The init function has been updated to set the zerolog.InterfaceMarshalFunc to a function that accepts any type and returns a JSON marshaled byte array. This change allows for custom JSON marshaling of various types, enhancing flexibility. However, ensure that the custom marshaling function handles all types that may be passed to it, as the default case simply attempts to marshal the input without any type checking. This could potentially lead to runtime errors if an incompatible type is passed.

  • 52-65: The WithJSONMarshal function has been added to configure global JSON encoding for zerolog. This function accepts a custom marshaler function and sets the zerolog.InterfaceMarshalFunc to a function that uses this custom marshaler. This is a good addition as it allows for customization of the JSON marshaling behavior. However, similar to the init function, ensure that the custom marshaler function handles all types that may be passed to it.

log/logger.go Show resolved Hide resolved
log/logger.go Show resolved Hide resolved
when a struct implements stringer and json.Marshaller, we should use that one first.
Copy link
Contributor

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

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9fcb000 and a8e29de.
Files selected for processing (1)
  • log/logger.go (2 hunks)
Additional comments: 1
log/logger.go (1)
  • 17-17: The comment from julienrbrt suggests that there might be some code that was removed and should be brought back. Please verify if this is the case.

log/logger.go Show resolved Hide resolved
log/logger.go Show resolved Hide resolved
log/logger.go Show resolved Hide resolved
Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

lgtm, thank you!

@julienrbrt julienrbrt added this pull request to the merge queue Nov 12, 2023
Merged via the queue into cosmos:main with commit b494d6a Nov 12, 2023
55 of 57 checks passed
@lilasxie lilasxie deleted the feat/custom-log-json-marshal branch November 13, 2023 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants