aboutsummaryrefslogtreecommitdiff
path: root/writers/kv/key_value.go
blob: b4434beaed47be7f9d7637db10ac89e86cb79ec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package kv

import (
	"fmt"
	"io"
	"os"
	"strings"

	"src.userspace.com.au/logger/message"
)

// Writer implementation.
type Writer struct {
	timeFormat string
	writer     io.Writer
}

// New creates a new Writer writer.
func New(opts ...Option) (*Writer, error) {
	w := &Writer{
		timeFormat: "2006-01-02T15:04:05.000Z0700",
		writer:     os.Stdout,
	}

	for _, opt := range opts {
		if err := opt(w); err != nil {
			return nil, err
		}
	}
	return w, nil
}

// Write implements the message.Writer interface.
func (w Writer) Write(m message.Message) {
	fmt.Fprintf(w.writer, "%s ", m.Time.Format(w.timeFormat))
	if m.Name != "" {
		fmt.Fprintf(w.writer, "%s: ", m.Name)
	}

	// Write message content first
	w.writer.Write([]byte(m.Content))

	for k, v := range m.Fields {
		writeKV(w.writer, k, v)
	}
	w.writer.Write([]byte{'\n'})
}

func writeKV(w io.Writer, k, v string) (int, error) {
	return fmt.Fprintf(w, " %s=%s", maybeQuote(k), maybeQuote(v))
}

func maybeQuote(s string) string {
	if strings.ContainsAny(s, " \t\n\r") {
		return fmt.Sprintf("%q", s)
	}
	return s
}