Skip to content

Releases: tryfix/log

v1.4.0

18 Nov 05:11
e24e52b
Compare
Choose a tag to compare

Bug Fixes

What's Changed

  • CtxMap doesn't get copied when creating nested loggers fixed by @gmbyapa in #6

Full Changelog: v1.3.0...v1.4.0

Context Map Extractor

12 Nov 20:49
f2af1e2
Compare
Choose a tag to compare
Context Map Extractor Pre-release
Pre-release

This release introduced a bug(⚠️ CtxMap doesn't get copied, when creating nested loggers) and should not be used

What's Changed

  • Context Map Extractor Added(WithCtxMapExtractor) by @gmbyapa in #5

Full Changelog: v1.2.1...v1.3.0

Bug Fixes

17 Aug 11:21
8b4198a
Compare
Choose a tag to compare

Fixes

  • #3 - @kosatnkn - fix: json logger cannot print complex param types

New Configuration Options

09 Jun 15:00
92a0523
Compare
Choose a tag to compare

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 in log.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

07 May 08:17
ed3e728
Compare
Choose a tag to compare

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

18 Mar 16:06
Compare
Choose a tag to compare
v1.0.2

go mod updates

GoMod Support

18 Mar 16:01
Compare
Choose a tag to compare
v1.0.1

go mod added

Initial Release

18 Mar 15:59
Compare
Choose a tag to compare
v1.0.0

initial commit