logfmt

package
v0.0.0-...-4d8b9e4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2017 License: MIT Imports: 7 Imported by: 0

README

logfmt

GoDoc License

Package logfmt provides some helper functions to write logfmt messages. See https://brandur.org/logfmt for a description of the logfmt format.

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

Examples

Constants

This section is empty.

Variables

View Source
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) Len

func (b *Buffer) Len() int

Len returns the length of the message stored in the buffer.

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

func (b *Buffer) String() string

String returns a string representation of the message in the buffer. It implements the fmt.Stringer interface.

func (*Buffer) WriteEOL

func (b *Buffer) WriteEOL() error

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

func (b *Buffer) WriteKey(key string) error

WriteKey writes a single key without a value to the buffer. The key should not contain any special characters.

func (*Buffer) WriteNewLine

func (b *Buffer) WriteNewLine() error

WriteNewLine writes a single new line byte to the buffer.

func (*Buffer) WriteProperty

func (b *Buffer) WriteProperty(key string, value interface{}) error

WriteProperty writes a key value pair to the buffer. If the value contains any special characters it will be quoted.

func (*Buffer) WriteTimestamp

func (b *Buffer) WriteTimestamp(t time.Time) error

WriteTimestamp writes a timestamp to the buffer. The format of the timestamp is determined by the TimestampFormat variable.

func (*Buffer) WriteTo

func (b *Buffer) WriteTo(w io.Writer) (int64, error)

WriteTo implements the io.WriterTo interface. It writes the formatted message to the writer w.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL