-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLTFRS-15345 use structured log (#10)
* refact: use slog * refact: removing custom logger
- Loading branch information
1 parent
c48c8d7
commit 0f8564e
Showing
4 changed files
with
144 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// https://github.com/jba/slog/blob/main/handlers/loghandler/log_handler.go | ||
// loghandler provides a slog.Handler whose output resembles that of [log.Logger]. | ||
package airvantage | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"runtime" | ||
"strconv" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type SimpleLogHandler struct { | ||
opts slog.HandlerOptions | ||
prefix string // preformatted group names followed by a dot | ||
preformat string // preformatted Attrs, with an initial space | ||
|
||
mu sync.Mutex | ||
w io.Writer | ||
} | ||
|
||
func NewSimpleLogHandler(w io.Writer, opts *slog.HandlerOptions) *SimpleLogHandler { | ||
h := &SimpleLogHandler{w: w} | ||
if opts != nil { | ||
h.opts = *opts | ||
} | ||
if h.opts.ReplaceAttr == nil { | ||
h.opts.ReplaceAttr = func(_ []string, a slog.Attr) slog.Attr { return a } | ||
} | ||
return h | ||
} | ||
|
||
func (h *SimpleLogHandler) Enabled(ctx context.Context, level slog.Level) bool { | ||
minLevel := slog.LevelInfo | ||
if h.opts.Level != nil { | ||
minLevel = h.opts.Level.Level() | ||
} | ||
return level >= minLevel | ||
} | ||
|
||
func (h *SimpleLogHandler) WithGroup(name string) slog.Handler { | ||
return &SimpleLogHandler{ | ||
w: h.w, | ||
opts: h.opts, | ||
preformat: h.preformat, | ||
prefix: h.prefix + name + ".", | ||
} | ||
} | ||
|
||
func (h *SimpleLogHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
var buf []byte | ||
for _, a := range attrs { | ||
buf = h.appendAttr(buf, h.prefix, a) | ||
} | ||
return &SimpleLogHandler{ | ||
w: h.w, | ||
opts: h.opts, | ||
prefix: h.prefix, | ||
preformat: h.preformat + string(buf), | ||
} | ||
} | ||
|
||
func (h *SimpleLogHandler) Handle(ctx context.Context, r slog.Record) error { | ||
var buf []byte | ||
if !r.Time.IsZero() { | ||
buf = r.Time.AppendFormat(buf, time.RFC3339) | ||
buf = append(buf, ' ') | ||
} | ||
buf = append(buf, r.Level.String()...) | ||
buf = append(buf, ' ') | ||
if h.opts.AddSource && r.PC != 0 { | ||
fs := runtime.CallersFrames([]uintptr{r.PC}) | ||
f, _ := fs.Next() | ||
buf = append(buf, f.File...) | ||
buf = append(buf, ':') | ||
buf = strconv.AppendInt(buf, int64(f.Line), 10) | ||
buf = append(buf, ' ') | ||
} | ||
buf = append(buf, r.Message...) | ||
buf = append(buf, h.preformat...) | ||
r.Attrs(func(a slog.Attr) bool { | ||
buf = h.appendAttr(buf, h.prefix, a) | ||
return true | ||
}) | ||
buf = append(buf, '\n') | ||
h.mu.Lock() | ||
defer h.mu.Unlock() | ||
_, err := h.w.Write(buf) | ||
return err | ||
} | ||
|
||
func (h *SimpleLogHandler) appendAttr(buf []byte, prefix string, a slog.Attr) []byte { | ||
if a.Equal(slog.Attr{}) { | ||
return buf | ||
} | ||
if a.Value.Kind() != slog.KindGroup { | ||
buf = append(buf, ' ') | ||
buf = append(buf, prefix...) | ||
buf = append(buf, a.Key...) | ||
buf = append(buf, '=') | ||
return fmt.Appendf(buf, "%v", a.Value.Any()) | ||
} | ||
// Group | ||
if a.Key != "" { | ||
prefix += a.Key + "." | ||
} | ||
for _, a := range a.Value.Group() { | ||
buf = h.appendAttr(buf, prefix, a) | ||
} | ||
return buf | ||
} |
Oops, something went wrong.