From 4bf577382f19c64457cbf0d64490401450954654 Mon Sep 17 00:00:00 2001 From: "Arvid E. Picciani" Date: Tue, 7 Dec 2021 09:36:47 +0100 Subject: [PATCH] feat: change the default logoutput to stderr also add an option to set it to any writer fixes #349 --- extra/bundebug/debug.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/extra/bundebug/debug.go b/extra/bundebug/debug.go index 11a0c2118..ccbdbc766 100644 --- a/extra/bundebug/debug.go +++ b/extra/bundebug/debug.go @@ -7,6 +7,7 @@ import ( "os" "reflect" "time" + "io" "github.com/fatih/color" @@ -30,6 +31,14 @@ func WithVerbose(on bool) Option { } } +// WithWriter sets the log output to an io.Writer +// the default is os.Stderr +func WithWriter(w io.Writer) Option { + return func(h *QueryHook) { + h.writer = w + } +} + // WithEnv configures the hook using the environment variable value. // For example, WithEnv("BUNDEBUG"): // - BUNDEBUG=0 - disables the hook. @@ -50,6 +59,7 @@ func FromEnv(key string) Option { type QueryHook struct { enabled bool verbose bool + writer io.Writer } var _ bun.QueryHook = (*QueryHook)(nil) @@ -57,6 +67,7 @@ var _ bun.QueryHook = (*QueryHook)(nil) func NewQueryHook(opts ...Option) *QueryHook { h := &QueryHook{ enabled: true, + writer: os.Stderr, } for _, opt := range opts { opt(h) @@ -101,7 +112,7 @@ func (h *QueryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) { ) } - fmt.Println(args...) + fmt.Fprintln(h.writer, args...) } func formatOperation(event *bun.QueryEvent) string {