log

package
v1.3.6 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: MIT Imports: 12 Imported by: 3

README

Read this in other languages: English, 中文.

log

zap_test.go

TestZapConsole

conf.Conf.LogLevel = "debug"
conf.Conf.Logs = []*conf.Log{{
	Logger:        lumberjack.Logger{Filename: "goutils.log"},
	OutputEncoder: "console",
	Stdout:        false,
	FileOut:       true,
}}
LogImpl = NewZapLogImpl()

ctx := ContextWithTraceId()
Debug(ctx, "I am debug log1")
LogLess()
Debug(ctx, "I am debug log2")

Info(ctx, "I am info log1: %v", "hello")
Log(ctx, zap.InfoLevel, "I am info level log1")
LogLess()
Log(ctx, zap.InfoLevel, "I am info level log2")
Info(ctx, "I am info log2: %v", "hello")

Warn(ctx, "I am warn log1: %v", "hello tom")
Log(ctx, zap.WarnLevel, "I am warn level log1: %v", "hello sam")
LogLess()
Warn(ctx, "I am warn log2: %v", "hello tom")
Log(ctx, zap.WarnLevel, "I am warn level log2: %v", "hello sam")

testPanicLog(func() {
	time.Sleep(time.Millisecond * 10)
	// panic(errors.New("test panic"))
})

Error(ctx, "I am error log1: %v, %v", "admin", "eee")
ErrorStack(ctx, "test panic log1")

LogLess()

Error(ctx, "I am error log2: %v, %v", "admin", "eee")
ErrorStack(ctx, "test panic log2")
TestZapJson

conf.Conf.Logs = []*conf.Log{{
	Logger:        lumberjack.Logger{Filename: "goutils.json"},
	OutputEncoder: "json",
	Stdout:        true,
	FileOut:       true,
}}
LogImpl = NewZapLogImpl()

ctx := ContextWithTraceId()

BaseFieldsGenerator = &GameDefaultFieldGenerator{}
Debug(ctx, "I am debug log1")

Info(ctx, "I am info log1", zap.String("userId", "0001"), zap.Bool("isAdmin", true))

Log(ctx, zap.WarnLevel, "I am warn log1", zap.String("userId", "0002"), zap.Bool("isAdmin", false))
TestZapHttpOut

conf.Conf.LogLevel = "debug"
conf.Conf.Logs = []*conf.Log{{
	OutputEncoder: "console",
	Stdout:        true,
	FileOut:       true,
	HttpOut:       true,
	HttpUrl:       "http://127.0.0.1:8053/goutils/log",
	HttpDebug:     true,
}}
LogImpl = NewZapLogImpl()
testRunLogServer(t, "log1", "log2")

ctx := ContextWithTraceId()
Debug(ctx, "I am debug log1")
LogLess()
Debug(ctx, "I am debug log2")

time.Sleep(time.Second)
TestLevelChange

SetLogLevel(zap.DebugLevel)

if GetLogLevel() != zap.DebugLevel {
	t.FailNow()
}

if LogLess() != zap.InfoLevel {
	t.FailNow()
}

if LogLess() != zap.WarnLevel {
	t.FailNow()
}

if LogLess() != zap.ErrorLevel {
	t.FailNow()
}

if LogLess() != zap.DPanicLevel {
	t.FailNow()
}

if LogLess() != zap.PanicLevel {
	t.FailNow()
}

if LogLess() != zap.FatalLevel {
	t.FailNow()
}

if LogLess() != zap.FatalLevel {
	t.FailNow()
}

SetLogLevel(zap.FatalLevel)

if LogMore() != zap.PanicLevel {
	t.FailNow()
}

if LogMore() != zap.DPanicLevel {
	t.FailNow()
}

if LogMore() != zap.ErrorLevel {
	t.FailNow()
}

if LogMore() != zap.WarnLevel {
	t.FailNow()
}

if LogMore() != zap.InfoLevel {
	t.FailNow()
}

if LogMore() != zap.DebugLevel {
	t.FailNow()
}

if LogMore() != zap.DebugLevel {
	t.FailNow()
}
TestNewTraceId

t.Log(NewTraceId())

Documentation

Index

Constants

View Source
const (
	LOGGER_ENCODER_JSON    = "json"
	LOGGER_ENCODER_CONSOLE = "console"
)

Variables

View Source
var LOG_TRACE_CTX_KEY = "__GTraceId__"
View Source
var LogImpl = NewZapLogImpl()

Functions

func ContextWithTraceId added in v1.3.5

func ContextWithTraceId() context.Context

func ContextWithTraceIdFromParent added in v1.3.5

func ContextWithTraceIdFromParent(parent context.Context) context.Context

func Debug

func Debug(ctx context.Context, msg string, args ...interface{})

func Error

func Error(ctx context.Context, msg string, args ...interface{})

func ErrorStack added in v1.1.25

func ErrorStack(ctx context.Context, msg string, args ...interface{})

func GetLogLevel added in v1.3.5

func GetLogLevel() zapcore.Level

func Info

func Info(ctx context.Context, msg string, args ...interface{})

func Log

func Log(ctx context.Context, level zapcore.Level, msg string, args ...interface{})

func LogLess

func LogLess() zapcore.Level

func LogMore

func LogMore() zapcore.Level

func NewTraceId added in v1.3.5

func NewTraceId() string

func Recover

func Recover(ctx context.Context, errHandler func(interface{}) string)

func SetLogLevel added in v1.3.5

func SetLogLevel(lvl zapcore.Level) zapcore.Level

func Warn

func Warn(ctx context.Context, msg string, args ...interface{})

Types

type DefaultFieldsGenerator added in v1.1.29

type DefaultFieldsGenerator struct {
	Nop []zapcore.Field
}

func (*DefaultFieldsGenerator) Generate added in v1.3.6

func (f *DefaultFieldsGenerator) Generate(ctx context.Context) []zapcore.Field

func (*DefaultFieldsGenerator) GenerateStr added in v1.3.6

func (f *DefaultFieldsGenerator) GenerateStr(context.Context) string

type IFieldsGenerator added in v1.3.6

type IFieldsGenerator interface {
	Generate(context.Context) []zapcore.Field
	GenerateStr(context.Context) string
}

IFieldsGenerator default json field

var BaseFieldsGenerator IFieldsGenerator = &DefaultFieldsGenerator{Nop: make([]zapcore.Field, 0)}

type ILog added in v1.3.6

type ILog interface {
	Log(context.Context, zapcore.Level, string, ...interface{})
	Debug(context.Context, string, ...interface{})
	Info(context.Context, string, ...interface{})
	Warn(context.Context, string, ...interface{})
	Error(context.Context, string, ...interface{})
	Recover(context.Context, func(interface{}) string)
	ErrorStack(context.Context, string, ...interface{})

	GetLogLevel() zapcore.Level
	SetLogLevel(zapcore.Level) zapcore.Level
	LogMore() zapcore.Level
	LogLess() zapcore.Level
}

func NewZapLogImpl added in v1.3.6

func NewZapLogImpl() ILog

type ZapLogImpl added in v1.3.6

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

func (*ZapLogImpl) Debug added in v1.3.6

func (l *ZapLogImpl) Debug(ctx context.Context, msg string, args ...interface{})

func (*ZapLogImpl) Error added in v1.3.6

func (l *ZapLogImpl) Error(ctx context.Context, msg string, args ...interface{})

func (*ZapLogImpl) ErrorStack added in v1.3.6

func (l *ZapLogImpl) ErrorStack(ctx context.Context, msg string, args ...interface{})

func (*ZapLogImpl) GetLogLevel added in v1.3.6

func (l *ZapLogImpl) GetLogLevel() zapcore.Level

func (*ZapLogImpl) Info added in v1.3.6

func (l *ZapLogImpl) Info(ctx context.Context, msg string, args ...interface{})

func (*ZapLogImpl) Log added in v1.3.6

func (l *ZapLogImpl) Log(ctx context.Context, level zapcore.Level, msg string, args ...interface{})

func (*ZapLogImpl) LogLess added in v1.3.6

func (l *ZapLogImpl) LogLess() zapcore.Level

func (*ZapLogImpl) LogMore added in v1.3.6

func (l *ZapLogImpl) LogMore() zapcore.Level

func (*ZapLogImpl) Recover added in v1.3.6

func (l *ZapLogImpl) Recover(ctx context.Context, errHandler func(interface{}) string)

func (*ZapLogImpl) SetLogLevel added in v1.3.6

func (l *ZapLogImpl) SetLogLevel(lvl zapcore.Level) zapcore.Level

func (*ZapLogImpl) Warn added in v1.3.6

func (l *ZapLogImpl) Warn(ctx context.Context, msg string, args ...interface{})

Jump to

Keyboard shortcuts

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