Categorygithub.com/tmbrwn/logging
2.0.1
Repository: https://github.com/tmbrwn/logging.git
Documentation: pkg.go.dev

# README

RETRACTED VERSION

This version of the library, as well as others specified in go.mod has been retracted due to versioning mistakes.

logging

A simple structured-logging library with a chaining API

example

The package attempts to provide a logging solution with the following properties:

  • Simplest possible API
  • Toggleable structured/pretty logging for production/dev work respectively
  • Debug-level logging toggleable at an arbitarily fine-grained level

This package takes inspiration from several sources, including other logging packages like zerolog, and from Dave Cheney's discussion of logging on his blog.

Anyone is welcome to use this package, but it is primarily designed for my own use, so I can't promise that any Issues will be resolved or PRs merged.

Getting started

go get github.com/tmbrwn/logging

Using the package

Using the package starts with creating a logging.Logger. Loggers can be shared between as many or as few parts of an application as desired, and Debug level logging can be configured wherever a Logger is kept.

A package-level default logger has been defined along with package-level logging functions for convenience.

Global settings among Loggers have the following defaults:

var Output io.Writer = os.Stdout     // Output to use for logging
var Pretty = false                   // Pretty print logs
var Clock = clockwork.NewRealClock() // Clock for timestamps
var DateTimeFormat = time.RFC3339    // Date format for timestamps

These parameters should be adjusted once on initialization. They have no thread-safe wrappers for adjustment mid-execution.

Examples

package main

import (
	"errors"

	"github.com/tmbrwn/logging"
)

func main() {
	logging.DateTimeFormat = "3:04pm"

	log := logging.Logger{
		EnableDebug: true,
	}

	log.Print("starting up")
	// {"time":"2:22pm","message":"attempting request","caller":"main.go:20"}

	l := log.Tag("request-id", "abc123").Logger()

	l.Debug("received request")
	// {"time":"2:22pm","message":"received request","caller":"main.go:17","request-id":"abc123",}

	err := errors.New("file does not exist")
	l.Err(err).Print("could not read file")
	// {"time":"2:22pm","message":"could not read file","caller":"main.go:26","request-id":"abc123","error":"file does not exist"}
}

API

The following methods can be used at the package level or on an individual Logger value.

MethodUsage
Print/PrintfMessages that a user should be concerned with
Debug/DebugfMessages that are too verbose or too numerous to be useful unless debugging
TagGive a key-value tag to a message
ErrShort for Tag("error", err)
LoggerSave the tags defined so far in a separate logger value

# Packages

No description provided by the author