optslog

package
v0.35.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2025 License: Unlicense Imports: 3 Imported by: 2

Documentation

Overview

Package optslog contains optimizations making debug logs using log/slog allocate less when debug or trace modes are not enabled.

Example (Debug)
package main

import (
	"context"

	"github.com/AdguardTeam/golibs/logutil/optslog"
	"github.com/AdguardTeam/golibs/logutil/slogutil"
)

func main() {
	l := slogutil.New(&slogutil.Config{
		Level:  slogutil.LevelInfo,
		Format: slogutil.FormatText,
	})

	ctx := context.Background()

	optslog.Debug1(ctx, l, "one arg; not printed", "1", 1)
	optslog.Debug2(ctx, l, "two args; not printed", "1", 1, "2", 2)
	optslog.Debug3(ctx, l, "three args; not printed", "1", 1, "2", 2, "3", 3)
	optslog.Debug4(ctx, l, "four args; not printed", "1", 1, "2", 2, "3", 3, "4", 4)

	l = slogutil.New(&slogutil.Config{
		Level:  slogutil.LevelDebug,
		Format: slogutil.FormatText,
	})

	optslog.Debug1(ctx, l, "one arg; printed", "1", 1)
	optslog.Debug2(ctx, l, "two args; printed", "1", 1, "2", 2)
	optslog.Debug3(ctx, l, "three args; printed", "1", 1, "2", 2, "3", 3)
	optslog.Debug4(ctx, l, "four args; printed", "1", 1, "2", 2, "3", 3, "4", 4)

}
Output:

level=DEBUG msg="one arg; printed" 1=1
level=DEBUG msg="two args; printed" 1=1 2=2
level=DEBUG msg="three args; printed" 1=1 2=2 3=3
level=DEBUG msg="four args; printed" 1=1 2=2 3=3 4=4
Example (Trace)
package main

import (
	"context"

	"github.com/AdguardTeam/golibs/logutil/optslog"
	"github.com/AdguardTeam/golibs/logutil/slogutil"
)

func main() {
	l := slogutil.New(&slogutil.Config{
		Level:  slogutil.LevelInfo,
		Format: slogutil.FormatText,
	})

	ctx := context.Background()

	optslog.Trace1(ctx, l, "one arg; not printed", "1", 1)
	optslog.Trace2(ctx, l, "two args; not printed", "1", 1, "2", 2)
	optslog.Trace3(ctx, l, "three args; not printed", "1", 1, "2", 2, "3", 3)
	optslog.Trace4(ctx, l, "four args; not printed", "1", 1, "2", 2, "3", 3, "4", 4)

	l = slogutil.New(&slogutil.Config{
		Level:  slogutil.LevelTrace,
		Format: slogutil.FormatText,
	})

	optslog.Trace1(ctx, l, "one arg; printed", "1", 1)
	optslog.Trace2(ctx, l, "two args; printed", "1", 1, "2", 2)
	optslog.Trace3(ctx, l, "three args; printed", "1", 1, "2", 2, "3", 3)
	optslog.Trace4(ctx, l, "four args; printed", "1", 1, "2", 2, "3", 3, "4", 4)

}
Output:

level=TRACE msg="one arg; printed" 1=1
level=TRACE msg="two args; printed" 1=1 2=2
level=TRACE msg="three args; printed" 1=1 2=2 3=3
level=TRACE msg="four args; printed" 1=1 2=2 3=3 4=4

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug1

func Debug1[T1 any](ctx context.Context, l *slog.Logger, msg, name1 string, arg1 T1)

Debug1 is an optimized version of slog.Logger.DebugContext that prevents it from allocating when debugging is not necessary.

func Debug2

func Debug2[T1, T2 any](
	ctx context.Context,
	l *slog.Logger,
	msg string,
	name1 string, arg1 T1,
	name2 string, arg2 T2,
)

Debug2 is an optimized version of slog.Logger.DebugContext that prevents it from allocating when debugging is not necessary.

func Debug3

func Debug3[T1, T2, T3 any](
	ctx context.Context,
	l *slog.Logger,
	msg string,
	name1 string, arg1 T1,
	name2 string, arg2 T2,
	name3 string, arg3 T3,
)

Debug3 is an optimized version of slog.Logger.DebugContext that prevents it from allocating when debugging is not necessary.

func Debug4

func Debug4[T1, T2, T3, T4 any](
	ctx context.Context,
	l *slog.Logger,
	msg string,
	name1 string, arg1 T1,
	name2 string, arg2 T2,
	name3 string, arg3 T3,
	name4 string, arg4 T4,
)

Debug4 is an optimized version of slog.Logger.DebugContext that prevents it from allocating when debugging is not necessary.

func Trace1

func Trace1[T1 any](ctx context.Context, l *slog.Logger, msg, name1 string, arg1 T1)

Trace1 is an optimized version of slog.Logger.Log that prevents it from allocating when debugging is not necessary.

func Trace2

func Trace2[T1, T2 any](
	ctx context.Context,
	l *slog.Logger,
	msg,
	name1 string, arg1 T1,
	name2 string, arg2 T2,
)

Trace2 is an optimized version of slog.Logger.Log that prevents it from allocating when debugging is not necessary.

func Trace3

func Trace3[T1, T2, T3 any](
	ctx context.Context,
	l *slog.Logger,
	msg,
	name1 string, arg1 T1,
	name2 string, arg2 T2,
	name3 string, arg3 T3,
)

Trace3 is an optimized version of slog.Logger.Log that prevents it from allocating when debugging is not necessary.

func Trace4

func Trace4[T1, T2, T3, T4 any](
	ctx context.Context,
	l *slog.Logger,
	msg,
	name1 string, arg1 T1,
	name2 string, arg2 T2,
	name3 string, arg3 T3,
	name4 string, arg4 T4,
)

Trace4 is an optimized version of slog.Logger.Log that prevents it from allocating when debugging is not necessary.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL