Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Conversation

@tonyredondo
Copy link
Contributor

@tonyredondo tonyredondo commented Apr 16, 2020

Closes #47

This PR contains a logrus hook implementation to write the logs in the span of the given context.

Usage

	ctx := scopeagent.GetContextFromTest(t).   // Gets the test context

	logger := logrus.New()
	logger.SetLevel(logrus.TraceLevel)
	logger.SetReportCaller(true)   // Allow to report the file and line number in the log event source
	AddScopeHook(logger)  // Adds the scope hook into the logros logger

	logger.WithContext(ctx).WithField("Data", "Value").Error("Error message")
	logger.WithContext(ctx).WithField("Data", "Value").Warn("Warning message")
	logger.WithContext(ctx).WithField("Data", "Value").Info("Info message")
	logger.WithContext(ctx).WithField("Data", "Value").Debug("Debug message")
	logger.WithContext(ctx).WithField("Data", "Value").Trace("Trace message")

Results
image

@tonyredondo tonyredondo self-assigned this Apr 16, 2020
@drodriguezhdez
Copy link
Contributor

Awesome @tonyredondo

I have a question: Can we have detect if the scope Hook was added before? To avoid adding several times.

I'm experiencing an issue in docker/distribution repo due to logrus.StandardLogger() returns a singleton, if the docker app is initialized several times, the hook is added those times with the following result:

image

image

@tonyredondo
Copy link
Contributor Author

Mmm ok, I’ll make the change.

@tonyredondo
Copy link
Contributor Author

Sorry for the delay, I've added a helper function for the problem you told me:

https://github.com/undefinedlabs/scope-go-agent/blob/logrus-support/instrumentation/logrus/hook.go#L24-L35

Just call it like:

AddScopeHook(logger)  // Adds the scope hook into the logros logger

@tonyredondo
Copy link
Contributor Author

@drodriguezhdez

@drodriguezhdez
Copy link
Contributor

awesome. Thanks @tonyredondo !

@tonyredondo tonyredondo merged commit 503d89f into master Apr 27, 2020
@tonyredondo tonyredondo deleted the logrus-support branch April 27, 2020 16:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for Logrus

3 participants