Documentation ¶
Index ¶
- Variables
- func WithFields(ctx context.Context, newFieldsList ...map[string]interface{}) context.Context
- type CtxLogger
- func (c CtxLogger) Debug(ctx context.Context, msg string, fields ...map[string]interface{})
- func (c CtxLogger) Error(ctx context.Context, msg string, fields ...map[string]interface{})
- func (c CtxLogger) Info(ctx context.Context, msg string, fields ...map[string]interface{})
- func (c CtxLogger) Trace(ctx context.Context, msg string, fields ...map[string]interface{})
- func (c CtxLogger) Warn(ctx context.Context, msg string, fields ...map[string]interface{})
- type Logger
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var LogKey = logKey("ctxlog")
LogKey is the key used to store and retrieve the entry log You'll probably want to store the initial entry at the very beginning of the request/trace/whatever
Functions ¶
func WithFields ¶
WithFields adds a map of fields to the context. If a map already exists, entries will be merged. Keys with the same name will be overwritten
Example ¶
ctx := ctxlog.WithFields(context.Background(), map[string]interface{}{"banana": true}) fields := ctx.Value(ctxlog.LogKey).(map[string]interface{}) fmt.Println(fields)
Output: map[banana:true]
Example (Merge) ¶
ctx := context.Background() fields, _ := ctx.Value(ctxlog.LogKey).(map[string]interface{}) fmt.Println("background", fields) ctx = ctxlog.WithFields(ctx, map[string]interface{}{"banana": true}) fields = ctx.Value(ctxlog.LogKey).(map[string]interface{}) fmt.Println("banana", fields) ctx = ctxlog.WithFields(ctx, map[string]interface{}{"apple": true}) fields = ctx.Value(ctxlog.LogKey).(map[string]interface{}) fmt.Println("apple", fields)
Output: background map[] banana map[banana:true] apple map[apple:true banana:true]
Types ¶
type CtxLogger ¶
CtxLogger is a wrapper around a regular logur Logger. When one of its method is called. it forwards to the logger not only the fields specified in the method call, but also fields that were saved into the context with the function WithFields
type Logger ¶
type Logger interface { Trace(ctx context.Context, msg string, fields ...map[string]interface{}) Debug(ctx context.Context, msg string, fields ...map[string]interface{}) Info(ctx context.Context, msg string, fields ...map[string]interface{}) Warn(ctx context.Context, msg string, fields ...map[string]interface{}) Error(ctx context.Context, msg string, fields ...map[string]interface{}) }
Logger is an interface very similar to the one from logur.dev, except for the first parameter which is context
Click to show internal directories.
Click to hide internal directories.