KubFly SDK for Go / slog
package slog
import "kubfly.io/sdk/slog"
Package slog connects KubFly with log/slog of the standard library in both directions, without a loop — the kubfly-sdk-slf4j module of the Java SDK:
- KubFly → slog. Collector prints the events of KubFly, those of the SDK too, through the default handler of slog (slog.Default): linking the package in registers it, as Collectors.Register does for every collector.
- slog → KubFly. Handler turns the records of slog into events of KubFly, in the frame of their context; with slog.SetDefault(slog.New(kubflyslog.NewHandler())) the package log of the standard library goes the same way.
The records of the bridge carry the path of their event as the attribute logger (LoggerKey), with a /; Handler skips such records and Collector skips the events Handler made (the module slog of the paths of the SDK), so a line is printed once and nothing loops.
import kubflyslog "kubfly.io/sdk/slog" slog.SetDefault(slog.New(kubflyslog.NewHandler())) // slog and log → KubFly
Constants
const ErrorKey = "error"
ErrorKey is the attribute of the error of an event in a record of the bridge.
const Imported = "slog"
Imported is the module of the events imported from slog, sdk/slog/<package>/<function> — a module of an integration of Go alone, as sdk/slf4j is of SLF4J in Java: the bridge skips them.
const LevelTrace = stdslog.LevelDebug - 4
LevelTrace is the level of slog of the events of TRACE: below slog.LevelDebug, as TRACE is below DEBUG.
const LoggerKey = "logger"
LoggerKey is the attribute of a record of the bridge that names the path of its event, project/module/area/system: slog has no names of loggers.
Types
type Collector
type Collector struct {
// contains filtered or unexported fields
}
Collector is the bridge to slog: an event is formatted — with its own tags and the frame of its context appended — and handed to the default handler of slog with the attribute logger (LoggerKey) naming its path, its own tags as attributes in the order they were added and its error as the attribute error (ErrorKey); FATAL goes to slog.LevelError, TRACE to LevelTrace. Thresholds follow the default handler at the time of Collectors.Refresh — slog has one level for all paths. Alerts are written only when the state of a metric changes, with the path of its project/module/area/system: CRITICAL at slog.LevelError, WARNING at slog.LevelWarn, SILENCE as heavy as alert.Alert.Silence, a return to OK at slog.LevelInfo; health checks likewise, with their message and error.
It is on, and a bridge — the first collector — only when the default handler of slog is not a Handler of KubFly: otherwise there is nobody to print to.
func NewCollector
func NewCollector() *Collector
NewCollector is the bridge to the default handler of slog, on if that handler is not a Handler of KubFly.
func (*Collector) Alert
func (c *Collector) Alert(id int64, begin int64, end int64, metric *api.Metric, severity alert.Severity, warnings int64, criticals int64, unassessed int64)
Alert writes the state of a metric when it changes (the first OK is not a change), with the path of its project/module/area/system.
func (*Collector) Begin
func (c *Collector) Begin(id int64, time int64, frame *api.Frame)
Begin writes the beginning of a frame at LevelTrace.
func (*Collector) Bridge
func (c *Collector) Bridge() bool
Bridge tells whether it is a bridge: while it works.
func (*Collector) Enabled
func (c *Collector) Enabled() bool
Enabled tells whether the bridge works: only with a default handler of slog other than KubFly.
func (*Collector) End
func (c *Collector) End(id int64, time int64, duration int64, threadID int64, threadName string, frame *api.Frame, err error)
End writes the end of a frame at LevelTrace: its duration, and the error of a failure.
func (*Collector) Event
func (c *Collector) Event(id int64, time int64, threadID int64, threadName string, frame *api.Frame, event *api.Event, message string, arg1 api.Arg, arg2 api.Arg, err error)
Event writes an event: the message formatted, with its own tags and the frame of its context appended. The events imported from slog are not written back.
func (*Collector) Finish
func (c *Collector) Finish(id int64, time int64, environment *environment.Environment)
Finish writes the finish of the process as a line of slog.LevelInfo of the path sdk/api/environment, with the largest number given.
func (*Collector) Frames
func (c *Collector) Frames(project, module, area, system, component string) bool
Frames tells whether the frames of a scope are written: at LevelTrace, when the default handler takes it.
func (*Collector) Health
func (c *Collector) Health(id int64, time int64, duration int64, check *api.HealthCheck, health api.Health)
Health writes a health check when its state changes, with the path of its project/module/area/system, its message and its error.
func (*Collector) Metrics
func (c *Collector) Metrics(id int64, begin int64, end int64, snapshots []*api.Snapshot)
Metrics are not written: slog has no metrics.
func (*Collector) Profile
func (c *Collector) Profile(id int64, begin int64, end int64, profile *profile.Profile)
Profile is not written: the profiles are not events of the application.
func (*Collector) Start
func (c *Collector) Start(id int64, time int64, environment *environment.Environment)
Start writes the start of the process as a line of slog.LevelInfo of the path sdk/api/environment: where and on what the application runs belongs to ordinary logs as well.
func (*Collector) Threshold
func (c *Collector) Threshold(project, module, area, system string) api.Level
Threshold is the most verbose level the default handler of slog takes; the events imported from slog — none.
func (*Collector) Traffic
func (c *Collector) Traffic(id int64, begin int64, end int64, edges []traffic.Edge)
Traffic is not written: the links are not events of the application.
func (*Collector) Usage
func (c *Collector) Usage(id int64, time int64, usage *usage.Usage)
Usage is not written: slog has no place for usage snapshots.
type Handler
type Handler struct {
// contains filtered or unexported fields
}
Handler is a handler of slog that writes its records as events of KubFly — KubFlyAppender of the Java SDK — in the frame of the context of the record:
- The path is sdk/slog/<package>/<function>, from the function that logged (Record.PC): the record of kubfly.io/shop/orders.(*Shipping).Ship is the event of sdk/slog/kubfly.io.shop.orders/Shipping — the import path with dots for its slashes, as the package of a logger of Java, and the type of the method or the function. A record without a place is of sdk/slog/root/root.
- The message is the template of the event as it is, without arguments: slog formats nothing.
- The attributes — of the record and of WithAttrs, under the names of WithGroup joined with dots — are the tags of the event; the first attribute whose value is an error is the error of the event.
- slog.LevelError and above is ERROR, slog.LevelWarn — WARN, slog.LevelInfo — INFO, slog.LevelDebug — DEBUG, lower — TRACE.
A record of the bridge — its attribute logger (LoggerKey) names a path, with a / — is not written: the event came from KubFly. Safe for concurrent use; the zero value is not usable — NewHandler.
func NewHandler
func NewHandler() *Handler
NewHandler is a handler that writes the records of slog as events of KubFly.
func (*Handler) Enabled
func (h *Handler) Enabled(ctx context.Context, level stdslog.Level) bool
Enabled lets every level through: the collectors decide by the path, which is known only from the record.
func (*Handler) Handle
func (h *Handler) Handle(ctx context.Context, record stdslog.Record) error
Handle writes a record as an event of KubFly, unless the record came from the bridge.
func (*Handler) WithAttrs
func (h *Handler) WithAttrs(attrs []stdslog.Attr) stdslog.Handler
WithAttrs is the handler with attributes added to every record, under the names of its groups.
func (*Handler) WithGroup
func (h *Handler) WithGroup(name string) stdslog.Handler
WithGroup is the handler whose attributes that follow are under one more name of a group.