isle/go/cmd/entrypoint/logger.go

57 lines
921 B
Go

package main
import (
"fmt"
"os"
"path"
"sync"
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
)
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
}