log

package
v0.0.0-...-a05a5a5 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package log implements a leveled logger with a simple API.

Index

Constants

This section is empty.

Variables

View Source
var (
	StdOutWriter = NewSimpWriter(os.Stdout)
	StdErrWriter = NewSimpWriter(os.Stderr)
)

stdout and stderr writer is created by default.

DefaultLevelWriter is the default LevelWriter.

View Source
var (
	ErrClosedWriter = errors.New("closed writer")
)

Functions

func WithTrace

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

WithTrace assigns a trace ID to the context. when applying this context to the logger, the trace ID will be logged in the log message. The trace ID will be generated automatically. User can specify a name for the trace, which will be logged in the log message as well. If provided context have nested trace, they will be logged as a parent-child relationship.

Types

type Level

type Level int32

Level represents the log level.

const (
	LLDebug Level = iota
	LLInfo
	LLWarn
	LLError
)

Log levels.

type LevelLogger

type LevelLogger interface {
	Debug(args ...interface{})
	// The method with suffix "xt" returns log function if the log level is
	// enabled. It is useful to avoid unnecessary memory allocation when the
	// log level is disabled. will improve performance when the log message is
	// expensive to construct.
	Debugxt() func(args ...interface{})
	Info(args ...interface{})
	Infoxt() func(args ...interface{})
	Warn(args ...interface{})
	Warnxt() func(args ...interface{})
	Error(args ...interface{})
	Errorxt() func(args ...interface{})
	// Fatal logs a message at fatal level and exits the application os.Exit(1).
	Fatal(args ...interface{})
	// Panic logs a message at panic level and panics the application.
	Panic(args ...interface{})
	// Derivatively creates a new logger with context trace.
	// An TraceID should be assigned by function WithTrace() before calling this
	// method.
	// The TraceID will be logged in the log message.
	FromTrace(ctx context.Context) (LevelLogger, error)
	// SetLevel sets the log level.
	SetLevel(level Level)
}

LevelLogger represents a leveled logger.

func GetQuickLogger

func GetQuickLogger(name string) LevelLogger

GetQuickLogger returns a quick logger by name.

func NewSimpleLogger

func NewSimpleLogger(
	modulename string,
	timefmt string,
	writer LevelWriter,
) LevelLogger

NewSimpleLogger returns a new simple LevelLogger.

type LevelWriter

type LevelWriter interface {
	Write(level Level) RawWriter
	SetLevel(level Level)
	Clone() LevelWriter
}

LevelWriter provides a way to write log data with different levels.

func NewLevelWriter

func NewLevelWriter(
	dbg, ifo, warn, err, ftl, pnc RawWriter,
	defaultLevel Level,
) LevelWriter

NewLevelWriter creates a new LevelWriter with the given RawWriters.

type RawWriter

type RawWriter interface {
	// WriteItem writes a single record to the writer.
	//
	// Upon the function's return, an internal EOR flag is triggered.
	// This flag is used to separate log records, replacing the traditional line
	// break, thereby allowing multiple lines within a single record.
	//
	// In such as database operations, each record can be stored as a separate
	// row.
	WriteItem(*logMeta, func(w io.Writer)) error
}

RawWriter represents a writer that writes raw log data.

func NewSimpWriter

func NewSimpWriter(w io.Writer) RawWriter

NewSimpFileWriter creates a new file writer with simple implementation.

type TraceID

type TraceID [32]byte

TraceID represents a trace ID.

func (TraceID) Hex

func (t TraceID) Hex() string

Hex returns the hex string of the trace ID.

func (TraceID) String

func (t TraceID) String() string

String returns the string representation of the trace ID.

func (TraceID) Sum

func (t TraceID) Sum() string

Sum returns the first 8 bytes summary of the trace ID.

type TraceScope

type TraceScope []TraceValue

TraceScope represents a trace chain. which includes a sequence of trace values. the last trace value is the current trace value, and the previous trace values are the parent traces.

func GetTrace

func GetTrace(ctx context.Context) TraceScope

GetTrace returns the trace value from the context.

func (TraceScope) Format

func (t TraceScope) Format(prefix, suffix, sep string) string

Format returns the formatted string representation of the trace chain.

func (TraceScope) String

func (t TraceScope) String() string

String returns the string representation of the trace chain.

type TraceValue

type TraceValue struct {
	ID   TraceID
	Name string
}

TraceValue represents a trace value.

Jump to

Keyboard shortcuts

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