log

package
v0.0.0-...-fdaf231 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2020 License: MIT Imports: 8 Imported by: 0

README

log

This extremely simple log library is intended for use with micro service applications, which have only two logging targets: the console while developing the application, and a log aggregation service in production.

This library was inspired by:

Features

  • The fmt library is not used to minimize memory allocations.
  • Logs to stdout by default.
  • Can be configured to send messages to a UDP log aggregator.

Usage

By default, log will emit messages on os.Stderr which is supposed to be an unbuffered stream. The destination can be changed with:

log.SetOutput(os.Stdout)

To prevent console logging from being visible at all, change the destination to /dev/null:

devnull, err := os.OpenFile(os.DevNull, O_WRONLY, 0666)
if err != null {
	panic(err)
}
defer devnull.Close()
log.SetOutput(devnull)

To send output to a UDP log aggregator, just set the address and port of the service as follows:

log.SetServer("10.10.10.10:8080")

Now every log message is formatted in JSON and will be sent to the aggregator:

log.Info("The quick brown fox")
// Output: {"time":1554370662469959000,"name":"main","level":"INFO","message":"The quick brown fox"} 

Documentation

Index

Constants

View Source
const (
	ISO8601Micro = "2006-01-02T15:04:05.000000Z0700"
)

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, fields ...Field)

Debug emits a message with the DEBUG level.

func Error

func Error(msg string, fields ...Field)

Error emits a message with the ERROR level.

func Info

func Info(msg string, fields ...Field)

Info emits a message with the INFO level.

func SetLevel

func SetLevel(l Level)

SetLevel changes the minimum level that is emitted. This is used to prevent logging from becoming too noisy. By default the minimum level is set to INFO so that DEBUG messages don't overwhelm the aggregator.

func SetServer

func SetServer(address string)

SetServer starts a UDP client that sends messages to a log aggregation server. This occurs in parallel with console logging.

func SetWriter

func SetWriter(w io.Writer)

SetWriter sets the output for all future log messages not bound for an aggregator.

func Warn

func Warn(msg string, fields ...Field)

Warn emits a message with the WARN level.

Types

type Field

type Field struct {
	Name        string
	Type        reflect.Kind
	BoolValue   bool
	IntValue    int64
	StringValue string
}

Field stores a name/value pair to be formatted by the emitter.

func Bool

func Bool(name string, value bool) Field

Bool returns a Field that contains a boolean value.

func Err

func Err(value error) Field

Err returns a Field that contains the message from an error.

func Int

func Int(name string, value int) Field

Int returns a Field that contains a default integer.

func Int8

func Int8(name string, value int8) Field

Int8 returns a Field that contains an 8-bit integer

func Int16

func Int16(name string, value int16) Field

Int16 returns a Field that contains an 16-bit integer

func Int32

func Int32(name string, value int32) Field

Int32 returns a Field that contains an 32-bit integer

func Int64

func Int64(name string, value int64) Field

Int64 returns a Field that contains an 64-bit integer

func String

func String(name string, value string) Field

String returns a Field that contains a string.

func (Field) Json

func (field Field) Json() string

Json returns the contents of the Field as `"key":value`.

func (Field) String

func (field Field) String() string

String returns the contents of the Field as `key=value`.

type Level

type Level int

Type that defines the logging level

const (
	DEBUG Level = iota
	INFO  Level = iota
	WARN  Level = iota
	ERROR Level = iota
)

These are the supported log levels.

func Parse

func Parse(s string) Level

func (Level) String

func (l Level) String() string

String converts a log level to its string representation

Jump to

Keyboard shortcuts

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