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

Integrate GORM Logger with Zerolog and Add Configuration Options for Logging and Performance #2040

Merged
merged 3 commits into from
Aug 19, 2024

Conversation

nadongjun
Copy link
Contributor

  • have read the CONTRIBUTING.md file
  • raised a GitHub issue or discussed it on the projects chat beforehand
  • added unit tests
  • added integration tests
  • updated documentation if needed
  • updated CHANGELOG.md

This PR integrates the GORM logger with zerolog to detect database-related bottlenecks. With this integration, the log level now follows the level set in zerolog (e.g., debug.level), ensuring consistent and detailed logging for database operations.

To activate database debugging, make sure to set log.level to debug and database.debug to true.

Additionally, custom configuration options for GORM have been added. Example configurations can be found in config-example.yaml. The available options include:

Options:

config-example.yaml

database:
...
  # Enable debug mode. Set log.level to "debug".
  debug: false

  # GORM configuration settings.
  gorm:
    # Enable prepared statements.
    prepare_stmt: true

    # Enable parameterized queries.
    parameterized_queries: true

    # Skip logging "record not found" errors.
    skip_err_record_not_found: true

    # Threshold for slow queries in milliseconds.
    slow_threshold: 1000

1. Log Options:

  • SlowThreshold (ms): Sets the duration threshold for logging slow queries.
  • ParameterizedQueries (bool): Provides the option to enable query caching. This option is enabled by default in Postgres.
  • SkipErrRecordNotFound (bool): Controls whether to skip logging "record not found" errors.

2. Statement Preparation Option:

  • PrepareStmt (bool): Specifies whether SQL statements should be prepared.

These options support existing GORM settings and allow for flexible configuration tailored to different environments and needs.

Summary of Changes

  • Integrated GORM logger with zerolog.
  • Added custom GORM configuration options.

Example

  • log.level: debug, database.debug: true, database.gorm.parameterized_queries: true
    image
  • log.level: debug, database.gorm.debug: true, database.gorm.parameterized_queries: false
    image
  • log.level: debug, database.gorm.debug: true, database.gorm.parameterized_queries: false, database.gorm.slow_threshold
    image

Reference

hscontrol/types/config.go Outdated Show resolved Hide resolved
config-example.yaml Outdated Show resolved Hide resolved
@nadongjun nadongjun requested a review from kradalby August 16, 2024 20:24
@kradalby kradalby merged commit fdc034e into juanfont:main Aug 19, 2024
109 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.

2 participants