log

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2020 License: MIT Imports: 8 Imported by: 14

README

log

A lightweight, leveled logging package for Go.

Install

go get github.com/tevino/log

Features

  • Leveled logging and filtering
  • Colored output
  • Log with file name and line number

Example

import "github.com/tevino/log"

log.SetOutputLevel(log.INFO)

log.Debugf("Output level is %s.", log.OutputLevel())
log.Info("Thus only this line is printed.")

Benchmark

BenchmarkLog15-4              300000          4391 ns/op        1202 B/op         16 allocs/op
BenchmarkLogrus-4             500000          3444 ns/op        1715 B/op         24 allocs/op
BenchmarkLoggo-4              500000          3275 ns/op         344 B/op         11 allocs/op
BenchmarkGoLogging-4         1000000          1136 ns/op         601 B/op         10 allocs/op
BenchmarkThisPackage-4       3000000           731 ns/op         214 B/op          2 allocs/op
BenchmarkStdLog-4            2000000           571 ns/op         174 B/op          2 allocs/op

Acknowledgements

  • @kirk91: Provide APIs to make it possible for user to specify a custom caller offset.

Documentation

Index

Examples

Constants

View Source
const (
	ColorBlue    = "\x1b[0;34m"
	ColorGreen   = "\x1b[0;32m"
	ColorYellow  = "\x1b[0;33m"
	ColorRed     = "\x1b[0;31m"
	ColorMegenta = "\x1b[0;35m"

	ColorRST = "\x1b[0;m"
)

Colors.

View Source
const (
	Ldate         = 1 << iota     // the date in the local time zone: 2009/01/23
	Ltime                         // the time in the local time zone: 01:23:23
	Lmicroseconds                 // microsecond resolution: 01:23:23.123123.  assumes Ltime.
	Llongfile                     // full file name and line number: /a/b/c/d.go:23
	Lshortfile                    // final file name element and line number: d.go:23. overrides Llongfile
	LUTC                          // if Ldate or Ltime is set, use UTC rather than the local time zone
	LstdFlags     = Ldate | Ltime // initial values for the standard logger
)

Flags from standard package log are copied here to avoid importing two packages with the same name log.

These flags define which text to prefix to each log entry generated by the Logger.

Variables

This section is empty.

Functions

func Debug

func Debug(a ...interface{})

Debug calls the same method on the default logger.

func Debugf

func Debugf(f string, a ...interface{})

Debugf calls the same method on the default logger.

func Error

func Error(a ...interface{})

Error calls the same method on the default logger.

func Errorf

func Errorf(f string, a ...interface{})

Errorf calls the same method on the default logger.

func Fatal

func Fatal(a ...interface{})

Fatal calls the same method on the default logger.

func Fatalf

func Fatalf(f string, a ...interface{})

Fatalf calls the same method on the default logger.

func Info

func Info(a ...interface{})

Info calls the same method on the default logger.

func Infof

func Infof(f string, a ...interface{})

Infof calls the same method on the default logger.

func IsTerminal

func IsTerminal(w io.Writer) bool

IsTerminal returns true if the given writer supports colored output.

func Print

func Print(a ...interface{})

Print calls the same method on the default logger.

func Printf

func Printf(f string, a ...interface{})

Printf calls the same method on the default logger.

func Println

func Println(a ...interface{})

Println calls the same method on the default logger.

func SetCallerOffset

func SetCallerOffset(offset int)

SetCallerOffset calls the same method on the default logger.

func SetDefaultLevel

func SetDefaultLevel(l Level)

SetDefaultLevel calls the same method on the default logger.

func SetDefaultLogger

func SetDefaultLogger(logger Logger)

SetDefaultLogger sets the default logger with CallerOffset set. NOTE: this function is supposed to be called while DefaultLogger is not in use, it's not goroutine safe for performance.

func SetOutputLevel

func SetOutputLevel(l Level)

SetOutputLevel calls the same method on the default logger.

func Warn

func Warn(a ...interface{})

Warn calls the same method on the default logger.

func Warnf

func Warnf(f string, a ...interface{})

Warnf calls the same method on the default logger.

Types

type CallerOffsetter

type CallerOffsetter interface {
	SetCallerOffset(int)
}

CallerOffsetter provides the ability of setting caller offset.

type DebugLogger

type DebugLogger interface {
	Debug(...interface{})
	Debugf(string, ...interface{})

	DebugDepth(int, ...interface{})
	DebugfDepth(int, string, ...interface{})
}

DebugLogger represents a logger with Debug* APIs.

type ErrorLogger

type ErrorLogger interface {
	Error(...interface{})
	Errorf(string, ...interface{})

	ErrorDepth(int, ...interface{})
	ErrorfDepth(int, string, ...interface{})
}

ErrorLogger represents a logger with Error* APIs.

type FatalLogger

type FatalLogger interface {
	Fatal(...interface{})
	Fatalf(string, ...interface{})

	FatalDepth(int, ...interface{})
	FatalfDepth(int, string, ...interface{})
}

FatalLogger represents a logger with Fatal* APIs.

type InfoLogger

type InfoLogger interface {
	Info(...interface{})
	Infof(string, ...interface{})

	InfoDepth(int, ...interface{})
	InfofDepth(int, string, ...interface{})
}

InfoLogger represents a logger with Info* APIs.

type Level

type Level int32

Level represents level of logging.

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

Log levels from low to high. NOTE: FATAL is the highest, NOTSET is the lowest.

func DefaultLevel

func DefaultLevel() Level

DefaultLevel calls the same method on the default logger.

func LevelFromString

func LevelFromString(s string) Level

LevelFromString parses string to Level. NOTSET is returned if the given string can not be recognized.

func OutputLevel

func OutputLevel() Level

OutputLevel calls the same method on the default logger.

func (Level) String

func (l Level) String() string

String returns the string representation of Level.

type LeveledLogger

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

LeveledLogger has the ability of logging with different levels.

func NewLeveledLogger

func NewLeveledLogger(out io.Writer, flag int) *LeveledLogger

NewLeveledLogger creates a LeveledLogger with given output writer and flag. The flag argument defines the logging properties, it is the same as log.New. You could use flags from THIS log package to avoid importing standard package log.

func NewLeveledLoggerWithColor

func NewLeveledLoggerWithColor(out io.Writer, flag int, colored bool) *LeveledLogger

NewLeveledLoggerWithColor is NewLeveledLogger with an additional colored parameter indicating if color is forced.

func (*LeveledLogger) Debug

func (l *LeveledLogger) Debug(a ...interface{})

Debug prints log with level DEBUG. Arguments are handled in the manner of fmt.Print.

func (*LeveledLogger) DebugDepth

func (l *LeveledLogger) DebugDepth(depth int, a ...interface{})

DebugDepth acts as Debug but uses depth to determine which call frame to log DebugDepth(0, "msg") is the same as Debug("msg")

func (*LeveledLogger) Debugf

func (l *LeveledLogger) Debugf(format string, a ...interface{})

Debugf prints log with level DEBUG. Arguments are handled in the manner of fmt.Printf.

func (*LeveledLogger) DebugfDepth

func (l *LeveledLogger) DebugfDepth(depth int, format string, a ...interface{})

DebugfDepth acts as Debugf but uses depth to determine which call frame to log DebugfDepth(0, "msg") is the same as Debugf("msg")

func (*LeveledLogger) DefaultLevel

func (l *LeveledLogger) DefaultLevel() Level

DefaultLevel is the level used by Print* methods.

func (*LeveledLogger) Error

func (l *LeveledLogger) Error(a ...interface{})

Error prints log with level ERROR. Arguments are handled in the manner of fmt.Print.

func (*LeveledLogger) ErrorDepth

func (l *LeveledLogger) ErrorDepth(depth int, a ...interface{})

ErrorDepth acts as Error but uses depth to determine which call frame to log ErrorDepth(0, "msg") is the same as Error("msg")

func (*LeveledLogger) Errorf

func (l *LeveledLogger) Errorf(format string, a ...interface{})

Errorf prints log with level ERROR. Arguments are handled in the manner of fmt.Printf.

func (*LeveledLogger) ErrorfDepth

func (l *LeveledLogger) ErrorfDepth(depth int, format string, a ...interface{})

ErrorfDepth acts as Errorf but uses depth to determine which call frame to log ErrorfDepth(0, "msg") is the same as Errorf("msg")

func (*LeveledLogger) Fatal

func (l *LeveledLogger) Fatal(a ...interface{})

Fatal prints log with level FATA then os.Exit(1). Arguments are handled in the manner of fmt.Print.

func (*LeveledLogger) FatalDepth

func (l *LeveledLogger) FatalDepth(depth int, a ...interface{})

FatalDepth acts as Fatal but uses depth to determine which call frame to log FatalDepth(0, "msg") is the same as Fatal("msg")

func (*LeveledLogger) Fatalf

func (l *LeveledLogger) Fatalf(format string, a ...interface{})

Fatalf prints log with level FATA then os.Exit(1). Arguments are handled in the manner of fmt.Printf.

func (*LeveledLogger) FatalfDepth

func (l *LeveledLogger) FatalfDepth(depth int, format string, a ...interface{})

FatalfDepth acts as Fatalf but uses depth to determine which call frame to log FatalfDepth(0, "msg") is the same as Fatalf("msg")

func (*LeveledLogger) Info

func (l *LeveledLogger) Info(a ...interface{})

Info prints log with level INFO. Arguments are handled in the manner of fmt.Print.

func (*LeveledLogger) InfoDepth

func (l *LeveledLogger) InfoDepth(depth int, a ...interface{})

InfoDepth acts as Info but uses depth to determine which call frame to log InfoDepth(0, "msg") is the same as Info("msg")

func (*LeveledLogger) Infof

func (l *LeveledLogger) Infof(format string, a ...interface{})

Infof prints log with level INFO. Arguments are handled in the manner of fmt.Printf.

func (*LeveledLogger) InfofDepth

func (l *LeveledLogger) InfofDepth(depth int, format string, a ...interface{})

InfofDepth acts as Infof but uses depth to determine which call frame to log InfofDepth(0, "msg") is the same as Infof("msg")

func (*LeveledLogger) OutputLevel

func (l *LeveledLogger) OutputLevel() Level

OutputLevel returns the minimal Level of log that will be outputted. Levels lower than this will be ignored.

func (*LeveledLogger) Print

func (l *LeveledLogger) Print(a ...interface{})

Print prints log with DefaultLevel. Arguments are handled in the manner of fmt.Print.

func (*LeveledLogger) PrintDepth

func (l *LeveledLogger) PrintDepth(depth int, a ...interface{})

PrintDepth acts as Print but uses depth to determine which call frame to log PrintDepth(0, "msg") is the same as Print("msg")

func (*LeveledLogger) Printf

func (l *LeveledLogger) Printf(format string, a ...interface{})

Printf prints log with DefaultLevel. Arguments are handled in the manner of fmt.Printf.

func (*LeveledLogger) PrintfDepth

func (l *LeveledLogger) PrintfDepth(depth int, format string, a ...interface{})

PrintfDepth acts as Printf but uses depth to determine which call frame to log PrintfDepth(0, "msg") is the same as Printf("msg")

func (*LeveledLogger) Println

func (l *LeveledLogger) Println(a ...interface{})

Println prints log with DefaultLevel. Arguments are handled in the manner of fmt.Println.

func (*LeveledLogger) PrintlnDepth

func (l *LeveledLogger) PrintlnDepth(depth int, a ...interface{})

PrintlnDepth acts as Printfln but uses depth to determine which call frame to log PrintflnDepth(0, "msg") is the same as Printfln("msg")

func (*LeveledLogger) SetCallerOffset

func (l *LeveledLogger) SetCallerOffset(offset int)

SetCallerOffset sets the offset used in runtime.Caller(3 + offset) while getting file name and line number. NOTE: Do not call this while logging, it's not goroutine safe.

func (*LeveledLogger) SetDefaultLevel

func (l *LeveledLogger) SetDefaultLevel(level Level)

SetDefaultLevel sets the DefaultLevel atomically.

func (*LeveledLogger) SetOutputLevel

func (l *LeveledLogger) SetOutputLevel(level Level)

SetOutputLevel sets the OutputLevel atomically.

func (*LeveledLogger) Warn

func (l *LeveledLogger) Warn(a ...interface{})

Warn prints log with level WARN. Arguments are handled in the manner of fmt.Print.

func (*LeveledLogger) WarnDepth

func (l *LeveledLogger) WarnDepth(depth int, a ...interface{})

WarnDepth acts as Warn but uses depth to determine which call frame to log WarnDepth(0, "msg") is the same as Warn("msg")

func (*LeveledLogger) Warnf

func (l *LeveledLogger) Warnf(format string, a ...interface{})

Warnf prints log with level WARN. Arguments are handled in the manner of fmt.Printf.

func (*LeveledLogger) WarnfDepth

func (l *LeveledLogger) WarnfDepth(depth int, format string, a ...interface{})

WarnfDepth acts as Warnf but uses depth to determine which call frame to log WarnfDepth(0, "msg") is the same as Warnf("msg")

type Leveler

type Leveler interface {
	DefaultLevel() Level
	SetDefaultLevel(Level)

	OutputLevel() Level
	SetOutputLevel(Level)
}

Leveler contains level-related APIs.

type Logger

Logger represents a full-featured logger.

Example
l := NewLoggerWithColor(os.Stdout, 0, false)
// Color and time-related flags should be disabled in testing.
// While you may want to use the following instead:
// l := NewLeveledLogger(os.Stdout, LstdFlags)

l.SetOutputLevel(INFO)
l.Debug("Output level is INFO.")
l.Info("Thus debug is not printed.")

l.SetOutputLevel(DEBUG)
l.Debug("The above line enables debug.")
Output:

I Thus debug is not printed.
D The above line enables debug.

func DefaultLogger

func DefaultLogger() Logger

DefaultLogger returns the default logger which writes to os.Stdout.

func NewLogger

func NewLogger(out io.Writer, flag int) Logger

NewLogger creates a Logger with given output and flag. The flag argument defines the logging properties, it is the same as log.New. You could use flags from THIS log package to avoid importing standard package log.

func NewLoggerWithColor

func NewLoggerWithColor(out io.Writer, flag int, colored bool) Logger

NewLoggerWithColor is NewLogger with an additional colored parameter indicating if color is forced.

type PrintLogger

type PrintLogger interface {
	Print(...interface{})
	Printf(string, ...interface{})
	Println(...interface{})

	PrintDepth(int, ...interface{})
	PrintfDepth(int, string, ...interface{})
	PrintlnDepth(int, ...interface{})
}

PrintLogger represents a logger with Print* APIs.

type WarnLogger

type WarnLogger interface {
	Warn(...interface{})
	Warnf(string, ...interface{})

	WarnDepth(int, ...interface{})
	WarnfDepth(int, string, ...interface{})
}

WarnLogger represents a logger with Warn* APIs.

Jump to

Keyboard shortcuts

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