log

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2022 License: MIT Imports: 11 Imported by: 4

README

Logger

Usage

全局共用一个global logger
package main

import (
	"github.com/sado0823/go-kitx/kit/log"
)

func main() {
	log.Debug("debug")
	log.Info("info")
	log.Warn("warn")
	log.Error("error")
	log.Fatal("fatal")

	// output
	//DEBUG  ts=2022-09-12T19:11:58+08:00 caller=go-kitx/main.go:8 msg=debug
	//INFO  ts=2022-09-12T19:11:58+08:00 caller=go-kitx/main.go:9 msg=info
	//WARN  ts=2022-09-12T19:11:58+08:00 caller=go-kitx/main.go:10 msg=warn
	//ERROR  ts=2022-09-12T19:11:58+08:00 caller=go-kitx/main.go:11 msg=error
	//FATAL  ts=2022-09-12T19:11:58+08:00 caller=go-kitx/main.go:12 msg=fatal

}

每个package自定义自己前缀的logger
package main

import (
	"fmt"
	"os"

	"github.com/sado0823/go-kitx/kit/log"
)

var (
	logger log.Logger
	helper *log.Helper
)

func init() {

	logger = log.NewStd(os.Stdout)
	// fields & valuer
	logger = log.WithFields(logger,
		"service.name", "hellworld",
		"service.version", "v1.0.0",
		"ts", log.DefaultTimestamp,
		"caller", log.Caller(3),
	)

	// helper
	helper = log.NewHelper(logger)
}

func main() {
	logger.Log(log.LevelError, "error")
	logger.Log(log.LevelInfo, "info")

	fmt.Println("")

	helper.Debug("debug")
	helper.Info("info")
	helper.Warn("warn")
	helper.Error("error")
	helper.Fatal("fatal")

	// output
	//ERROR  service.name=hellworld service.version=v1.0.0 ts=2022-09-12T19:09:59+08:00 caller=go-kitx/main.go:30 error=log key value unpaired
	//INFO  service.name=hellworld service.version=v1.0.0 ts=2022-09-12T19:09:59+08:00 caller=go-kitx/main.go:31 info=log key value unpaired
	//
	//DEBUG  service.name=hellworld service.version=v1.0.0 ts=2022-09-12T19:09:59+08:00 caller=log/helper.go:47 msg=debug
	//INFO  service.name=hellworld service.version=v1.0.0 ts=2022-09-12T19:09:59+08:00 caller=log/helper.go:59 msg=info
	//WARN  service.name=hellworld service.version=v1.0.0 ts=2022-09-12T19:09:59+08:00 caller=log/helper.go:71 msg=warn
	//ERROR  service.name=hellworld service.version=v1.0.0 ts=2022-09-12T19:09:59+08:00 caller=log/helper.go:83 msg=error
	//FATAL  service.name=hellworld service.version=v1.0.0 ts=2022-09-12T19:09:59+08:00 caller=log/helper.go:95 msg=fatal
}

其它方式
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/sado0823/go-kitx/kit/log"
)

var (
	logger  = log.NewHelper(log.WithFields(log.NewStd(os.Stdout), "caller", log.Caller(4)))
	logger2 = log.NewHelper(log.WithFields(log.GetGlobal(), "dao.name", "article"))
)

func main() {
	logger.Log(log.LevelError, "abc1", "123")
	logger2.Log(log.LevelError, "abc1", "123")
	log.Log(log.LevelError, "abc2", "432")

	fmt.Println("")

	logger.Log(log.LevelError, "abc1", "123")
	logger2.Log(log.LevelError, "abc1", "123")
	log.Log(log.LevelError, "abc2", "432")

	fmt.Println("")

	logger.Error("1")
	logger2.Error("1")
	log.Error("2")

	fmt.Println("")
	ctx := context.Background()

	logger.WithContext(ctx).Error("1")
	logger2.WithContext(ctx).Error("1")
	log.Context(ctx).Error("222")

	// output
	//ERROR  caller=go-kitx/main.go:17 abc1=123
	//ERROR  ts=2022-09-12T19:06:09+08:00 caller=go-kitx/main.go:18 dao.name=article abc1=123
	//ERROR  ts=2022-09-12T19:06:09+08:00 caller=go-kitx/main.go:19 abc2=432
	//
	//ERROR  caller=go-kitx/main.go:23 abc1=123
	//ERROR  ts=2022-09-12T19:06:09+08:00 caller=go-kitx/main.go:24 dao.name=article abc1=123
	//ERROR  ts=2022-09-12T19:06:09+08:00 caller=go-kitx/main.go:25 abc2=432
	//
	//ERROR  caller=go-kitx/main.go:29 msg=1
	//ERROR  ts=2022-09-12T19:06:09+08:00 caller=go-kitx/main.go:30 dao.name=article msg=1
	//ERROR  ts=2022-09-12T19:06:09+08:00 caller=go-kitx/main.go:31 msg=2
	//
	//ERROR  caller=go-kitx/main.go:36 msg=1
	//ERROR  ts=2022-09-12T19:06:09+08:00 caller=go-kitx/main.go:37 dao.name=article msg=1
	//ERROR  ts=2022-09-12T19:06:09+08:00 caller=go-kitx/main.go:38 msg=222

}

Third party log plugin

logrus
go get -u github.com/sado0823/go-kitx/plugin/logger/logrus
import (
	"context"
	"fmt"
	
	"github.com/sado0823/go-kitx/kit/log"
	pLogger "github.com/sado0823/go-kitx/plugin/logger/logrus"
	"github.com/sirupsen/logrus"
)

func init() {
	logger := pLogger.New(logrus.New())
	// fields & valuer
	logger = log.WithFields(logger,
		"service.name", "hellworld",
		"service.version", "v1.0.0",
		"ts", log.DefaultTimestamp,
		"caller", log.DefaultCaller,
	)
	
	log.SetGlobal(logger)
}

func main() {

	log.Debug("debug", 123)
	log.Info("info", 456)
	log.Warn("warn")
	log.Error("error")
	//log.Fatal("fatal")
	log.Context(context.Background()).Error("ccccccc")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultCaller    = Caller(4)
	DefaultTimestamp = Timestamp(time.RFC3339)
)
View Source
var DefaultMessageKey = "msg"

Functions

func Debug

func Debug(a ...interface{})

func Debugf

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

func Debugw

func Debugw(keyvals ...interface{})

func Error

func Error(a ...interface{})

func Errorf

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

func Errorw

func Errorw(keyvals ...interface{})

func Fatal

func Fatal(a ...interface{})

func Fatalf

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

func Fatalw

func Fatalw(keyvals ...interface{})

func Info

func Info(a ...interface{})

func Infof

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

func Infow

func Infow(keyvals ...interface{})

func Log

func Log(level Level, keyvals ...interface{})

func NewWriter

func NewWriter(l Logger, ops ...WriterOption) io.Writer

func SetGlobal

func SetGlobal(l Logger)

SetGlobal should be called before init App

func Warn

func Warn(a ...interface{})

func Warnf

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

func Warnw

func Warnw(keyvals ...interface{})

Types

type Helper

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

func Context

func Context(ctx context.Context) *Helper

func NewHelper

func NewHelper(logger Logger, options ...HelperOption) *Helper

func (*Helper) Debug

func (h *Helper) Debug(a ...interface{})

func (*Helper) Debugf

func (h *Helper) Debugf(format string, a ...interface{})

func (*Helper) Debugw

func (h *Helper) Debugw(keyvals ...interface{})

func (*Helper) Error

func (h *Helper) Error(a ...interface{})

func (*Helper) Errorf

func (h *Helper) Errorf(format string, a ...interface{})

func (*Helper) Errorw

func (h *Helper) Errorw(keyvals ...interface{})

func (*Helper) Fatal

func (h *Helper) Fatal(a ...interface{})

func (*Helper) Fatalf

func (h *Helper) Fatalf(format string, a ...interface{})

func (*Helper) Fatalw

func (h *Helper) Fatalw(keyvals ...interface{})

func (*Helper) Info

func (h *Helper) Info(a ...interface{})

func (*Helper) Infof

func (h *Helper) Infof(format string, a ...interface{})

func (*Helper) Infow

func (h *Helper) Infow(keyvals ...interface{})

func (*Helper) Log

func (h *Helper) Log(level Level, keyvals ...interface{})

func (*Helper) Warn

func (h *Helper) Warn(a ...interface{})

func (*Helper) Warnf

func (h *Helper) Warnf(format string, a ...interface{})

func (*Helper) Warnw

func (h *Helper) Warnw(keyvals ...interface{})

func (*Helper) WithContext

func (h *Helper) WithContext(ctx context.Context) *Helper

type HelperOption

type HelperOption func(helper *Helper)

func WithHelperMessageKey

func WithHelperMessageKey(msgKey string) HelperOption

type Level

type Level int8
const (
	LevelDebug Level = iota - 1
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
)

func ToLevel

func ToLevel(s string) Level

func (Level) String

func (l Level) String() string

type Logger

type Logger interface {
	Log(level Level, kvs ...interface{}) error
}

func GetGlobal

func GetGlobal() Logger

func NewStd

func NewStd(w io.Writer) Logger

func WithContext

func WithContext(ctx context.Context, l Logger) Logger

WithContext return a shadow copy of logger with a new context

func WithFields

func WithFields(l Logger, kvs ...interface{}) Logger

WithFields add new fields to the logger

type Valuer

type Valuer func(ctx context.Context) interface{}

func Caller

func Caller(depth int) Valuer

func Timestamp

func Timestamp(layout string) Valuer

type WriterOption

type WriterOption func(*writer)

func WithWriterLevel

func WithWriterLevel(level Level) WriterOption

func WithWriterMessageKey

func WithWriterMessageKey(msgKey string) WriterOption

Jump to

Keyboard shortcuts

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