Skip to content

Commit

Permalink
make NewStdLog take a level; log http span at DebugLevel like we did …
Browse files Browse the repository at this point in the history
…for 2.4 in #8456
  • Loading branch information
jrockway committed Dec 21, 2022
1 parent 39a9752 commit 1e55a7a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/internal/dockertestenv/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func newMySQLEphemeralURL(ctx context.Context, t testing.TB, name string) pachsq
db := testutil.OpenDBURL(t, u, MySQLPassword)
ctx, cf := context.WithTimeout(ctx, 30*time.Second)
defer cf()
mysql.SetLogger(log.NewStdLog(pctx.Child(pctx.TODO(), "ephemeral-mysql"))) //nolint:errcheck
mysql.SetLogger(log.NewStdLogAt(pctx.Child(pctx.TODO(), "ephemeral-mysql"), log.DebugLevel)) //nolint:errcheck
require.NoError(t, dbutil.WaitUntilReady(ctx, db))
testutil.CreateEphemeralDB(t, db, name)
u2 := u
Expand Down
2 changes: 1 addition & 1 deletion src/internal/log/amazon.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ var _ aws.Logger = new(amazon)

// NewAmazonLogger returns an aws.Logger.
func NewAmazonLogger(ctx context.Context) aws.Logger {
a := amazon(*NewStdLog(ctx))
a := amazon(*NewStdLogAt(ctx, DebugLevel))
return &a
}
6 changes: 3 additions & 3 deletions src/internal/log/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@
//
// NewAmazonLogger(ctx) # For the S3 client
// NewLogrus(ctx) # For logrus users, like dex and snowflake.
// NewStdLog(ctx) # For "log" users, like the net/http server.
// NewStdLogAt(ctx, [level]) # For "log" users, like the net/http server.
// AddLoggerToEtcdServer(ctx, server) # To add a logger to an in-memory etcd.
// AddLoggerToHttpServer(ctx, name, server) # To add a logger to incoming requests in a net/http server.
//
// Do not use a NewLogrus or a NewStdLog in any code you're writing. They are only for third-party
// packages.
// Do not use a NewLogrus or a NewStdLogAt as the primary logger in any code you're writing. They
// are only for adapting to the needs of third-party packages.
//
// # EXTENDING THE LOGGING PACKAGE
//
Expand Down
2 changes: 1 addition & 1 deletion src/internal/log/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func AddLoggerToHTTPServer(rctx context.Context, name string, s *http.Server) {
s.BaseContext = func(l net.Listener) context.Context {
return ctx
}
s.ErrorLog = NewStdLog(ctx)
s.ErrorLog = NewStdLogAt(ctx, DebugLevel)
if s.Handler != nil {
orig := s.Handler
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
10 changes: 7 additions & 3 deletions src/internal/log/stdlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
"go.uber.org/zap"
)

// NewStdLog returns a *log.Logger (from the standard library "log" package) that logs to the provided context.
func NewStdLog(ctx context.Context) *log.Logger {
return zap.NewStdLog(extractLogger(ctx).WithOptions(zap.AddCallerSkip(-1)))
// NewStdLogAt returns a *log.Logger (from the standard library "log" package) that logs to the
// provided context, at the provided level.
func NewStdLogAt(ctx context.Context, lvl Level) *log.Logger {
// NewStdLogAt cannot return an error for any of the log levels that lvl.coreLevel()
// returns, since coreLevel will change the level to Debug if it's unknown.
l, _ := zap.NewStdLogAt(extractLogger(ctx).WithOptions(zap.AddCallerSkip(-1)), lvl.coreLevel()) //nolint:errcheck
return l
}

0 comments on commit 1e55a7a

Please sign in to comment.