xlog

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2025 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	XLOG_TRACE_ID_KEY string = "XLOG_TRACE_ID_KEY"
	XLOG_KEY          string = "XLOG_KEY"
)

Variables

This section is empty.

Functions

func AnyFieldToZeroLogContext added in v0.5.0

func AnyFieldToZeroLogContext(c zerolog.Context, key string, value any) zerolog.Context

func AnyFieldToZeroLogEvent added in v0.5.0

func AnyFieldToZeroLogEvent(c *zerolog.Event, key string, value any) *zerolog.Event

func AnyFieldsToContext added in v0.5.0

func AnyFieldsToContext(c zerolog.Context, m map[string]any) zerolog.Context

func AnyFieldsToEvent added in v0.5.0

func AnyFieldsToEvent(c *zerolog.Event, m map[string]any) *zerolog.Event

func FormatDuration added in v0.5.0

func FormatDuration(d time.Duration) string

FormatDuration dynamically formats time.Duration into ns, µs, ms, s, m, h, or d

func Msgf

func Msgf(ctx context.Context, msg string, args ...any) string

func NewZeroLog

func NewZeroLog(opts ...LogOptionFn) zerolog.Logger

func PgxQueryName added in v0.5.0

func PgxQueryName(ctx context.Context, name string) context.Context

func SetupFxLogger

func SetupFxLogger(logger *DebugLogger) fxevent.Logger

Types

type CtxKey added in v0.4.0

type CtxKey string
const (
	XLOG_REQ_TRACE_ID_CTX_KEY CtxKey = "XLOG_REQ_TRACE_ID_CTX_KEY"
)

func (CtxKey) String added in v0.4.0

func (s CtxKey) String() string

type DebugLogger

type DebugLogger struct {
	SingleLogger
}

type Entry

type Entry struct {
	Options LogOptions
	// contains filtered or unexported fields
}

Multi Logger

func NewEntry

func NewEntry(key string, rotation *lumberjack.Logger, opts ...LogOptionFn) Entry

Note:

  • SetLogFileOutput(io.Writer) this option is not used in this option.

type FxZeroLogger

type FxZeroLogger struct {
	DebugLog *DebugLogger
}

ZeroLogger is an Fx event logger that logs events to Zero.

func (*FxZeroLogger) LogEvent

func (l *FxZeroLogger) LogEvent(event fxevent.Event)

LogEvent logs the given event to the provided Zap logger.

type IOLogger

type IOLogger struct {
	SingleLogger
}

type LogArrayMarshaler added in v0.5.0

type LogArrayMarshaler interface {
	MarshalZerologArray(*zerolog.Array)
}

type LogObjectMarshaler added in v0.5.0

type LogObjectMarshaler interface {
	MarshalZerologObject(*zerolog.Event)
}

type LogOptionFn

type LogOptionFn func(opt *LogOptions)

func SetField

func SetField(key string, val any) LogOptionFn

func SetLogConsoleDisabled

func SetLogConsoleDisabled(v bool) LogOptionFn

func SetLogConsoleLevel

func SetLogConsoleLevel(lvl int) LogOptionFn

func SetLogConsoleOutput

func SetLogConsoleOutput(out io.Writer) LogOptionFn

func SetLogFileDisabled

func SetLogFileDisabled(v bool) LogOptionFn

func SetLogFileLevel

func SetLogFileLevel(lvl int) LogOptionFn

func SetLogFileOutput

func SetLogFileOutput(out io.Writer) LogOptionFn

func SetLogHook added in v0.5.0

func SetLogHook(v ...zerolog.Hook) LogOptionFn

type LogOptions

type LogOptions struct {
	// Log Hook
	Hook []zerolog.Hook

	// Log Fields
	LogFields map[string]any

	// Console
	LogConsoleDisable null.Bool
	LogConsoleLevel   int
	LogConsoleOut     io.Writer

	// File
	LogFileDisable null.Bool
	LogFileLevel   int
	LogFileOut     io.Writer
}

type Logger

type Logger interface {
	/*
		Fields is a helper function to use a map or slice to set fields using type assertion.
		[]any must alternate string keys and arbitrary values, and extraneous ones are ignored. i.e:

		With Request HTTP Context:

			var (
				first  = "first value"
				second = "second value"
			)
			<Imported_Log>.Trace(ctx, "hello", "first", first, "second", second)
			<Imported_Log>.Trace(ctx, xlog.Msgf("hello %s", "world!"), "first", first, "second", second)
			<Imported_Log>.Trace(ctx, "oh snap! got error", "error", err)
	*/
	Trace(ctx context.Context, msg string, fields ...any)

	/*
		Fields is a helper function to use a map or slice to set fields using type assertion.
		[]any must alternate string keys and arbitrary values, and extraneous ones are ignored. i.e:

		With Request HTTP Context:

			var (
				first  = "first value"
				second = "second value"
			)
			<Imported_Log>.Debug(ctx, "hello", "first", first, "second", second)
			<Imported_Log>.Debug(ctx, xlog.Msgf("hello %s", "world!"), "first", first, "second", second)
			<Imported_Log>.Debug(ctx, "oh snap! got error", "error", err)
	*/
	Debug(ctx context.Context, msg string, fields ...any)

	/*
		Fields is a helper function to use a map or slice to set fields using type assertion.
		[]any must alternate string keys and arbitrary values, and extraneous ones are ignored. i.e:

		With Request HTTP Context:

			var (
				first  = "first value"
				second = "second value"
			)
			<Imported_Log>.Info(ctx, "hello", "first", first, "second", second)
			<Imported_Log>.Info(ctx, xlog.Msgf("hello %s", "world!"), "first", first, "second", second)
			<Imported_Log>.Info(ctx, "oh snap! got error", "error", err)
	*/
	Info(ctx context.Context, msg string, fields ...any)

	/*
		Fields is a helper function to use a map or slice to set fields using type assertion.
		[]any must alternate string keys and arbitrary values, and extraneous ones are ignored. i.e:

		With Request HTTP Context:

			var (
				first  = "first value"
				second = "second value"
			)
			<Imported_Log>.Warn(ctx, "hello", "first", first, "second", second)
			<Imported_Log>.Warn(ctx, xlog.Msgf("hello %s", "world!"), "first", first, "second", second)
			<Imported_Log>.Warn(ctx, "oh snap! got error", "error", err)
	*/
	Warn(ctx context.Context, msg string, fields ...any)

	/*
		Fields is a helper function to use a map or slice to set fields using type assertion.
		[]any must alternate string keys and arbitrary values, and extraneous ones are ignored. i.e:

		With Request HTTP Context:

			var (
				first  = "first value"
				second = "second value"
			)
			<Imported_Log>.Error(ctx, "hello", "first", first, "second", second)
			<Imported_Log>.Error(ctx, xlog.Msgf("hello %s", "world!"), "first", first, "second", second)
			<Imported_Log>.Error(ctx, "oh snap! got error", "error", err)
	*/
	Error(ctx context.Context, msg string, fields ...any)

	/*
		Fields is a helper function to use a map or slice to set fields using type assertion.
		[]any must alternate string keys and arbitrary values, and extraneous ones are ignored. i.e:

		With Request HTTP Context:

			var (
				first  = "first value"
				second = "second value"
			)
			<Imported_Log>.Fatal(ctx, "hello", "first", first, "second", second)
			<Imported_Log>.Fatal(ctx, xlog.Msgf("hello %s", "world!"), "first", first, "second", second)
			<Imported_Log>.Fatal(ctx, "oh snap! got error", "error", err)
	*/
	Fatal(ctx context.Context, msg string, fields ...any)

	/*
		Fields is a helper function to use a map or slice to set fields using type assertion.
		[]any must alternate string keys and arbitrary values, and extraneous ones are ignored. i.e:

		With Request HTTP Context:

			var (
				first  = "first value"
				second = "second value"
			)
			<Imported_Log>.Panic(ctx, "hello", "first", first, "second", second)
			<Imported_Log>.Panic(ctx, xlog.Msgf("hello %s", "world!"), "first", first, "second", second)
			<Imported_Log>.Panic(ctx, "oh snap! got error", "error", err)
	*/
	Panic(ctx context.Context, msg string, fields ...any)
}

func FromEcho

func FromEcho(c echo.Context) Logger

func NewLogger

func NewLogger(log zerolog.Logger) Logger

type MultiLogger

type MultiLogger struct {
	// contains filtered or unexported fields
}

func NewMultiLogging

func NewMultiLogging(entries ...Entry) MultiLogger

func (*MultiLogger) Close

func (c *MultiLogger) Close()

func (*MultiLogger) LogBy

func (c *MultiLogger) LogBy(key string) zerolog.Logger

func (*MultiLogger) Rotate

func (c *MultiLogger) Rotate() error

func (*MultiLogger) RotateBy

func (c *MultiLogger) RotateBy(key string) error

type Option added in v0.5.0

type Option interface {
	// contains filtered or unexported methods
}

Option configures a Hook.

func WithLevel added in v0.5.0

func WithLevel(lvl zerolog.Level) Option

WithLevel returns an Option that configures the version of the log.Logger used by a [OtelLogger]. The version should be the version of the package that is being logged.

func WithLogEnabled added in v0.5.0

func WithLogEnabled(v bool) Option

func WithLogWriterDisabled added in v0.5.0

func WithLogWriterDisabled(v bool) Option

func WithLoggerProvider added in v0.5.0

func WithLoggerProvider(provider log.LoggerProvider) Option

WithLoggerProvider returns an Option that configures log.LoggerProvider

By default if this Option is not provided, the Logger will use the global LoggerProvider.

func WithSchemaURL added in v0.5.0

func WithSchemaURL(schemaURL string) Option

WithSchemaURL returns an Option that configures the semantic convention schema URL of the log.Logger used by a [OtelLogger]. The schemaURL should be the schema URL for the semantic conventions used in log records.

func WithServerAddress added in v0.5.0

func WithServerAddress(serverAddres string) Option

WithServerAddress returns an Option that configures the semantic convention Server Address of the log.Logger used by a [OtelLogger]. The Server Address should be the Server Address for the semantic conventions used in log records.

func WithServerName added in v0.5.0

func WithServerName(serverName string) Option

WithServerName returns an Option that configures the semantic convention Server Name of the log.Logger used by a [OtelLogger]. The Server Name should be the Server Name for the semantic conventions used in log records.

func WithVersion added in v0.5.0

func WithVersion(version string) Option

WithVersion returns an Option that configures the version of the log.Logger used by a [OtelLogger]. The version should be the version of the package that is being logged.

type OtelHook added in v0.5.0

type OtelHook struct {
	// contains filtered or unexported fields
}

Hook is a zerolog.Hook that sends all logging records it receives to OpenTelemetry. See package documentation for how conversions are made.

func NewOtelHook added in v0.5.0

func NewOtelHook(name string, options ...Option) *OtelHook

NewHook returns a new [Hook] to be used as a [Zerolog.Hook]. If WithLoggerProvider is not provided, the returned Hook will use the global LoggerProvider.

func (OtelHook) Run added in v0.5.0

func (h OtelHook) Run(e *zerolog.Event, level zerolog.Level, msg string)

Run handles the passed record, and sends it to OpenTelemetry.

type PgxLogger added in v0.5.0

type PgxLogger struct {
	Log Logger
}

PgxLogger is a custom QueryTracer implementation for pgx using zerolog.

func (*PgxLogger) TraceQueryEnd added in v0.5.0

func (t *PgxLogger) TraceQueryEnd(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData)

TraceQueryEnd logs the end of a query.

func (*PgxLogger) TraceQueryStart added in v0.5.0

func (t *PgxLogger) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryStartData) context.Context

TraceQueryStart logs the start of a query.

type SingleLogger

type SingleLogger struct {
	Logger      zerolog.Logger
	LogRotation lumberjack.Logger
}

func NoopSingleLogger

func NoopSingleLogger() SingleLogger

type TrxLogger

type TrxLogger struct {
	MultiLogger
}

type ZeroLogger

type ZeroLogger struct {
	// contains filtered or unexported fields
}

func (*ZeroLogger) Debug

func (zl *ZeroLogger) Debug(ctx context.Context, msg string, fields ...any)

func (*ZeroLogger) Error

func (zl *ZeroLogger) Error(ctx context.Context, msg string, fields ...any)

func (*ZeroLogger) Fatal

func (zl *ZeroLogger) Fatal(ctx context.Context, msg string, fields ...any)

func (*ZeroLogger) Info

func (zl *ZeroLogger) Info(ctx context.Context, msg string, fields ...any)

func (*ZeroLogger) Panic

func (zl *ZeroLogger) Panic(ctx context.Context, msg string, fields ...any)

func (*ZeroLogger) Trace

func (zl *ZeroLogger) Trace(ctx context.Context, msg string, fields ...any)

func (*ZeroLogger) Warn

func (zl *ZeroLogger) Warn(ctx context.Context, msg string, fields ...any)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳