lgr

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2019 License: MIT Imports: 8 Imported by: 399

README

lgr - simple logger with some extras Build Status Coverage Status godoc

install

go get github/go-pkgz/lgr

usage

    l := lgr.New(lgr.Debug, lgr.CallerFile) // allow debug and caller file info
    l.Logf("INFO some important err message, %v", err)
    l.Logf("DEBUG some less important err message, %v", err)

output looks like this:

2018/01/07 13:02:34.000 INFO  {svc/handler.go:101 h.MyFunc1} some important err message, can't open file`
2018/01/07 13:02:34.015 DEBUG {svc/handler.go:155 h.MyFunc2} some less important err message, file is too small`

Without lgr.CallerFile it will drop {caller} part

details

interfaces and default loggers
  • lgr package provides a single interface lgr.L with a single method Logf(format string, args ...interface{}). Function wrapper lgr.Func allows to make lgr.L from a function directly.
  • Default logger functionality can be used without lgr.New, but just lgr.Printf
  • Two predefined loggers available: lgr.NoOp (do-nothing logger) and lgr.Std (passing directly to stdlib log)
options

lgr.New call accepts functional options:

  • lgr.Debug - turn debug mode on. This allows messages with "DEBUG" level (filtered overwise)
  • lgr.CallerFile - adds the caller file info each message
  • lgr.CallerFunc - adds the caller function info each message
  • lgr.LevelBraces - wraps levels with "[" and "]"
  • lgr.Msec - adds milliseconds to timestamp
  • lgr.Out(io.Writer) - sets the output writer, default os.Stdout
  • lgr.Err(io.Writer) - sets the error writer, default os.Stderr
levels

lgr.Logf recognizes prefixes like "INFO" or "[INFO]" as levels. The full list of supported levels - "DEBUG", "INFO", "WARN", "ERROR", "PANIC" and "FATAL"

  • DEBUG will be filtered unless lgr.Debug option defined
  • INFO and WARN don't have any special behavior attached
  • ERROR sends messages to both out and err writers
  • PANIC and FATAL send messages to both out and err writers. In addition sends dump of callers and runtime info to err only, and call os.Exit(1).
global logger

Users should avoid global logger and pass the concrete logger as a dependency. However, in some cases global logger may be needed, for example migration from stdlib log to lgr. For such cases log "github.com/go-pkgz/lgr" can be imported instead of log package.

Global logger provides lgr.Printf, lgr.Print and lgr.Fatalf functions. User can customize the logger by calling lgr.Setup(options ...). The instance of this logger can be retried with lgr.Default()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoOp = Func(func(format string, args ...interface{}) {})

NoOp logger

View Source
var Std = Func(func(format string, args ...interface{}) { stdlog.Printf(format, args...) })

Std logger sends to std default logger directly

Functions

func CallerFile added in v0.1.4

func CallerFile(l *Logger)

CallerFile adds caller info with file, and line number

func CallerFunc added in v0.1.4

func CallerFunc(l *Logger)

CallerFunc adds caller info with function name

func Debug

func Debug(l *Logger)

Debug turn on dbg mode

func Fatalf added in v0.2.0

func Fatalf(format string, args ...interface{})

Fatalf simplifies replacement of std logger

func LevelBraces added in v0.1.4

func LevelBraces(l *Logger)

LevelBraces adds [] to level

func Msec added in v0.2.0

func Msec(l *Logger)

Msec adds .msec to timestamp

func Print added in v0.1.2

func Print(line string)

Print simplifies replacement of std logger

func Printf

func Printf(format string, args ...interface{})

Printf simplifies replacement of std logger

func Setup added in v0.1.1

func Setup(opts ...Option)

Setup default logger with options

Types

type Func

type Func func(format string, args ...interface{})

Func type is an adapter to allow the use of ordinary functions as Logger.

func (Func) Logf

func (f Func) Logf(format string, args ...interface{})

Logf calls f(id)

type L

type L interface {
	Logf(format string, args ...interface{})
}

L defines minimal interface used to log things

func Default added in v0.1.1

func Default() L

Default returns pre-constructed def logger (debug off, callers disabled)

type Logger

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

Logger provided simple logger with basic support of levels. Thread safe

func New

func New(options ...Option) *Logger

New makes new leveled logger. Accepts dbg flag turing on info about the caller and allowing DEBUG messages/ Two writers can be passed optionally - first for out and second for err

func (*Logger) Logf

func (l *Logger) Logf(format string, args ...interface{})

Logf implements L interface to output with printf style. Each line prefixed with ts, level and optionally (dbg mode only) by caller info. ERROR and FATAL also send the same line to err writer. FATAL adds runtime stack and os.exit(1), like panic.

type Option

type Option func(l *Logger)

Option func type

func Err

func Err(w io.Writer) Option

Err sets error writer

func Out

func Out(w io.Writer) Option

Out sets out writer

Jump to

Keyboard shortcuts

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