Releases: tryfix/log
v1.4.0
Context Map Extractor
This release introduced a bug(⚠️ CtxMap doesn't get copied, when creating nested loggers) and should not be used
What's Changed
Full Changelog: v1.2.1...v1.3.0
Bug Fixes
New Configuration Options
Skip Frames
log.FileDepth
is deprecated and is replaced by log.WithSkipFrameCount
.
This is used to configure how many stack frames to skip when retrieving the callers file name, line number and function path.
Log Output
The logger supports two output formats. Text and JSON
Use log.WithOutput
to configure this option.
constructor := log.NewLog(log.WithOutput(log.OutJson))
logger := constructor.Log()
logger.Error("message")
When this option is not provided the log output will default to Text.
Also, the standard logger's output is Text.
Note:
Sub loggers of a logger will always inherit the output type of the parent logger.
You cannot alternate between output types by passing inlog.WithOutput
option to the sub logger.
Caller Information
log.WithFilePath
and log.WithFuncPath
oprions can be used to configure whether to log the caller's file path, line number and function path.
Trace Id Extraction
Automatic Trace Id extraction from the context is removed. Use the log.WithCtxTraceExtractor
configuration option to set up a Trace Id extractor function.
ctx := traceable_context.WithUUID(uuid.New())
logger := constructor.Log(
log.WithLevel(log.TRACE),
log.WithCtxTraceExtractor(func(ctx context.Context) string {
if trace := traceable_context.FromContext(ctx); trace != uuid.Nil {
return trace.String()
}
return ""
}))
logger.ErrorContext(ctx, "message", "param1", "param2")
Prefix Adder
log.WithPrefix
is deprecated. Do not use it when the logger is configured to output JSON.
Context Extraction
Details can be extracted from the context and attached to the log entry now.
This is done by using the new WithCtxExtractor
function.
An extraction function that has the signature of func(ctx context.Context) []interface{}
can be passed in to the logger using WithCtxExtractor
when creating a new logger.
type keyOne string
const k1 keyOne = "key1"
lCtx := context.Background()
lCtx = context.WithValue(lCtx, k1, "context_val_1")
ctxLogger := log.Constructor.Log(log.WithColors(true),
log.WithLevel(log.TRACE),
log.WithFilePath(false),
log.Prefixed(`context_logger`),
log.WithCtxExtractor(func(ctx context.Context) []interface{} {
return []interface{}{
fmt.Sprintf("%s: %+v", k1, ctx.Value(k1)),
}
}),
)
ctxLogger.ErrorContext(lCtx, `message`)
Output
Details extracted from the context extraction function will be appended to the very end of the log message.
2021/05/07 13:32:45.415464 [ERROR] [690452e4-e456-4f21-a759-36969a63e090] [context_logger] [message on func main.main] [key1: context_val_1]
Additional context extractor functions can be attache when creating child loggers from a logger. This will enable the child logger to use context extraction functions of the parent logger as well.
type keyOne string
type keyTwo string
const k1 keyOne = "key1"
const k2 keyTwo = "key2"
lCtx := context.Background()
lCtx = context.WithValue(lCtx, k1, "context_val_1")
lCtx = context.WithValue(lCtx, k2, "context_val_2")
// child logger with additional context extraction functionality
ctxChildLogger := ctxLogger.NewLog(log.Prefixed(`context_child_logger`),
log.WithCtxExtractor(func(ctx context.Context) []interface{} {
return []interface{}{
fmt.Sprintf("%s: %+v", k2, ctx.Value(k2)),
}
}),
)
ctxChildLogger.ErrorContext(lCtx, `message`)
Output
2021/05/07 13:32:45.415507 [ERROR] [41127b91-b6de-45f7-a512-618f82c6e54e] [context_logger.context_child_logger] [message on func main.main] [key1: context_val_1 key2: context_val_2]
Dependency updates
v1.0.2 go mod updates
GoMod Support
v1.0.1 go mod added
Initial Release
v1.0.0 initial commit