Please note that this repository has been deprecated and is no longer actively maintained by Polyverse Corporation. It may be removed in the future, but for now remains public for the benefit of any users.
Importantly, as the repository has not been maintained, it may contain unpatched security issues and other critical issues. Use at your own risk.
While it is not maintained, we would graciously consider any pull requests in accordance with our Individual Contributor License Agreement. https://github.com/polyverse/contributor-license-agreement
For any other issues, please feel free to contact [email protected]
A Logrus Hook to pull keys from Context (when passed) and convert into log fields.
package main
import (
log "github.com/sirupsen/logrus"
"github.com/polyverse/logrus_context_hook"
)
func main() {
// Add the Context Hook
log.AddHook(logrus_context_hook.NewContextHook("*", // * is special for ANY field of type context.Context.
[]string{"ServerId", "RequestId", "HostId"})) //Second parameter is the list of keys in that context to look for.
// Then log with as you do
log.Infof("Test1: Hello from Logger...")
// OR Log with Context field name
ctx := context.WithValue(context.Background(), "ServerId", "DemoServer")
log.WithField("Context", ctx).Infof("Test1: Hello from Logger with context under field 'Context'")
// OR Log with Context under any field name (because of the "*")
ctx = context.WithValue(context.Background(), "RequestId", "Want this logged anyway")
log.WithField("NotContextField", ctx).Infof("Test1: Hello from Logger with context under field 'NotContextField'")
}
package main
import (
log "github.com/sirupsen/logrus"
"github.com/polyverse/logrus_context_hook"
)
func main() {
// Add the Context Hook
log.AddHook(logrus_context_hook.NewContextHook("Context", // First parameter is the field name to look for context at
[]string{"ServerId", "RequestId", "HostId"})) //Second parameter is the list of keys in that context to look for.
// Then log with as you do
log.Infof("Test2: Hello from Logger...");
// OR Log with Context field Name
ctx := context.WithValue(context.Background(), "ServerId", "DemoServer")
log.WithField("Context", ctx).Infof("Test2: Hello from Logger with context under field 'Context'")
// EXCEPT Log without Context field name, and hook does nothing
ctx = context.WithValue(context.Background(), "RequestId", "Don't Want this logged")
log.WithField("NotContextField", ctx).Infof("Test2: Hello from Logger with context under field 'NotContextField'")
}
package main
import (
log "github.com/sirupsen/logrus"
"github.com/polyverse/logrus_context_hook"
)
func main() {
// Hold reference to the hook
ctxHook := logrus_context_hook.NewContextHook("Context", // First parameter is the field to look for context at
[]string{"ServerId", "RequestId", "HostId"}) //Second parameter is the list of keys in that context to look for.
// Add the Context Hook
log.AddHook(ctxHook)
// Then log with as you do
log.Infof("Test3: Hello from Logger...");
// OR Log with Context field
ctx := context.WithValue(context.Background(), "ServerId", "DemoServer")
log.WithField("Context", ctx).Infof("Test3: Hello from Logger with context under field 'Context'")
// EXCEPT Log without Context field, and hook does nothing
ctx = context.WithValue(context.Background(), "RequestId", "Don't Want this logged")
log.WithField("NotContextField", ctx).Infof("Test3: Hello from Logger with context under field 'NotContextField'")
// BUT we can change that at runtime
ctxHook.SetContextField("*")
// AND it works now...
ctx = context.WithValue(context.Background(), "RequestId", "Changed our mind at runtime. We want this logged.")
log.WithField("NotContextField", ctx).Infof("Test3: Hello from Logger with context under field 'NotContextField'")
// EXCEPT we just need a new key called UserId
ctx = context.WithValue(context.Background(), "UserId", "This key wasn't being looked for")
log.WithField("NotContextField", ctx).Infof("Test3: Hello from Logger with context having new key 'UserId'")
// BUT we can change that at runtime TOO
ctxHook.SetContextKeys(append(ctxHook.GetContextKeys(), "UserId"))
// AMD it works now
ctx = context.WithValue(context.Background(), "UserId", "This key is being looked for. Changed our mind at runtime again.")
log.WithField("NotContextField", ctx).Infof("Test3: Hello from Logger with context having new key 'UserId'")
}