log

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2020 License: MIT Imports: 8 Imported by: 25

README

log GoDoc Build Coverage Status GoReport

It is a simple structured logging package for Go.

Features

  • easy, configurable, and pretty logging for development
  • built-in some handlers
  • allow to use different level for each handler
  • goroutine safety (thread-safe)
  • allow to add default fields to every log. ( ex. You maybe want to add app_id per each app or env per each environment)
  • colored text for console handler (linux, mac, and windows are supported)
  • context.Context integration

Handlers

  • console
  • gelf (graylog)
  • memory (unit test)
  • discard (benchmark)

Installation

Use go get

go get -u github.com/jasonsoft/log

Example

package main

import (
	"errors"

	"github.com/jasonsoft/log"
	"github.com/jasonsoft/log/handlers/console"
)

func main() {
	// use console handler to log all level logs
	clog := console.New()
	log.RegisterHandler(clog, log.AllLevels...)

	// optional: allow handlers to clear all buffer
	defer log.Flush()

	// use withDefaultFields to add fields to every logs
	log.WithDefaultFields(
		log.Fields{
			"app_id": "santa",
			"env":    "dev",
		},
	)

	// use trace to get how long it takes
	defer log.Trace("time to run").Stop()

	// print message use DEBUG level
	log.Debug("hello world")

	// log information with custom fileds
	log.Str("city", "keelung").Infof("more info")

	// log error struct and print error message
	err := errors.New("something bad happened")
	log.WithError(err).Error("oops...")
}

Output

Field Types

Standard Types
  • Str
  • Bool
  • Int, Int8, Int16, Int32, Int64
  • Uint, Uint8, Uint16, Uint32, Uint64
  • Float32, Float64

Benchmarks

Run on MacBook Pro 15-inch 2018 using go version go1.13.5 windows 10 OS

go test -bench=. -benchmem -run=^bb -v

goos: windows
goarch: amd64
pkg: github.com/jasonsoft/log
BenchmarkSmall-12       13483690                82.6 ns/op             0 B/op          0 allocs/op
BenchmarkMedium-12       2489635               605 ns/op             336 B/op          2 allocs/op
BenchmarkLarge-12         479955              2802 ns/op            2183 B/op          9 allocs/op
PASS
ok      github.com/jasonsoft/log        4.604s

Documentation

Index

Constants

This section is empty.

Variables

AllLevels is an array of all log levels, for easier registering of all levels to a handler

Functions

func Debug

func Debug(msg string)

Debug level formatted message.

func Debugf

func Debugf(msg string, v ...interface{})

Debugf level formatted message.

func Error

func Error(msg string)

Error level formatted message

func Errorf

func Errorf(msg string, v ...interface{})

Errorf level formatted message

func Fatal

func Fatal(msg string)

Fatal level formatted message, followed by an exit.

func Fatalf

func Fatalf(msg string, v ...interface{})

Fatalf level formatted message, followed by an exit.

func Flush added in v1.0.1

func Flush()

Flush clear all handler's buffer

func Info

func Info(msg string)

Info level formatted message.

func Infof

func Infof(msg string, v ...interface{})

Infof level formatted message.

func NewContext

func NewContext(ctx context.Context, e Entry) context.Context

NewContext return a new context with a logger value

func Panic

func Panic(msg string)

Panic level formatted message

func Panicf

func Panicf(msg string, v ...interface{})

Panicf level formatted message

func RegisterHandler

func RegisterHandler(handler Handler, levels ...Level)

RegisterHandler adds a new Log Handler and specifies what log levels the handler will be passed log entries for

func Warn

func Warn(msg string)

Warn level formatted message.

func Warnf

func Warnf(msg string, v ...interface{})

Warnf level formatted message.

func WithDefaultFields

func WithDefaultFields(fields Fields)

WithDefaultFields adds fields to every entry instance

Types

type Entry

type Entry struct {
	Level     Level     `json:"level"`
	Message   string    `json:"message"`
	Timestamp time.Time `json:"timestamp"`
	Fields    Fields    `json:"fields"` // single map; easy to use for handlers
	// contains filtered or unexported fields
}

Entry defines a single log entry

func Bool added in v1.0.4

func Bool(key string, val bool) Entry

Bool add bool field to current entry

func Float32 added in v1.0.4

func Float32(key string, val float32) Entry

Float32 add float32 field to current entry

func Float64 added in v1.0.4

func Float64(key string, val float64) Entry

Float64 add Float64 field to current entry

func FromContext

func FromContext(ctx context.Context) Entry

FromContext return a logger from the context

func Int added in v1.0.4

func Int(key string, val int) Entry

Int add Int field to current entry

func Int16 added in v1.0.4

func Int16(key string, val int16) Entry

Int16 add Int16 field to current entry

func Int32 added in v1.0.4

func Int32(key string, val int32) Entry

Int32 add Int32 field to current entry

func Int64 added in v1.0.4

func Int64(key string, val int64) Entry

Int64 add Int64 field to current entry

func Int8 added in v1.0.4

func Int8(key string, val int8) Entry

Int8 add Int8 field to current entry

func Str added in v1.0.4

func Str(key string, val string) Entry

Str add string field to current entry

func Trace

func Trace(msg string) Entry

Trace returns a new entry with a Stop method to fire off a corresponding completion log, useful with defer.

func Uint added in v1.0.4

func Uint(key string, val uint) Entry

Uint add Uint field to current entry

func Uint16 added in v1.0.4

func Uint16(key string, val uint16) Entry

Uint16 add Uint16 field to current entry

func Uint32 added in v1.0.4

func Uint32(key string, val uint32) Entry

Uint32 add Uint32 field to current entry

func Uint64 added in v1.0.4

func Uint64(key string, val uint64) Entry

Uint64 add Uint64 field to current entry

func Uint8 added in v1.0.4

func Uint8(key string, val uint8) Entry

Uint8 add Uint8 field to current entry

func WithError

func WithError(err error) Entry

WithError returns a new entry with the "error" set to `err`.

func WithField

func WithField(key string, value interface{}) Entry

WithField returns a new entry with the `key` and `value` set.

func WithFields

func WithFields(fields Fields) Entry

WithFields returns a log Entry with fields set

func (Entry) Bool added in v1.0.4

func (e Entry) Bool(key string, val bool) Entry

Bool add bool field to current entry

func (Entry) Debug

func (e Entry) Debug(msg string)

Debug level message.

func (Entry) Debugf

func (e Entry) Debugf(msg string, v ...interface{})

Debugf level message.

func (Entry) Error

func (e Entry) Error(msg string)

Error level message.

func (Entry) Errorf

func (e Entry) Errorf(msg string, v ...interface{})

Errorf level message.

func (Entry) Fatal

func (e Entry) Fatal(msg string)

Fatal level message.

func (Entry) Fatalf

func (e Entry) Fatalf(msg string, v ...interface{})

Fatalf level message.

func (Entry) Float32 added in v1.0.4

func (e Entry) Float32(key string, val float32) Entry

Float32 add Float32 field to current entry

func (Entry) Float64 added in v1.0.4

func (e Entry) Float64(key string, val float64) Entry

Float64 add Float64 field to current entry

func (Entry) Info

func (e Entry) Info(msg string)

Info level message.

func (Entry) Infof

func (e Entry) Infof(msg string, v ...interface{})

Infof level message.

func (Entry) Int added in v1.0.4

func (e Entry) Int(key string, val int) Entry

Int add Int field to current entry

func (Entry) Int16 added in v1.0.4

func (e Entry) Int16(key string, val int16) Entry

Int16 add Int16 field to current entry

func (Entry) Int32 added in v1.0.4

func (e Entry) Int32(key string, val int32) Entry

Int32 add Int32 field to current entry

func (Entry) Int64 added in v1.0.4

func (e Entry) Int64(key string, val int64) Entry

Int64 add Int64 field to current entry

func (Entry) Int8 added in v1.0.4

func (e Entry) Int8(key string, val int8) Entry

Int8 add Int8 field to current entry

func (Entry) Panic

func (e Entry) Panic(msg string)

Panic level message.

func (Entry) Panicf

func (e Entry) Panicf(msg string, v ...interface{})

Panicf level message.

func (Entry) Stop

func (e Entry) Stop()

Stop should be used with Trace, to fire off the completion message. When an `err` is passed the "error" field is set, and the log level is error.

func (Entry) Str added in v1.0.4

func (e Entry) Str(key string, val string) Entry

Str add string field to current entry

func (Entry) Trace

func (e Entry) Trace(msg string) Entry

Trace returns a new entry with a Stop method to fire off a corresponding completion log, useful with defer.

func (Entry) Uint added in v1.0.4

func (e Entry) Uint(key string, val uint) Entry

Uint add Uint field to current entry

func (Entry) Uint16 added in v1.0.4

func (e Entry) Uint16(key string, val uint16) Entry

Uint16 add Uint16 field to current entry

func (Entry) Uint32 added in v1.0.4

func (e Entry) Uint32(key string, val uint32) Entry

Uint32 add Uint32 field to current entry

func (Entry) Uint64 added in v1.0.4

func (e Entry) Uint64(key string, val uint64) Entry

Uint64 add Uint64 field to current entry

func (Entry) Uint8 added in v1.0.4

func (e Entry) Uint8(key string, val uint8) Entry

Uint8 add Uint8 field to current entry

func (Entry) Warn

func (e Entry) Warn(msg string)

Warn level message.

func (Entry) Warnf

func (e Entry) Warnf(msg string, v ...interface{})

Warnf level message.

func (Entry) WithError

func (e Entry) WithError(err error) Entry

WithError returns a new entry with the "error" set to `err`.

func (Entry) WithField

func (e Entry) WithField(key string, value interface{}) Entry

WithField returns a new entry with the `key` and `value` set.

func (Entry) WithFields

func (e Entry) WithFields(fields Fields) Entry

WithFields adds the provided fields to the current entry

type Fields

type Fields map[string]interface{}

Fields represents a map of entry level data used for structured logging.

func (Fields) Get

func (f Fields) Get(name string) interface{}

Get field value by name.

func (Fields) Names

func (f Fields) Names() (v []string)

Names returns field names sorted. map is not

type Flusher added in v1.0.1

type Flusher interface {
	Flush() error
}

Flusher is an interface that allow handles have the ability to clear buffer and close connection

type Handler

type Handler interface {
	Log(Entry) error
}

Handler is an interface that log handlers need to be implemented

type Level

type Level uint8

Level of the log

const (
	DebugLevel Level = iota
	InfoLevel
	WarnLevel
	ErrorLevel
	PanicLevel
	FatalLevel
)

Log levels.

func GetLevelsFromMinLevel

func GetLevelsFromMinLevel(minLevel string) []Level

GetLevelsFromMinLevel returns Levels array which above minLevel

func (Level) String

func (p Level) String() string

String returns the string representation of a logging level.

Directories

Path Synopsis
examples
handlers
discard
Package discard implements a no-op handler useful for benchmarks and tests.
Package discard implements a no-op handler useful for benchmarks and tests.
memory
Package memory implements an in-memory handler useful for testing, as the entries can be accessed after writes.
Package memory implements an in-memory handler useful for testing, as the entries can be accessed after writes.

Jump to

Keyboard shortcuts

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