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: bunslog.QueryHook for Bun logging using log/slog #904

Merged
merged 3 commits into from
Sep 27, 2023

Conversation

hirasawayuki
Copy link
Contributor

@hirasawayuki hirasawayuki commented Sep 22, 2023

Description

This PR implements a new package called bunslog to support enhanced logging functionalities for the Bun using log/slog.

ref #903

Features

  • Supports setting a *slog.Logger instance or uses the global logger if not set.
  • Logs general SQL queries with configurable log levels.
  • Logs slow SQL queries based on a configurable duration threshold.
  • Logs SQL queries that result in errors, for easier debugging.
  • Allows for custom log formatting.

Usage

Simply create a new QueryHook and add it to existing *db.Bun database instance.

logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
slog.SetDefault(logger)

db := bun.NewDB(sqldb, dialect)
hook := bunslog.NewQueryHook(
    bunslog.WithQueryLogLevel(slog.LevelDebug),
    bunslog.WithSlowQueryLogLevel(slog.LevelWarn),
    bunslog.WithErrorQueryLogLevel(slog.LevelError),
    bunslog.WithSlowQueryThreshold(3 * time.Second),
)
db.AddQueryHook(hook)

@hirasawayuki hirasawayuki force-pushed the feature-bunslog-query-hook branch from 5f67437 to 1e9ccf6 Compare September 23, 2023 03:32
@vmihailenco
Copy link
Member

vmihailenco commented Sep 24, 2023

@hirasawayuki Looks good, thanks. Could you add a go.mod file so we don't require log/slog which is only available starting from the Go 1.21.

@hirasawayuki hirasawayuki force-pushed the feature-bunslog-query-hook branch from 1e9ccf6 to 1b05a16 Compare September 25, 2023 15:18
@hirasawayuki hirasawayuki force-pushed the feature-bunslog-query-hook branch from 1b05a16 to 433ad6c Compare September 25, 2023 15:52
@hirasawayuki
Copy link
Contributor Author

@vmihailenco
I've made some modifications to the implementation:

  • Updated to allow for passing an instance of slog.Logger as an option, in addition to using the default logger.
  • Added the ability to set a custom format through an option.
    I would appreciate another review! Thank you!

(It appears that the build is failing for Go 1.19 and Go 1.20 due to the import of log/slog. Would it be a good idea to also modify the Makefile to avoid loading it during the build?)

@vmihailenco
Copy link
Member

Would it be a good idea to also modify the Makefile to avoid loading it during the build?)

Let's just bump Go in tests to v1.21.

@vmihailenco vmihailenco merged commit 4953367 into uptrace:master Sep 27, 2023
3 checks passed
@vmihailenco
Copy link
Member

Thanks!

@hirasawayuki hirasawayuki deleted the feature-bunslog-query-hook branch September 27, 2023 12:37
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