2022-11-13 15:45:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
|
2024-06-22 15:49:56 +00:00
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2022-11-13 15:45:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type logMsgHandler struct {
|
|
|
|
stderr *os.File
|
|
|
|
l sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLogMsgHandler() mlog.MessageHandler {
|
|
|
|
return &logMsgHandler{
|
|
|
|
stderr: os.Stderr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *logMsgHandler) Sync() error {
|
|
|
|
return h.stderr.Sync()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *logMsgHandler) Handle(msg mlog.FullMessage) error {
|
|
|
|
h.l.Lock()
|
|
|
|
defer h.l.Unlock()
|
|
|
|
|
|
|
|
var namespaceStr string
|
|
|
|
|
|
|
|
if len(msg.Namespace) > 0 {
|
|
|
|
namespaceStr = "[" + path.Join(msg.Namespace...) + "] "
|
|
|
|
}
|
|
|
|
|
|
|
|
var annotationsStr string
|
|
|
|
|
|
|
|
if m := mctx.EvaluateAnnotations(msg.Context, nil).StringMap(); len(m) > 0 {
|
|
|
|
|
|
|
|
for k, v := range m {
|
|
|
|
annotationsStr += fmt.Sprintf(" %q=%q", k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(
|
|
|
|
h.stderr, "%s %s%s%s\n",
|
|
|
|
msg.Level.String(),
|
|
|
|
namespaceStr,
|
|
|
|
msg.Description,
|
|
|
|
annotationsStr,
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|