Documentation ¶
Overview ¶
Package logfmt provides some helper functions to write logfmt messages. See https://brandur.org/logfmt for a description of the logfmt format.
Example ¶
package main import ( "fmt" "time" "github.com/spkg/slog/logfmt" ) func main() { // 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"
Index ¶
- Variables
- type Buffer
- func (b *Buffer) Len() int
- func (b *Buffer) Reset()
- func (b *Buffer) String() string
- func (b *Buffer) WriteEOL() error
- func (b *Buffer) WriteKey(key string) error
- func (b *Buffer) WriteNewLine() error
- func (b *Buffer) WriteProperty(key string, value interface{}) error
- func (b *Buffer) WriteTimestamp(t time.Time) error
- func (b *Buffer) WriteTo(w io.Writer) (int64, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
// TimeFormat is the time format used for timestamps.
TimeFormat = "2006-01-02T15:04:05.000000-0700"
)
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is used for building up logfmt messages. Once the message is built, the text can be obtained by the String method, or the message can be written to an io.Writer using the WriteTo method. The calling program should call the Reset method after it is finished with the buffer because then internal buffers can be re-used to take pressure off the garbage collector.
func (*Buffer) Reset ¶
func (b *Buffer) Reset()
Reset releases internal memory used by the buffer and makes that memory available for reuse by other Buffers. Calling Reset in programs that log many messages will reduce the pressure on the garbage collector.
func (*Buffer) String ¶
String returns a string representation of the message in the buffer. It implements the fmt.Stringer interface.
func (*Buffer) WriteEOL ¶
WriteEOL writes the OS-specific new line bytes to the buffer. On Windows the new line is 0xd, 0xa. For all other operating systems the new line is 0x0a.
func (*Buffer) WriteKey ¶
WriteKey writes a single key without a value to the buffer. The key should not contain any special characters.
func (*Buffer) WriteNewLine ¶
WriteNewLine writes a single new line byte to the buffer.
func (*Buffer) WriteProperty ¶
WriteProperty writes a key value pair to the buffer. If the value contains any special characters it will be quoted.
func (*Buffer) WriteTimestamp ¶
WriteTimestamp writes a timestamp to the buffer. The format of the timestamp is determined by the TimestampFormat variable.