Skip to content

inkel/logfmt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logfmt - An opinionated logging library in logfmt for Go

Go Reference

How opinionated?

  • The only possible output format is logfmt.
  • All string values are quoted.
  • Label keys are sorted except ts for timestamp and msg for the message are sorted.
  • time.Time values are in UTC and formatted using time.RFC3339.

Another strong opinion is that it simply writes to an io.Writer. You get to choose if this writer is buffered, a file, a network connection, a terminal. It's not something the package should worry about.

Usage

package main

import (
	"os"
	"time"

	"github.com/inkel/logfmt"
)

func main() {
	l := logfmt.NewLogger(os.Stdout)

	l.Log("a simple log message without any labels", nil)
	l.Logf("a formatted log %s without any labels", nil, "message")

	l.Log("message with a few labels of different types", logfmt.Labels{
		"foo": "bar",
		"err": os.ErrPermission,
		"now": time.Now(),
	})
}

This will produce an output like the following:

ts=2022-07-13T12:31:41Z msg="a simple log message without any labels"
ts=2022-07-13T12:31:41Z msg="a formatted log message without any labels"
ts=2022-07-13T12:31:41Z msg="message with a few labels of different types" err="permission denied" foo="bar" now=2022-07-13T12:31:41Z

License

MIT. See LICENSE.

About

Small and opinionated logging package

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages