Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TraceLog changes #1822

Merged
merged 27 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ func isNil(i any) bool {
}

func (s *Server) handleRequest(ctx context.Context, req *Request) (*response, error) {
s.log.Tracew("Received request", "req", req)
if err := req.isSane(); err != nil {
s.log.Tracew("Request sanity check failed", "err", err)
return nil, err
}

Expand All @@ -409,6 +411,7 @@ func (s *Server) handleRequest(ctx context.Context, req *Request) (*response, er
calledMethod, found := s.methods[req.Method]
if !found {
res.Error = Err(MethodNotFound, nil)
s.log.Tracew("Method not found in request", "method", req.Method)
return res, nil
}

Expand All @@ -417,6 +420,7 @@ func (s *Server) handleRequest(ctx context.Context, req *Request) (*response, er
args, err := s.buildArguments(ctx, req.Params, calledMethod)
if err != nil {
res.Error = Err(InvalidParams, err.Error())
s.log.Tracew("Error building arguments for RPC call", "err", err)
return res, nil
}
defer func() {
Expand All @@ -425,6 +429,7 @@ func (s *Server) handleRequest(ctx context.Context, req *Request) (*response, er

tuple := reflect.ValueOf(calledMethod.Handler).Call(args)
if res.ID == nil { // notification
s.log.Tracew("Notification received, no response expected")
return nil, nil
}

Expand All @@ -439,6 +444,7 @@ func (s *Server) handleRequest(ctx context.Context, req *Request) (*response, er
return res, nil
}
res.Result = tuple[0].Interface()

return res, nil
}

Expand Down
3 changes: 2 additions & 1 deletion p2p/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ func (s *syncService) logError(msg string, err error) {
if !errors.Is(err, context.Canceled) {
var log utils.SimpleLogger
if v, ok := s.log.(*utils.ZapLogger); ok {
log = v.WithOptions(zap.AddCallerSkip(1))
enhancedLogger := v.SugaredLogger.Desugar().WithOptions(zap.AddCallerSkip(1)).Sugar()
log = &utils.ZapLogger{SugaredLogger: enhancedLogger}
} else {
log = s.log
}
Expand Down
5 changes: 5 additions & 0 deletions upgrader/upgrader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type upgradeLogger struct {
infoMsg string
warnMsg string
errorMsg string
traceMsg string
}

func (l *upgradeLogger) Debugw(msg string, keysAndValues ...any) {}
Expand All @@ -35,6 +36,10 @@ func (l *upgradeLogger) Errorw(msg string, keysAndValues ...any) {
l.errorMsg = msg
}

func (l *upgradeLogger) Tracew(msg string, keysAndValues ...any) {
l.traceMsg = msg
}

func newVersion(t *testing.T, v string) semver.Version {
version, err := semver.StrictNewVersion(v)
require.NoError(t, err)
Expand Down
38 changes: 33 additions & 5 deletions utils/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
)

var ErrUnknownLogLevel = fmt.Errorf(
"unknown log level (known: %s, %s, %s, %s)",
DEBUG, INFO, WARN, ERROR,
"unknown log level (known: %s, %s, %s, %s, %s)",
DEBUG, INFO, WARN, ERROR, TRACE,
)

type LogLevel int
Expand All @@ -30,6 +30,7 @@
INFO
WARN
ERROR
TRACE
)

func (l LogLevel) String() string {
Expand All @@ -42,6 +43,8 @@
return "warn"
case ERROR:
return "error"
case TRACE:
return "trace"
default:
// Should not happen.
panic(ErrUnknownLogLevel)
Expand All @@ -62,6 +65,8 @@
*l = WARN
case "ERROR", "error":
*l = ERROR
case "TRACE", "trace":
*l = TRACE
default:
return ErrUnknownLogLevel
}
Expand Down Expand Up @@ -90,12 +95,27 @@
Infow(msg string, keysAndValues ...any)
Warnw(msg string, keysAndValues ...any)
Errorw(msg string, keysAndValues ...any)
Tracew(msg string, keysAndValues ...any)
}

type ZapLogger struct {
*zap.SugaredLogger
}

const (
traceLevel = zapcore.Level(-2)
)

func (l *ZapLogger) IsTraceEnabled() bool {
return l.Desugar().Core().Enabled(traceLevel)
}

func (l *ZapLogger) Tracew(msg string, keysAndValues ...interface{}) {
if l.IsTraceEnabled() {
l.Debugw("[TRACE] "+msg, keysAndValues...) // Hack: we use Debug for traces

Check warning on line 115 in utils/log.go

View check run for this annotation

Codecov / codecov/patch

utils/log.go#L115

Added line #L115 was not covered by tests
}
}

var _ Logger = (*ZapLogger)(nil)

func NewNopZapLogger() *ZapLogger {
Expand All @@ -113,9 +133,17 @@
config.EncoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Local().Format("15:04:05.000 02/01/2006 -07:00"))
}
level, err := zapcore.ParseLevel(logLevel.String())
if err != nil {
return nil, err

var level zapcore.Level
var err error
levelStr := logLevel.String()
if logLevel == TRACE {
rianhughes marked this conversation as resolved.
Show resolved Hide resolved
level = traceLevel
} else {
level, err = zapcore.ParseLevel(levelStr)
if err != nil {
return nil, err

Check warning on line 145 in utils/log.go

View check run for this annotation

Codecov / codecov/patch

utils/log.go#L145

Added line #L145 was not covered by tests
}
}
config.Level.SetLevel(level)
log, err := config.Build()
Expand Down
1 change: 1 addition & 0 deletions utils/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var levelStrings = map[utils.LogLevel]string{
utils.INFO: "info",
utils.WARN: "warn",
utils.ERROR: "error",
utils.TRACE: "trace",
}

func TestLogLevelString(t *testing.T) {
Expand Down
Loading