Skip to content

Commit

Permalink
Initialize the default logger to output to stderr (#1569)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcastorina authored Jul 31, 2023
1 parent 10b6e28 commit 070014f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ package context
import (
"context"
"fmt"
"os"
"runtime/debug"
"sync"
"time"

"github.com/go-logr/logr"
"github.com/trufflesecurity/trufflehog/v3/pkg/log"
)

var (
// defaultLogger can be set via SetDefaultLogger.
defaultLogger logr.Logger = logr.Discard()
// It is initialized to write to stderr. To disable, you can call
// SetDefaultLogger with logr.Discard().
defaultLogger logr.Logger
)

func init() {
defaultLogger, _ = log.New("context", log.WithConsoleSink(os.Stderr))
}

// Context wraps context.Context and includes an additional Logger() method.
type Context interface {
context.Context
Expand Down Expand Up @@ -136,7 +144,9 @@ func AddLogger(parent context.Context) Context {
}

// SetupDefaultLogger sets the package-level global default logger that will be
// used for Background and TODO contexts.
// used for Background and TODO contexts. On startup, the default logger will
// be configured to output logs to stderr. Use logr.Discard() to disable all
// logs from Contexts.
func SetDefaultLogger(l logr.Logger) {
defaultLogger = l
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/context/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestWithValues(t *testing.T) {
assert.NotContains(t, logs[6], `what does this do?`)
}

func TestDiscardLogger(t *testing.T) {
func TestDefaultLogger(t *testing.T) {
var panicked bool
defer func() {
if r := recover(); r != nil {
Expand Down

0 comments on commit 070014f

Please sign in to comment.