Skip to content

Commit

Permalink
feat: change the default logoutput to stderr
Browse files Browse the repository at this point in the history
also add an option to set it to any writer

fixes #349
  • Loading branch information
aep committed Dec 7, 2021
1 parent b117325 commit 4bf5773
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion extra/bundebug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"reflect"
"time"
"io"

"github.com/fatih/color"

Expand All @@ -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.
Expand All @@ -50,13 +59,15 @@ func FromEnv(key string) Option {
type QueryHook struct {
enabled bool
verbose bool
writer io.Writer
}

var _ bun.QueryHook = (*QueryHook)(nil)

func NewQueryHook(opts ...Option) *QueryHook {
h := &QueryHook{
enabled: true,
writer: os.Stderr,
}
for _, opt := range opts {
opt(h)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4bf5773

Please sign in to comment.