Skip to content

Commit

Permalink
Switch to using log/slog
Browse files Browse the repository at this point in the history
This changes a few interfaces of the logging library, but maintains as much compatability as possible in switching to the standard library log/slog. The new APIs are designed to allow for a more programatic configuration of the logger, instead of always relying on the environment.
  • Loading branch information
sethvargo committed Aug 9, 2023
1 parent de12aa8 commit 1d400c9
Show file tree
Hide file tree
Showing 20 changed files with 705 additions and 278 deletions.
2 changes: 1 addition & 1 deletion .github/actions/terraform-linter/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ runs:
- id: 'setup-go'
uses: 'actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753' # ratchet:actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21.0-rc.3'

- id: 'run-linter'
shell: 'bash'
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ jobs:
lint:
uses: './.github/workflows/go-lint.yml'
with:
go_version: '1.20'
go_version: '1.21.0-rc.3'

test:
uses: './.github/workflows/go-test.yml'
with:
go_version: '1.20'
go_version: '1.21.0-rc.3'

lint-terraform:
uses: './.github/workflows/terraform-lint.yml'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
lint:
uses: 'abcxyz/pkg/.github/workflows/go-lint.yml@main'
with:
go_version: '1.20'
go_version: '1.21'
```
Linting is done via [golangci-lint](https://golangci-lint.run/). If a
Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
lint:
uses: 'abcxyz/pkg/.github/workflows/go-test.yml@main'
with:
go_version: '1.20'
go_version: '1.21'
```

Testing is done via the `go test` command with:
Expand Down
6 changes: 3 additions & 3 deletions bqutil/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ func (q *bqQuery[T]) Execute(ctx context.Context) ([]T, error) {
// result, err := RetryQueryEntries(ctx, q, 1, backoff)
func RetryQueryEntries[T any](ctx context.Context, q Query[T], wantCount int, backoff retry.Backoff) ([]T, error) {
logger := logging.FromContext(ctx)
logger.Debugw("Start retrying query", "query", q.String())
logger.Debug("Start retrying query", "query", q.String())

var result []T
if err := retry.Do(ctx, backoff, func(ctx context.Context) error {
entries, err := q.Execute(ctx)
if err != nil {
logger.Debugw("Query failed; will retry", "error", err)
logger.Debug("Query failed; will retry", "error", err)
return retry.RetryableError(err)
}

Expand All @@ -126,7 +126,7 @@ func RetryQueryEntries[T any](ctx context.Context, q Query[T], wantCount int, ba
return nil
}

logger.Debugw("Not enough entries; will retry", "gotCount", gotCount, "wantCount", wantCount)
logger.Debug("Not enough entries; will retry", "gotCount", gotCount, "wantCount", wantCount)
return retry.RetryableError(fmt.Errorf("not enough entries"))
}); err != nil {
return nil, fmt.Errorf("retry backoff exhausted: %w", err)
Expand Down
5 changes: 1 addition & 4 deletions bqutil/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"github.com/abcxyz/pkg/testutil"
"github.com/google/go-cmp/cmp"
"github.com/sethvargo/go-retry"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
)

type fakeQuery[T any] struct {
Expand Down Expand Up @@ -94,8 +92,7 @@ func TestRetryQueryEntries(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

ctx := logging.WithLogger(context.Background(),
logging.TestLogger(t, zaptest.Level(zapcore.DebugLevel)))
ctx := logging.WithLogger(context.Background(), logging.TestLogger(t))

wantCount := len(tc.wantEntries)

Expand Down
47 changes: 47 additions & 0 deletions cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ import (
"flag"
"fmt"
"io"
"log/slog"
"os"
"sort"
"strconv"
"strings"
"time"

"github.com/abcxyz/pkg/logging"
"github.com/abcxyz/pkg/timeutil"

"github.com/kr/text"
"github.com/posener/complete/v2"
"github.com/posener/complete/v2/predict"
Expand Down Expand Up @@ -868,6 +871,50 @@ func (f *FlagSection) Uint64Var(i *Uint64Var) {
})
}

type LogLevelVar struct {
EnvVar string
Target slog.LevelVar
}

func (f *FlagSection) LogLevelVar(i *LogLevelVar) {
envvar := i.EnvVar
if envvar == "" {
envvar = "LOG_LEVEL"
}

parser := func(s string) (slog.Level, error) {
v, err := logging.LookupLevel(s)
if err != nil {
return 0, err
}
return v, nil
}

printer := func(v slog.Level) string { return logging.LevelString(v) }

// trick the CLI into thinking we need a value to set.
var fake slog.Level

levelNames := logging.LevelNames()

Flag(f, &Var[slog.Level]{
Name: "log-level",
Aliases: []string{"-l"},
Usage: `Sets the logging verbosity. Valid values include: ` +
strings.Join(levelNames, ",") + `.`,
Example: "warn",
Default: slog.LevelInfo,
EnvVar: envvar,
Predict: predict.Set(levelNames),
Target: &fake,
Parser: parser,
Printer: printer,
Setter: func(_ *slog.Level, val slog.Level) {
i.Target.Set(val)
},
})
}

// wrapAtLengthWithPadding wraps the given text at the maxLineLength, taking
// into account any provided left padding.
func wrapAtLengthWithPadding(s string, pad int) string {
Expand Down
4 changes: 2 additions & 2 deletions containertest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:
steps:
- uses: actions/setup-go@v4
with:
go-version: '1.20' # Optional
go-version: '1.21' # Optional
```
... and *don't* do this:
Expand All @@ -111,5 +111,5 @@ jobs:
test:
name: Go Test
runs-on: ubuntu-latest
container: golang:1.20 # DON'T DO THIS
container: golang:1.21 # DON'T DO THIS
```
2 changes: 1 addition & 1 deletion gcputil/gcputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func ProjectID(ctx context.Context) string {

v, err := metadata.ProjectID()
if err != nil {
logging.FromContext(ctx).Errorw("failed to get project id", "error", err)
logging.FromContext(ctx).Error("failed to get project id", "error", err)
return ""
}
return v
Expand Down
55 changes: 25 additions & 30 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
module github.com/abcxyz/pkg

go 1.20
go 1.21

require (
cloud.google.com/go/bigquery v1.53.0
cloud.google.com/go/compute/metadata v0.2.3
github.com/go-sql-driver/mysql v1.7.1
github.com/google/go-cmp v0.5.9
github.com/hashicorp/hcl/v2 v2.17.0
github.com/jackc/pgx/v5 v5.4.2
github.com/jackc/pgx/v5 v5.4.3
github.com/kr/text v0.2.0
github.com/lestrrat-go/jwx/v2 v2.0.11
github.com/mattn/go-isatty v0.0.19
github.com/ory/dockertest/v3 v3.10.0
github.com/posener/complete/v2 v2.1.0
github.com/sethvargo/go-envconfig v0.9.0
github.com/sethvargo/go-retry v0.2.4
go.uber.org/zap v1.24.0
golang.org/x/oauth2 v0.10.0
golang.org/x/oauth2 v0.11.0
golang.org/x/sync v0.3.0
golang.org/x/text v0.11.0
google.golang.org/api v0.126.0
google.golang.org/grpc v1.56.2
golang.org/x/text v0.12.0
google.golang.org/api v0.135.0
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
gopkg.in/yaml.v3 v3.0.1
)

require (
cloud.google.com/go v0.110.4 // indirect
cloud.google.com/go/compute v1.22.0 // indirect
cloud.google.com/go/iam v1.1.0 // indirect
cloud.google.com/go v0.110.7 // indirect
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go/iam v1.1.2 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/apache/arrow/go/v12 v12.0.0 // indirect
github.com/apache/thrift v0.16.0 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/apache/arrow/go/v12 v12.0.1 // indirect
github.com/apache/thrift v0.18.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/continuity v0.4.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
Expand All @@ -51,18 +49,18 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v2.0.8+incompatible // indirect
github.com/google/flatbuffers v23.5.26+incompatible // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.11.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/klauspost/asmfmt v1.3.2 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/lestrrat-go/blackmagic v1.0.1 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
github.com/lestrrat-go/httprc v1.0.4 // indirect
Expand All @@ -76,10 +74,9 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.8 // indirect
github.com/pierrec/lz4/v4 v4.1.15 // indirect
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/posener/script v1.2.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
Expand All @@ -88,17 +85,15 @@ require (
github.com/zclconf/go-cty v1.13.2 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/tools v0.11.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/tools v0.12.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230726155614-23370e0ffb3e // indirect
google.golang.org/genproto v0.0.0-20230807174057-1744710a1577 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230807174057-1744710a1577 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 1d400c9

Please sign in to comment.