Skip to content

Commit

Permalink
Merge pull request #24 from gnolang/feat/use-slog
Browse files Browse the repository at this point in the history
feat: utilize slog
  • Loading branch information
gfanton authored Mar 8, 2024
2 parents 5fbbc33 + 28379aa commit cd086ac
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 87 deletions.
43 changes: 0 additions & 43 deletions cmd/logger.go

This file was deleted.

9 changes: 3 additions & 6 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
"log/slog"
"os"
"regexp"
"strconv"
Expand All @@ -18,7 +19,6 @@ import (
"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/peterbourgon/ff/v3/fftoml"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -179,17 +179,14 @@ func (c *faucetCfg) exec(_ context.Context, _ []string) error {
}

// Create a new logger
logger, err := zap.NewDevelopment()
if err != nil {
return err
}
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

// Create a new faucet with
// static gas estimation
f, err := faucet.NewFaucet(
static.New(gasFee, gasWanted),
tm2Client.NewClient(c.remote),
faucet.WithLogger(newCommandLogger(logger)),
faucet.WithLogger(logger),
faucet.WithConfig(c.config),
)
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"net"
"net/http"
"time"
Expand All @@ -13,8 +15,6 @@ import (
"github.com/gnolang/faucet/estimate"
"github.com/gnolang/faucet/keyring"
"github.com/gnolang/faucet/keyring/memory"
"github.com/gnolang/faucet/log"
"github.com/gnolang/faucet/log/noop"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/go-chi/chi/v5"
"github.com/rs/cors"
Expand All @@ -24,7 +24,7 @@ import (
// Faucet is a standard Gno faucet
type Faucet struct {
estimator estimate.Estimator // gas pricing estimations
logger log.Logger // log feedback
logger *slog.Logger // log feedback
client client.Client // TM2 client
keyring keyring.Keyring // the faucet keyring

Expand All @@ -38,6 +38,8 @@ type Faucet struct {
sendAmount std.Coins // for fast lookup
}

var noopLogger = slog.New(slog.NewTextHandler(io.Discard, nil))

// NewFaucet creates a new instance of the Gno faucet server
func NewFaucet(
estimator estimate.Estimator,
Expand All @@ -47,7 +49,7 @@ func NewFaucet(
f := &Faucet{
estimator: estimator,
client: client,
logger: noop.New(),
logger: noopLogger,
config: config.DefaultConfig(),
prepareTxMsgFn: defaultPrepareTxMessage,
middlewares: nil, // no middlewares by default
Expand Down
3 changes: 1 addition & 2 deletions faucet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/gnolang/faucet/config"
"github.com/gnolang/faucet/log/noop"
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/std"
Expand Down Expand Up @@ -135,7 +134,7 @@ func TestFaucet_NewFaucet(t *testing.T) {
&mockEstimator{},
&mockClient{},
WithConfig(config.DefaultConfig()),
WithLogger(noop.New()),
WithLogger(noopLogger),
)

assert.NotNil(t, f)
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/peterbourgon/ff/v3 v3.4.0
github.com/rs/cors v1.10.1
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
golang.org/x/sync v0.6.0
)

Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,8 @@ go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ=
go.etcd.io/bbolt v1.3.7/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
7 changes: 0 additions & 7 deletions log/log.go

This file was deleted.

15 changes: 0 additions & 15 deletions log/noop/noop.go

This file was deleted.

5 changes: 3 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package faucet

import (
"log/slog"

"github.com/gnolang/faucet/config"
"github.com/gnolang/faucet/log"
)

type Option func(f *Faucet)

// WithLogger specifies the logger for the faucet
func WithLogger(l log.Logger) Option {
func WithLogger(l *slog.Logger) Option {
return func(f *Faucet) {
f.logger = l
}
Expand Down
6 changes: 3 additions & 3 deletions writer/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package http
import (
"encoding/json"
"fmt"
"log/slog"
"net/http"

"github.com/gnolang/faucet/log"
"github.com/gnolang/faucet/writer"
)

var _ writer.ResponseWriter = (*ResponseWriter)(nil)

type ResponseWriter struct {
logger log.Logger
logger *slog.Logger
w http.ResponseWriter
}

func New(logger log.Logger, w http.ResponseWriter) ResponseWriter {
func New(logger *slog.Logger, w http.ResponseWriter) ResponseWriter {
return ResponseWriter{
logger: logger,
w: w,
Expand Down

0 comments on commit cd086ac

Please sign in to comment.