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 logging Example : log only first field, when using #693

Closed
wants to merge 0 commits into from

Conversation

arckadious
Copy link
Contributor

Changes

This PR follows issue #691.

Verification

grpc_logging.UnaryServerInterceptor(InterceptorLogrus(logger),grpc_logging.WithLogOnEvents(grpc_logging.FinishCall)),

My logging interceptor function :

func InterceptorLogrus(l log.FieldLogger) grpc_logging.Logger {
	return grpc_logging.LoggerFunc(func(_ context.Context, lvl grpc_logging.Level, msg string, fields ...any) {
		f := make(map[string]any, len(fields)/2)

		logFields := grpc_logging.Fields(fields)

		i := logFields.Iterator()

		for i.Next() {
			k, v := i.At()
			f[k] = v
		}
		l := l.WithFields(f)

		if _, ok := f["protocol"]; ok {
			l = l.WithField("system", f["protocol"])
		}
		l = l.WithField("span.kind", "server")

		switch lvl {
		case grpc_logging.LevelDebug:
			l.Debug(msg)
		case grpc_logging.LevelInfo:
			l.Info(msg)
		case grpc_logging.LevelWarn:
			l.Warn(msg)
		case grpc_logging.LevelError:
			l.Error(msg)
		default:
			l.Error(fmt.Sprintf("InterceptorLogrus : unknown level %v", lvl), " - logging message : ", msg)
		}
	})
}

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.

1 participant