Skip to content

Commit

Permalink
Merge pull request #153 from ngrok/feat/change-slog-import-path
Browse files Browse the repository at this point in the history
Update the slog import path for go 1.21
  • Loading branch information
jrobsonchase authored Jan 22, 2024
2 parents 3808ea4 + f612a21 commit ed50385
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 125 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/go.work.sum
.env
.direnv
*.swp
Expand Down
14 changes: 7 additions & 7 deletions examples/go.mod
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
module golang.ngrok.com/ngrok/examples

go 1.20
go 1.21

require (
golang.ngrok.com/ngrok v0.0.0
golang.ngrok.com/ngrok/log/slog v0.0.0-00010101000000-000000000000
golang.org/x/exp v0.0.0-20230307190834-24139beb5833
)

require (
github.com/go-stack/stack v1.8.1 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/inconshreveable/log15 v3.0.0-testing.3+incompatible // indirect
github.com/inconshreveable/log15/v3 v3.0.0-testing.5 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.ngrok.com/muxado/v2 v2.0.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.12.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)

replace (
Expand Down
33 changes: 19 additions & 14 deletions examples/go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/inconshreveable/log15 v3.0.0-testing.3+incompatible h1:zaX5fYT98jX5j4UhO/WbfY8T1HkgVrydiDMC9PWqGCo=
github.com/inconshreveable/log15 v3.0.0-testing.3+incompatible/go.mod h1:cOaXtrgN4ScfRrD9Bre7U1thNq5RtJ8ZoP4iXVGRj6o=
github.com/inconshreveable/log15/v3 v3.0.0-testing.5 h1:h4e0f3kjgg+RJBlKOabrohjHe47D3bbAB9BgMrc3DYA=
Expand All @@ -16,25 +19,27 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.ngrok.com/muxado/v2 v2.0.0 h1:bu9eIDhRdYNtIXNnqat/HyMeHYOAbUH55ebD7gTvW6c=
golang.ngrok.com/muxado/v2 v2.0.0/go.mod h1:wzxJYX4xiAtmwumzL+QsukVwFRXmPNv86vB8RPpOxyM=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/exp v0.0.0-20230307190834-24139beb5833 h1:SChBja7BCQewoTAU7IgvucQKMIXrEpFxNMs0spT3/5s=
golang.org/x/exp v0.0.0-20230307190834-24139beb5833/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7 changes: 4 additions & 3 deletions examples/slog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os"
"strings"

"golang.org/x/exp/slog"
"log/slog"

"golang.ngrok.com/ngrok"
"golang.ngrok.com/ngrok/config"
Expand Down Expand Up @@ -47,8 +47,9 @@ func run(ctx context.Context, lvlName string) error {
default:
return fmt.Errorf("invalid log level: %s", lvlName)
}
h := slog.HandlerOptions{Level: programLevel}.NewTextHandler(os.Stdout)
slog.SetDefault(slog.New(h))
opts := &slog.HandlerOptions{Level: programLevel}
logger := slog.New(slog.NewTextHandler(os.Stdout, opts))
slog.SetDefault(logger)

ln, err := ngrok.Listen(ctx,
config.HTTPEndpoint(),
Expand Down
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
go_1_20
go_1_21
gotools
golint
go-tools
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module golang.ngrok.com/ngrok

go 1.20
go 1.21

require (
github.com/inconshreveable/log15/v3 v3.0.0-testing.5
Expand Down
2 changes: 1 addition & 1 deletion go.work
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// because `go mod tidy` & co seem to ignore the ones at the workspace level.
// See: https://github.com/golang/go/issues/50750.

go 1.20
go 1.21

use (
.
Expand Down
59 changes: 0 additions & 59 deletions go.work.sum

This file was deleted.

4 changes: 2 additions & 2 deletions internal/pb/middleware.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion log/log15/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module golang.ngrok.com/ngrok/log/log15

go 1.20
go 1.21

require github.com/inconshreveable/log15/v3 v3.0.0-testing.1

Expand Down
2 changes: 1 addition & 1 deletion log/logrus/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module golang.ngrok.com/ngrok/log/logrus

go 1.20
go 1.21

require github.com/sirupsen/logrus v1.9.0

Expand Down
28 changes: 5 additions & 23 deletions log/slog/adapter.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Package slog provides a logger that writes to a
// golang.org/x/exp/slog.Logger and implements the
// Package slog provides a logger that writes
// to a log/slog.Logger and implements the
// golang.ngrok.com/ngrok/log.Logger interface.
package slog

import (
"context"
"fmt"

"golang.org/x/exp/slog"
"log/slog"
)

type LogLevel = int
Expand All @@ -34,23 +33,6 @@ func NewLogger(l *slog.Logger) *Logger {
}

func (l *Logger) Log(ctx context.Context, level LogLevel, msg string, data map[string]interface{}) {
var logError error
// The slog `Error` method takes an error field. Look through our log
// args and see if one was provided, and trim it out.
if level == LogLevelError {
var errorKey string
for k, v := range data {
if err, ok := v.(error); ok {
logError = err
errorKey = k
break
}
}
if logError != nil {
delete(data, errorKey)
}
}

logArgs := make([]interface{}, 0, len(data))
for k, v := range data {
logArgs = append(logArgs, k, v)
Expand All @@ -66,8 +48,8 @@ func (l *Logger) Log(ctx context.Context, level LogLevel, msg string, data map[s
case LogLevelWarn:
l.inner.Warn(msg, logArgs...)
case LogLevelError:
l.inner.Error(msg, logError, logArgs...)
l.inner.Error(msg, logArgs...)
default:
l.inner.Error(msg, fmt.Errorf("INVALID LOG LEVEL: %d", level), logArgs...)
l.inner.Error(msg, append(logArgs, "INVALID_LOG_LEVEL", level)...)
}
}
4 changes: 1 addition & 3 deletions log/slog/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module golang.ngrok.com/ngrok/log/slog

go 1.20

require golang.org/x/exp v0.0.0-20230307190834-24139beb5833
go 1.21
2 changes: 0 additions & 2 deletions log/slog/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
golang.org/x/exp v0.0.0-20230307190834-24139beb5833 h1:SChBja7BCQewoTAU7IgvucQKMIXrEpFxNMs0spT3/5s=
golang.org/x/exp v0.0.0-20230307190834-24139beb5833/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
2 changes: 1 addition & 1 deletion log/zap/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module golang.ngrok.com/ngrok/log/zap

go 1.20
go 1.21

require go.uber.org/zap v1.23.0

Expand Down

0 comments on commit ed50385

Please sign in to comment.