log

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2019 License: GPL-3.0 Imports: 11 Imported by: 3

README

go-log GoDoc Report card Sourcegraph

Go logging done right (well, you know...)

Usage

import "github.com/golangly/log"
...
log.Trace("Hello!")
log.Debug("Hello!")
log.Info("Hello!")
log.Warn("Hello!")
log.Error("Hello!")
log.Panic("Hello!")
log.Fatal("Hello!")

So far, so good - right? well that's basic usage identical to most other loggers. In factm, this logging implementation is probably even a bit slower than loggers such as zerolog or ruslog. There are, however, a few extra features that make this library both opinionated and more compatible to my view of logging:

  • Accept recovered panic objects (via recover() in defer functions usually) and infer automatically whether they are errors or not, and treat them accordingly (it's still a panic, of course and treated as such also).
  • Automatic configuration from environment variables since you want to configure logging before everything else, including before command line parsing.
  • Only print to console. Use other tools to route logging to other places.
  • Supports go-errors for error stack traces and error causes automatically.
  • Super simple.

Configuration

All environment variable names & values are case insensitive.

Set LOG_PRETTY to 1, y, yes or true to enable pretty print (by default JSON logging is used).

Set LOG_LEVEL to disabled, panic or fatal to disable logging (except panics & fatal events), or to error, warn, info, debug or trace to set the log level accordingly.

Contributing

Please read the Code of Conduct & Contributing documents.

License

GNUv3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Root    = &Logger{context: make(map[string]interface{}, 0), level: LevelInfo}
	Printer func(c Logger, msg string)
)

Functions

func Debug

func Debug(msg string)

func Error

func Error(msg string)

func Fatal

func Fatal(msg string)

func Info

func Info(msg string)

func JSONPrinter added in v0.0.3

func JSONPrinter(c Logger, msg string)

func Panic

func Panic(msg string)

func PrettyPrinter added in v0.0.3

func PrettyPrinter(c Logger, msg string)

func Trace

func Trace(msg string)

func Warn

func Warn(msg string)

func Writer

func Writer() io.Writer

Types

type Level

type Level uint
const (
	LevelTrace Level = iota
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelDisabled
)

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func With

func With(key string, val interface{}) *Logger

func WithErr

func WithErr(err error) *Logger

func WithLevel

func WithLevel(level Level) *Logger

func WithPanic

func WithPanic(recovered interface{}) *Logger

func (*Logger) Debug

func (c *Logger) Debug(msg string)

func (*Logger) Error

func (c *Logger) Error(msg string)

func (*Logger) Fatal

func (c *Logger) Fatal(msg string)

func (*Logger) Info

func (c *Logger) Info(msg string)

func (*Logger) Panic

func (c *Logger) Panic(msg string)

func (*Logger) Trace

func (c *Logger) Trace(msg string)

func (*Logger) Warn

func (c *Logger) Warn(msg string)

func (*Logger) With

func (c *Logger) With(key string, val interface{}) *Logger

func (*Logger) WithErr

func (c *Logger) WithErr(err error) *Logger

func (*Logger) WithLevel

func (c *Logger) WithLevel(level Level) *Logger

func (*Logger) WithPanic

func (c *Logger) WithPanic(recovered interface{}) *Logger

func (*Logger) Writer

func (c *Logger) Writer() io.Writer

Jump to

Keyboard shortcuts

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