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

Fix: Add AddCallerSkip for zap to show real caller #178

Merged
merged 2 commits into from
Apr 4, 2024

Conversation

fyInALT
Copy link
Contributor

@fyInALT fyInALT commented Apr 4, 2024

Motivation

For a zap logger wrapper, if not contain the AddCallerSkip, it 's caller will be the log func 's pos:

2024-04-04T15:59:14.151+0800    DEBUG   logging/zap_logger.go:45        List of queried operator registration events in blsApkRegistry{"alreadyRegisteredOperatorAddr": ["0xe9a7669ac9ebe9b7e21e0a323fc3a6f34ce744eb"], "service": "OperatorPubkeysServiceInMemory"}
2024-04-04T15:59:14.151+0800    INFO    logging/zap_logger.go:69        Starting aggregator.
2024-04-04T15:59:14.151+0800    INFO    logging/zap_logger.go:69        Starting aggregator rpc server.
2024-04-04T15:59:14.151+0800    INFO    logging/zap_logger.go:49        Aggregator set to send new task every 10 seconds...
2024-04-04T15:59:14.151+0800    INFO    logging/zap_logger.go:49        Start LegacyRpcServer   {"addr": "localhost:8090"}

Note all log 's caller is the logging wrapper, which is:

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

Solution

So we need add AddCallerSkip(1), then we will got:

2024-04-04T16:03:43.217+0800    INFO    aggregator/aggregator.go:120    Starting aggregator.
2024-04-04T16:03:43.217+0800    INFO    aggregator/aggregator.go:121    Starting aggregator rpc server.
2024-04-04T16:03:43.217+0800    INFO    aggregator/aggregator.go:125    Aggregator set to send new task every 10 seconds...
2024-04-04T16:03:43.217+0800    INFO    rpc/legacy.go:34        Start LegacyRpcServer   {"addr": "localhost:8090"}

Note the user may define the zap logger by spec configs, so we add a new func to create:

// NewZapLoggerByConfig creates a logger wrapped the zap.Logger
// Note if the logger need to show the caller, need use `zap.AddCallerSkip(1)` ad options
func NewZapLoggerByConfig(config zap.Config, options ...zap.Option) (Logger, error) 

@shrimalmadhur
Copy link
Collaborator

this is nice. thanks for fixing it.

@shrimalmadhur shrimalmadhur merged commit a887202 into Layr-Labs:master Apr 4, 2024
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants