// Package processlog builds the logger for the process's own messages, // which it writes as JSON lines marked "type":"process". The request log // is package requestlog's. package processlog import ( "io" "log/slog" "sneak.berlin/go/smallwebwaf/internal/requestlog" ) // Params are what New needs. type Params struct { // Output is where the lines are written. Output io.Writer // InstanceName, SWW_WAF_INSTANCE_NAME, is each line's instance. InstanceName string // Level, SWW_WAF_LOG_LEVEL, is the least severe level written. Level slog.Level } // New returns the logger for the process's own messages: JSON lines on // params.Output, marked "type":"process", with the time in the same form // as a request line's. It writes only the messages at params.Level or more // severe; the request lines requestlog.Write writes are never held back. func New(params Params) *slog.Logger { handler := slog.NewJSONHandler(params.Output, &slog.HandlerOptions{ Level: params.Level, ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr { if attr.Key == slog.TimeKey && len(groups) == 0 { return slog.String( slog.TimeKey, requestlog.FormatTime(attr.Value.Time()), ) } return attr }, }) return slog.New(handler). With("type", "process", "instance", params.InstanceName) }