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 {