logger

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterSink

func RegisterSink(l Logger, s Sink)

RegisterSink adds a sink at runtime.

Types

type CLEFSink

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

CLEFSink writes log entries as CLEF-compatible JSON lines. Each line is a single JSON object with @t, @l (omitted for Information), @m, and all structured fields flattened at the root level, matching the format emitted by Serilog's RenderedCompactJsonFormatter, so that Go and C# service logs are queryable with the same field selectors in Grafana/Loki.

func NewCLEFSink

func NewCLEFSink(w io.Writer) *CLEFSink

NewCLEFSink constructs a CLEFSink. When w is nil, os.Stdout is used.

func (*CLEFSink) Log

func (c *CLEFSink) Log(e Entry) error

Log writes e as a CLEF JSON line to the underlying writer.

type ConsoleSink

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

ConsoleSink writes entries as compact JSON lines to an io.Writer.

func NewConsoleSink

func NewConsoleSink(w io.Writer) *ConsoleSink

NewConsoleSink constructs a ConsoleSink.

func (*ConsoleSink) Log

func (c *ConsoleSink) Log(e Entry) error

Log writes a structured entry to the underlying writer.

type Entry

type Entry struct {
	Level   string                 `json:"level"`
	Time    time.Time              `json:"time"`
	Msg     string                 `json:"msg"`
	Fields  map[string]interface{} `json:"fields,omitempty"`
	TraceID string                 `json:"trace_id,omitempty"`
	SpanID  string                 `json:"span_id,omitempty"`
}

Entry is the log payload passed to sinks.

type Field

type Field struct {
	Key   string      `json:"key"`
	Value interface{} `json:"value"`
}

Field is a single structured key/value pair.

type Level

type Level int

Level represents log severity.

const (
	// DebugLevel logs detailed diagnostic information.
	DebugLevel Level = iota
	// InfoLevel logs general operational messages.
	InfoLevel
	// WarnLevel logs potential problems.
	WarnLevel
	// ErrorLevel logs error conditions.
	ErrorLevel
	// FatalLevel logs severe errors that should terminate the process.
	FatalLevel
)

Log levels in ascending order of severity.

func (Level) String

func (l Level) String() string

String returns the textual representation of the log level.

type Logger

type Logger interface {
	With(fields ...Field) Logger
	Debug(msg string, fields ...Field)
	Info(msg string, fields ...Field)
	Warn(msg string, fields ...Field)
	Error(msg string, fields ...Field)
	RegisterSink(s Sink)
	SetLevel(l Level)
}

Logger is the public logging contract.

func New

func New(opts ...Option) Logger

New constructs a logger with optional options.

Example:

log := logger.New(
	logger.WithLevel(logger.DebugLevel),
	logger.WithoutDefaultSink(),
	logger.WithSink(mySink),
)
log.Info("started", logger.Field{Key: "version", Value: "1.0"})

type Option

type Option func(*stdLogger)

Option configures the concrete logger on creation.

func WithAsync

func WithAsync(bufSize int) Option

WithAsync enables asynchronous logging with a buffered channel of the specified size.

func WithContext

func WithContext(ctx context.Context) Option

WithContext binds a context to the logger, allowing sinks to extract trace/span IDs for distributed tracing.

func WithFields

func WithFields(fields ...Field) Option

WithFields binds fields to the logger returned from New.

func WithLevel

func WithLevel(level Level) Option

WithLevel sets the minimum level for emitted logs.

func WithSink

func WithSink(s Sink) Option

WithSink adds an initial sink.

func WithoutDefaultSink

func WithoutDefaultSink() Option

WithoutDefaultSink removes the default ConsoleSink added by New. Use before WithSink to create a logger with only custom sinks:

logger.New(logger.WithoutDefaultSink(), logger.WithSink(clefSink))

type Sink

type Sink interface {
	Log(e Entry) error
}

Sink receives log entries for processing.

Jump to

Keyboard shortcuts

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