Documentation ¶
Overview ¶
Package flash provides a feature-complete, performant slog.Handler implementation.
This handler started as a clone of the sloggy handler and was then tweaked for performance.
Extra Options ¶
When creating a flash handler via NewHandler basic options can be specified by the slog.HandlerOptions options argument. In addition, the handler also supports optional flash.Extras options which can be used to adjust basic logging behavior slightly. This can be used to test the behavior of ReplaceAttr functionality or to match the behavior of another logging library.
Performance Edits ¶
After flash was cloned from sloggy it went through a number of performance-related edits.
Index ¶
Examples ¶
Constants ¶
const (
DefaultTimeFormat = time.RFC3339Nano
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Extras ¶
type Extras struct { // TimeFormat holds the format of the basic time field. // If not set defaults to the value of flash.DefaultTimeFormat (= time.RFC3339Nano). TimeFormat string // LevelNames holds a map from slog.Level to string. // If these fields are configured they replace the usual level names. // It is possible to configure only some of the level names. // Any level name that is not configured will be set to the appropriate // slog global constant (e.g. slog.LevelInfo.String()). LevelNames map[slog.Level]string // LevelKey specifies the JSON field name for the slog.Level for the log records. // If this field is not configured the value of slog.LevelKey is used. LevelKey string // MessageKey specifies the JSON field name for the log message. // If this field is not configured the value of slog.MessageKey is used. MessageKey string // SourceKey specifies the JSON field name for source data. // If this field is not configured the value of slog.SourceKey is used. SourceKey string // TimeKey specifies the JSON field name for the time the record was logged. // If this field is not configured the value of slog.TimeKey is used. TimeKey string }
Extras defines extra options specific to a flash.Handler.
Using these options it is possible to override some of the log/slog "standard" behavior. This supports testing of slog.HandlerOptions.ReplaceAttr functions and may also be used to replicate non-standard behavior in other handlers.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler provides a feature-complete, performant slog.Handler implementation.
Example ¶
var buff bytes.Buffer logger := slog.New(NewHandler(&buff, nil, nil)) logger.Info("hello", "count", math.Pi) var logMap map[string]any _ = json.Unmarshal(buff.Bytes(), &logMap) fmt.Printf("%s %6.5f\n", logMap["msg"], logMap["count"].(float64))
Output: hello 3.14159
func NewHandler ¶
NewHandler returns a new sloggy handler with the specified output writer and slog.HandlerOptions. If the options argument is nil it will be set to a level of slog.LevelInfo and nothing else. If the extras argument is nil it will be set to defaults that match slog.JSONHandler.