Skip to content

Commit

Permalink
feat: enable logger from env
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Jun 22, 2023
1 parent 2a5fd45 commit 8bedb52
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,41 @@ import (
"context"
"fmt"
"io"
"os"
"time"

"github.com/dgraph-io/badger/v2/options"
"github.com/ipfs/go-datastore"
badger "github.com/ipfs/go-ds-badger2"
"go.uber.org/zap"
"google.golang.org/grpc"

"berty.tech/weshnet/pkg/grpcutil"
"berty.tech/weshnet/pkg/ipfsutil"
ipfs_mobile "berty.tech/weshnet/pkg/ipfsutil/mobile"
"berty.tech/weshnet/pkg/logutil"
"berty.tech/weshnet/pkg/protocoltypes"
)

const (
defaultLoggingFiltersKey = ":default:"
defaultLoggingFiltersValue = "info+:bty.* error+:*,-ipfs*,-*.tyber"
)

type ServiceClient interface {
protocoltypes.ProtocolServiceClient

io.Closer
}

func NewServiceClient(opts Opts) (ServiceClient, error) {
var err error

cleanupLogger := func() {}
if opts.Logger == nil {
opts.Logger, cleanupLogger, err = setupDefaultLogger()
}

svc, err := NewService(opts)
if err != nil {
return nil, err
Expand All @@ -43,6 +58,7 @@ func NewServiceClient(opts Opts) (ServiceClient, error) {
ServiceClient: c,
server: s,
service: svc,
cleanup: cleanupLogger,
}, nil
}

Expand Down Expand Up @@ -97,6 +113,9 @@ func NewPersistentServiceClient(path string) (ServiceClient, error) {

opts.RootDatastore = ds

cleanupLogger := func() {}
opts.Logger, cleanupLogger, err = setupDefaultLogger()

cl, err := NewServiceClient(opts)
if err != nil {
return nil, err
Expand All @@ -105,6 +124,7 @@ func NewPersistentServiceClient(path string) (ServiceClient, error) {
return &persistentServiceClient{
ServiceClient: cl,
ds: ds,
cleanup: cleanupLogger,
}, nil
}

Expand All @@ -115,11 +135,13 @@ type serviceClient struct {

service Service
server *grpc.Server
cleanup func()
}

type persistentServiceClient struct {
ServiceClient
ds datastore.Batching
ds datastore.Batching
cleanup func()
}

func (p *persistentServiceClient) Close() error {
Expand All @@ -129,13 +151,25 @@ func (p *persistentServiceClient) Close() error {
// only return ds error if no error have been catch earlier
err = fmt.Errorf("unable to close datastore: %w", dserr)
}

if p.cleanup != nil {
p.cleanup()
}

return err
}

func (c *serviceClient) Close() error {
func (c *serviceClient) Close() (err error) {
c.server.GracefulStop() // gracefully stop grpc server
_ = c.ServiceClient.Close() // close client and discard error
return c.service.Close() // return real service error

err = c.service.Close()

if c.cleanup != nil {
c.cleanup()
}

return // return real service error
}

type client struct {
Expand Down Expand Up @@ -170,3 +204,17 @@ func NewClientFromService(ctx context.Context, s *grpc.Server, svc Service, opts
l: bl,
}, nil
}

func setupDefaultLogger() (logger *zap.Logger, cleanup func(), err error) {
// setup log from env
if logfilter := os.Getenv("WESHNET_LOG_FILTER"); logfilter != "" {
if logfilter == defaultLoggingFiltersKey {
logfilter = defaultLoggingFiltersValue
}

s := logutil.NewStdStream(logfilter, "color", os.Stderr.Name())
return logutil.NewLogger(s)
}

return zap.NewNop(), func() {}, nil
}

0 comments on commit 8bedb52

Please sign in to comment.