Skip to content

Commit

Permalink
make log more pertty (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
fyInALT authored Apr 3, 2024
1 parent 64f1c4b commit b930d4b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
3 changes: 2 additions & 1 deletion core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"

"github.com/alt-research/avs/core"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/urfave/cli"
Expand Down Expand Up @@ -110,7 +111,7 @@ func NewConfig(ctx *cli.Context) (*Config, error) {
}
}

logger, err := sdklogging.NewZapLogger(configRaw.Environment)
logger, err := core.NewZapLogger(configRaw.Environment)
if err != nil {
return nil, err
}
Expand Down
80 changes: 80 additions & 0 deletions core/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package core

import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

sdklogging "github.com/Layr-Labs/eigensdk-go/logging"
)

type ZapLogger struct {
logger *zap.Logger
}

var _ sdklogging.Logger = (*ZapLogger)(nil)

func NewZapLogger(env sdklogging.LogLevel) (sdklogging.Logger, error) {
config := zap.NewProductionConfig()
if env == sdklogging.Development {
config = zap.NewDevelopmentConfig()
}

config.DisableStacktrace = true
config.Encoding = "console"
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
config.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder

logger, err := config.Build(zap.AddCallerSkip(1))
if err != nil {
panic(err)
}
return &ZapLogger{
logger: logger,
}, nil
}

func (z *ZapLogger) Debug(msg string, tags ...any) {
z.logger.Sugar().Debugw(msg, tags...)
}

func (z *ZapLogger) Info(msg string, tags ...any) {
z.logger.Sugar().Infow(msg, tags...)
}

func (z *ZapLogger) Warn(msg string, tags ...any) {
z.logger.Sugar().Warnw(msg, tags...)
}

func (z *ZapLogger) Error(msg string, tags ...any) {
z.logger.Sugar().Errorw(msg, tags...)
}

func (z *ZapLogger) Fatal(msg string, tags ...any) {
z.logger.Sugar().Fatalw(msg, tags...)
}

func (z *ZapLogger) Debugf(template string, args ...interface{}) {
z.logger.Sugar().Debugf(template, args...)
}

func (z *ZapLogger) Infof(template string, args ...interface{}) {
z.logger.Sugar().Infof(template, args...)
}

func (z *ZapLogger) Warnf(template string, args ...interface{}) {
z.logger.Sugar().Warnf(template, args...)
}

func (z *ZapLogger) Errorf(template string, args ...interface{}) {
z.logger.Sugar().Errorf(template, args...)
}

func (z *ZapLogger) Fatalf(template string, args ...interface{}) {
z.logger.Sugar().Fatalf(template, args...)
}

func (z *ZapLogger) With(tags ...any) sdklogging.Logger {
return &ZapLogger{
logger: z.logger.Sugar().With(tags...).Desugar(),
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/sourcegraph/jsonrpc2 v0.2.0
github.com/urfave/cli v1.22.14
go.uber.org/mock v0.4.0
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.18.0
)

Expand Down Expand Up @@ -50,7 +51,6 @@ require (
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/sync v0.5.0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/prometheus/client_golang/prometheus"

"github.com/alt-research/avs/core"
"github.com/alt-research/avs/core/alert"
"github.com/alt-research/avs/core/chainio"
"github.com/alt-research/avs/core/config"
Expand Down Expand Up @@ -198,7 +199,7 @@ func NewOperatorFromConfig(cfg config.NodeConfig) (*Operator, error) {
} else {
logLevel = sdklogging.Development
}
logger, err := sdklogging.NewZapLogger(logLevel)
logger, err := core.NewZapLogger(logLevel)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b930d4b

Please sign in to comment.