You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.0 KiB
Go

5 years ago
package blammo
5 years ago
import (
"bytes"
"io"
"strconv"
"strings"
"sync"
"time"
)
5 years ago
// LineWriter is an EventWriter that writes one event per line. Event that have
// multiline messages will have the newlines in their messages replaced with \n
// sequences. This is the default EventWriter and is designed to be reasonably
// safe and practical for most users.
5 years ago
type LineWriter struct {
5 years ago
pool sync.Pool
out struct {
sync.Mutex
io.Writer
}
}
func NewLineWriter(w io.Writer) *LineWriter {
lw := new(LineWriter)
lw.pool = sync.Pool{
New: func() interface{} { return new(bytes.Buffer) },
}
lw.out.Writer = w
return lw
}
func (l *LineWriter) WriteEvent(e *Event) {
buf := l.pool.Get().(*bytes.Buffer)
buf.Reset()
buf.WriteString(e.Time.Format(time.RFC3339))
buf.WriteRune(' ')
switch e.Level {
case Debug:
buf.WriteString("d ")
case Info:
buf.WriteString("i ")
case Error:
buf.WriteString("e ")
default:
buf.WriteString("? ")
}
buf.WriteRune('[')
5 years ago
if e.Path != nil {
buf.WriteString(e.Path.String())
}
5 years ago
buf.WriteRune(']')
buf.WriteRune(' ')
if e.Tags == nil {
5 years ago
buf.WriteString("[]")
5 years ago
} else {
buf.WriteRune('[')
writeTags(buf, e.Tags)
5 years ago
buf.WriteRune(']')
5 years ago
}
5 years ago
if e.Text != "" {
buf.WriteRune(' ')
buf.WriteString(strings.ReplaceAll(e.Text, string('\n'), "\n"))
buf.WriteRune('\n')
5 years ago
}
5 years ago
l.out.Lock()
l.out.Write(buf.Bytes())
l.out.Unlock()
l.pool.Put(buf)
}
5 years ago
// writeTags is a helper for LineWriter's WriteEvent method. writeTags writes
// tags in the order in which they were added to the list of tags; the oldest
// tag is always written first.
5 years ago
func writeTags(buf *bytes.Buffer, tags *Tags) {
if tags.parent != nil {
writeTags(buf, tags.parent)
buf.WriteRune('+')
}
buf.WriteString(tags.key)
if tags.value != nil {
buf.WriteRune('=')
switch v := tags.value.(type) {
case int:
buf.WriteString(strconv.Itoa(v))
case string:
buf.WriteString(MakeSafeName(v))
case float64:
buf.WriteString(strconv.FormatFloat(v, 'f', -1, 64))
default:
buf.WriteString("???")
}
}
5 years ago
}