logs

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2018 License: MIT Imports: 5 Imported by: 0

README

Objective

Offer a Golang logger based on Lalamove k8s logging format.

Install

go get -u github.com/lalamove-go/logs

Importance

The backtrace will only show while the level => Error

Usage

import lalamove "github.com/lalamove-go/logs"

func main(){
    lalamove.Logger().Debug("I am a Debug")
    // {"level":"debug","time":"2017-12-23T05:42:47.752491212Z","src_file":"logs/logs_test.go:10","message":"I am a Debug","src_line":"10"}

    lalamove.Logger().Info("I am an Info")
    // {"level":"info","time":"2017-12-23T05:42:47.752524440Z","src_file":"logs/logs_test.go:11","message":"I am an Info","src_line":"11"}

    lalamove.Logger().Warn("I am a Warn")
    // {"level":"warning","time":"2018-01-12T04:20:12.336273889Z","src_file":"logs/logs_test.go:14","message":"I am a Warn","src_line":"14","context":{}}

    lalamove.Logger().Error("I am an Error")
    // {"level":"error","time":"2017-12-23T05:42:47.752575758Z","src_file":"logs/logs_test.go:13","message":"I am an Error","src_line":"13","backtrace":"github.com/logs.TestGetLalamoveLoggerPassDebug\n\t/home/alpha/works/src/github.com/logs/logs_test.go:13\ntesting.tRunner\n\t/home/alpha/go/src/testing/testing.go:746"}

    lalamove.Logger().Fatal("I am a Fatal")
    // {"level":"fatal","time":"2017-12-23T05:30:41.901899661Z","src_file":"logs/logs_test.go:49","message":"I am a Fatal","src_line":"49","backtrace":"github.com/logs.TestGetLalamoveLoggerPassFatal\n\t/home/alpha/works/src/github.com/logs/logs_test.go:49\ntesting.tRunner\n\t/home/alpha/go/src/testing/testing.go:746"}

    // Testing with extra fields
    lalamove.Logger().Debug("I am a Debug", zap.String("f0", "I go to school by bus"),zap.String("f1", "Goodest english"))
    // {"level":"debug","time":"2018-01-03T03:42:42.145362160Z","src_file":"logs/logs_test.go:40","message":"I am a Debug","src_line":"40","context":{"f0":"I go to school by bus","f1":"Goodest english"}}

    // Remember to close the logger
    defer lalamove.Logger().Sync()
}

Run test

go test . -v

Run benchmark

go test -bench=. -benchmem

Benchmark

lalamove-go/logs
1000000	      1812 ns/op	    1542 B/op	       8 allocs/op

go.uber.org/zap
1000000	      1385 ns/op	    1479 B/op	       8 allocs/op

Report issue

alpha.wong@lalamove.com

Credit

License

Released under the MIT License.

Documentation

Index

Constants

View Source
const (
	ISO8601 = "2006-01-02T15:04:05.000000000Z0700"

	TimeKey        = "time"
	LevelKey       = "level"
	CallerKey      = "src_file"
	MessageKey     = "message"
	StacktraceKey  = "backtrace"
	CustomFieldKey = "context"

	SourceLineKey = "src_line"

	Warning = "warning"
)

Variables

View Source
var (
	Log *zap.Logger
)

Functions

func LalamoveISO8601TimeEncoder

func LalamoveISO8601TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)

LalamoveISO8601TimeEncoder will convert the time to ISO8601 based on Lalamove k8s logging format

func LalamoveLevelEncoder

func LalamoveLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder)

LalamoveLevelEncoder will convert the warn display string to warning

func Logger

func Logger() *zap.Logger

Logger will create a zap based logger Extra field will inside fields namespace return a *zap.Logger for logging

func NewLalamoveEncoderConfig

func NewLalamoveEncoderConfig() *zapcore.EncoderConfig

NewLalamoveEncoderConfig will create an EncoderConfig

Types

This section is empty.

Jump to

Keyboard shortcuts

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