Documentation
¶
Overview ¶
Package structured provides a Transport that always outputs a single JSON object per log entry with msg, level, and time fields by default.
See https://go.loglayer.dev for usage guides and the full API reference.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
transport.BaseConfig
// MessageField is the key for the joined message text. Defaults to "msg".
MessageField string
// DateField is the key for the timestamp. Defaults to "time".
DateField string
// LevelField is the key for the log level. Defaults to "level".
LevelField string
// DateFn overrides the default ISO-8601 timestamp.
DateFn func() string
// LevelFn overrides the default level string.
LevelFn func(loglayer.LogLevel) string
// MessageFn formats the message portion. Its return value is used as the message text.
MessageFn func(params loglayer.TransportParams) string
// Writer overrides the default output (os.Stdout).
Writer io.Writer
}
Config holds configuration options for Transport.
type Transport ¶
type Transport struct {
transport.BaseTransport
// contains filtered or unexported fields
}
Transport always outputs one JSON object per log entry. All messages are joined with a space and placed under MessageField.
func New ¶
New creates a Transport from the given Config.
Example ¶
New builds a structured-JSON transport. DateFn returns a fixed string here so the example output is deterministic.
t := structured.New(structured.Config{
DateFn: func() string { return "2026-04-26T12:00:00Z" },
})
log := loglayer.New(loglayer.Config{
Transport: t,
DisableFatalExit: true,
})
log.WithMetadata(loglayer.Metadata{"durationMs": 42}).Info("served")
Output: {"level":"info","time":"2026-04-26T12:00:00Z","msg":"served","durationMs":42}
func (*Transport) GetLoggerInstance ¶
GetLoggerInstance returns nil; structured transport has no underlying logger library.
func (*Transport) SendToLogger ¶
func (s *Transport) SendToLogger(params loglayer.TransportParams)
SendToLogger writes one JSON object per entry: the configured level, date, and message fields first (in that order), followed by Data and Metadata entries in iteration order. Per-value marshaling uses goccy/go-json so structs, slices, and json.Marshaler types render exactly as encoding/json would.