Beispiel #1
0
func (m *Message) logfmtBuffer() logfmt.Buffer {
	var buf logfmt.Buffer

	buf.WriteTimestamp(m.Timestamp)
	buf.WriteKey(m.Level.String())
	buf.WriteProperty("msg", m.Text)
	if m.Err != nil {
		buf.WriteProperty("error", m.Err.Error())
	}

	for _, p := range m.Properties {
		buf.WriteProperty(p.Key, p.Value)
	}

	for _, p := range m.Context {
		buf.WriteProperty(p.Key, p.Value)
	}

	if m.code != "" {
		buf.WriteProperty("code", m.code)
	}

	if m.status != 0 {
		buf.WriteProperty("status", m.status)
	}

	return buf
}
Beispiel #2
0
func Example() {
	// create a buffer and ensure that its internal memory buffer is
	// available for re-use when no longer needed
	buf := logfmt.Buffer{}
	defer buf.Reset()

	buf.WriteTimestamp(time.Unix(1234567890, 987654321).UTC())
	buf.WriteKey("info")
	buf.WriteProperty("key1", 1)
	buf.WriteProperty("key2", "value 2")

	fmt.Println(buf.String())
	// Output: 2009-02-13T23:31:30.987654+0000 info key1=1 key2="value 2"
}