57 lines
925 B
Go
57 lines
925 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"sync"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/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
|
|
}
|