[PR #2040] [MERGED] Integrate GORM Logger with Zerolog and Add Configuration Options for Logging and Performance #2463

Closed
opened 2025-12-29 03:21:23 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/juanfont/headscale/pull/2040
Author: @nadongjun
Created: 8/1/2024
Status: Merged
Merged: 8/19/2024
Merged by: @kradalby

Base: mainHead: add-db-logger


📝 Commits (3)

  • f1e1a66 Integrate GORM logger with zerolog and add custom GORM configuration options
  • fddb9de Add GormConfig struct to group GORM-related settings
  • 1b63c5d Update debug mode instruction in config-example.yaml

📊 Changes

4 files changed (+118 additions, -2 deletions)

View changed files

📝 config-example.yaml (+17 -0)
📝 hscontrol/db/db.go (+3 -2)
📝 hscontrol/types/config.go (+23 -0)
📝 hscontrol/util/log.go (+75 -0)

📄 Description

  • 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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/juanfont/headscale/pull/2040 **Author:** [@nadongjun](https://github.com/nadongjun) **Created:** 8/1/2024 **Status:** ✅ Merged **Merged:** 8/19/2024 **Merged by:** [@kradalby](https://github.com/kradalby) **Base:** `main` ← **Head:** `add-db-logger` --- ### 📝 Commits (3) - [`f1e1a66`](https://github.com/juanfont/headscale/commit/f1e1a6601c6d78ad66493eadc135189d23e4a25d) Integrate GORM logger with zerolog and add custom GORM configuration options - [`fddb9de`](https://github.com/juanfont/headscale/commit/fddb9de5595c6061bb709bd746da764ebd61eac2) Add GormConfig struct to group GORM-related settings - [`1b63c5d`](https://github.com/juanfont/headscale/commit/1b63c5d4c8d38dd966cfb9035bcd4e5c730dcb51) Update debug mode instruction in config-example.yaml ### 📊 Changes **4 files changed** (+118 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `config-example.yaml` (+17 -0) 📝 `hscontrol/db/db.go` (+3 -2) 📝 `hscontrol/types/config.go` (+23 -0) 📝 `hscontrol/util/log.go` (+75 -0) </details> ### 📄 Description <!-- Headscale is "Open Source, acknowledged contribution", this means that any contribution will have to be discussed with the Maintainers before being submitted. This model has been chosen to reduce the risk of burnout by limiting the maintenance overhead of reviewing and validating third-party code. Headscale is open to code contributions for bug fixes without discussion. If you find mistakes in the documentation, please submit a fix to the documentation. --> <!-- Please tick if the following things apply. You… --> - [x] have read the [CONTRIBUTING.md](./CONTRIBUTING.md) file - [ ] raised a GitHub issue or discussed it on the projects chat beforehand - [ ] added unit tests - [ ] added integration tests - [x] updated documentation if needed - [ ] updated CHANGELOG.md <!-- If applicable, please reference the issue using `Fixes #XXX` and add tests to cover your new code. --> 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](https://github.com/user-attachments/assets/9ab62ea0-4811-4d7c-be05-d1900579b91b) - log.level: debug, database.gorm.debug: true, database.gorm.parameterized_queries: false ![image](https://github.com/user-attachments/assets/b6adb731-9863-4c52-adf9-0f176406113b) - log.level: debug, database.gorm.debug: true, database.gorm.parameterized_queries: false, database.gorm.slow_threshold ![image](https://github.com/user-attachments/assets/b72957f5-abd0-4bae-a6a4-428d635cc17f) ## Reference - [Statement preparation option](https://gorm.io/docs/performance.html) - [Log option](https://gorm.io/docs/logger.html) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-29 03:21:23 +01:00
adam closed this issue 2025-12-29 03:21:23 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/headscale#2463