log

package
v1.1.0-rc3 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

README

log

重新定义标准日志接口,可以灵活适配各种日志框架。

Install

go get github.com/go-spring/spring-base@v1.1.0-rc2 

Import

import "github.com/go-spring/spring-base/log"

Example

log.SetLevel(log.TraceLevel)
defer log.Reset()

log.Trace("a", "=", "1")
log.Tracef("a=%d", 1)

log.Trace(func() []interface{} {
    return log.T("a", "=", "1")
})

log.Tracef("a=%d", func() []interface{} {
    return log.T(1)
})

...
ctx := context.WithValue(context.TODO(), traceIDKey, "0689")

log.SetLevel(log.TraceLevel)
log.SetOutput(myOutput)
defer log.Reset()

logger := log.Ctx(ctx)
logger.Trace("level:", "trace")
logger.Tracef("level:%s", "trace")

...

logger = log.Tag("__in")
logger.Ctx(ctx).Trace("level:", "trace")
logger.Ctx(ctx).Tracef("level:%s", "trace")

...

Documentation

Overview

Package log 重新定义标准日志接口,可以灵活适配各种日志框架。

Package log is a generated GoMock package.

Index

Constants

View Source
const (
	TraceLevel = Level(iota)
	DebugLevel
	InfoLevel
	WarnLevel
	ErrorLevel
	PanicLevel
	FatalLevel
)

Variables

View Source
var Console = FuncOutput(func(level Level, msg *Message) {
	defer func() { msg.Reuse() }()
	strLevel := strings.ToUpper(level.String())
	if level >= ErrorLevel {
		strLevel = color.Red.Sprint(strLevel)
	} else if level == WarnLevel {
		strLevel = color.Yellow.Sprint(strLevel)
	} else if level == TraceLevel {
		strLevel = color.Green.Sprint(strLevel)
	}
	var buf bytes.Buffer
	for _, a := range msg.Args() {
		buf.WriteString(cast.ToString(a))
	}
	strTime := msg.Time().Format("2006-01-02T15:04:05.000")
	fileLine := util.Contract(fmt.Sprintf("%s:%d", msg.File(), msg.Line()), 48)
	_, _ = fmt.Printf("[%s][%s][%s] %s\n", strLevel, strTime, fileLine, buf.String())
})

Console 将日志输出到控制台。

View Source
var UnknownError = NewErrNo(999, 999, "UNKNOWN ERROR")

Functions

func Caller

func Caller(skip int, fast bool) (file string, line int, loaded bool)

Caller 获取调用点的文件及行号信息,fast 为 true 时使用缓存进行加速。

func Debug

func Debug(args ...interface{})

Debug 输出 DEBUG 级别的日志。

func Debugf

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

Debugf 输出 DEBUG 级别的日志。

func EnableDebug

func EnableDebug() bool

EnableDebug 是否允许输出 DEBUG 级别的日志。

func EnableError

func EnableError() bool

EnableError 是否允许输出 ERROR 级别的日志。

func EnableFatal

func EnableFatal() bool

EnableFatal 是否允许输出 FATAL 级别的日志。

func EnableInfo

func EnableInfo() bool

EnableInfo 是否允许输出 INFO 级别的日志。

func EnablePanic

func EnablePanic() bool

EnablePanic 是否允许输出 PANIC 级别的日志。

func EnableTrace

func EnableTrace() bool

EnableTrace 是否允许输出 TRACE 级别的日志。

func EnableWarn

func EnableWarn() bool

EnableWarn 是否允许输出 WARN 级别的日志。

func Error

func Error(args ...interface{})

Error 输出 ERROR 级别的日志。

func Errorf

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

Errorf 输出 ERROR 级别的日志。

func Fatal

func Fatal(args ...interface{})

Fatal 输出 FATAL 级别的日志。

func Fatalf

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

Fatalf 输出 FATAL 级别的日志。

func Info

func Info(args ...interface{})

Info 输出 INFO 级别的日志。

func Infof

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

Infof 输出 INFO 级别的日志。

func Panic

func Panic(args ...interface{})

Panic 输出 PANIC 级别的日志。

func Panicf

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

Panicf 输出 PANIC 级别的日志。

func Reset

func Reset()

Reset 恢复默认的日志输出配置。

func SetLevel

func SetLevel(level Level)

SetLevel 设置日志的输出级别。

func SetOutput

func SetOutput(output Output)

SetOutput 设置日志的输出格式。

func T added in v1.1.3

func T(a ...interface{}) []interface{}

T 将可变参数转换成切片形式。

func Trace

func Trace(args ...interface{})

Trace 输出 TRACE 级别的日志。

func Tracef

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

Tracef 输出 TRACE 级别的日志。

func Warn

func Warn(args ...interface{})

Warn 输出 WARN 级别的日志。

func Warnf

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

Warnf 输出 WARN 级别的日志。

Types

type BaseEntry

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

func Skip

func Skip(n int) BaseEntry

Skip 创建包含 skip 信息的 Entry 。

func Tag

func Tag(tag string) BaseEntry

Tag 创建包含 tag 信息的 Entry 。

func (BaseEntry) Ctx

func (e BaseEntry) Ctx(ctx context.Context) CtxEntry

func (BaseEntry) Debug

func (e BaseEntry) Debug(args ...interface{})

Debug 输出 DEBUG 级别的日志。

func (BaseEntry) Debugf

func (e BaseEntry) Debugf(format string, args ...interface{})

Debugf 输出 DEBUG 级别的日志。

func (BaseEntry) Error

func (e BaseEntry) Error(args ...interface{})

Error 输出 ERROR 级别的日志。

func (BaseEntry) Errorf

func (e BaseEntry) Errorf(format string, args ...interface{})

Errorf 输出 ERROR 级别的日志。

func (BaseEntry) Fatal

func (e BaseEntry) Fatal(args ...interface{})

Fatal 输出 FATAL 级别的日志。

func (BaseEntry) Fatalf

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

Fatalf 输出 FATAL 级别的日志。

func (BaseEntry) GetContext

func (e BaseEntry) GetContext() context.Context

func (BaseEntry) GetErrNo

func (e BaseEntry) GetErrNo() ErrNo

func (BaseEntry) GetSkip

func (e BaseEntry) GetSkip() int

func (BaseEntry) GetTag

func (e BaseEntry) GetTag() string

func (BaseEntry) Info

func (e BaseEntry) Info(args ...interface{})

Info 输出 INFO 级别的日志。

func (BaseEntry) Infof

func (e BaseEntry) Infof(format string, args ...interface{})

Infof 输出 INFO 级别的日志。

func (BaseEntry) Panic

func (e BaseEntry) Panic(args ...interface{})

Panic 输出 PANIC 级别的日志。

func (BaseEntry) Panicf

func (e BaseEntry) Panicf(format string, args ...interface{})

Panicf 输出 PANIC 级别的日志。

func (BaseEntry) Skip

func (e BaseEntry) Skip(n int) BaseEntry

func (BaseEntry) Tag

func (e BaseEntry) Tag(tag string) BaseEntry

func (BaseEntry) Trace

func (e BaseEntry) Trace(args ...interface{})

Trace 输出 TRACE 级别的日志。

func (BaseEntry) Tracef

func (e BaseEntry) Tracef(format string, args ...interface{})

Tracef 输出 TRACE 级别的日志。

func (BaseEntry) Warn

func (e BaseEntry) Warn(args ...interface{})

Warn 输出 WARN 级别的日志。

func (BaseEntry) Warnf

func (e BaseEntry) Warnf(format string, args ...interface{})

Warnf 输出 WARN 级别的日志。

type CtxEntry

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

func Ctx

func Ctx(ctx context.Context) CtxEntry

Ctx 创建包含 context.Context 对象的 Entry 。

func (CtxEntry) Debug

func (e CtxEntry) Debug(args ...interface{})

Debug 输出 DEBUG 级别的日志。

func (CtxEntry) Debugf

func (e CtxEntry) Debugf(format string, args ...interface{})

Debugf 输出 DEBUG 级别的日志。

func (CtxEntry) Error

func (e CtxEntry) Error(errno ErrNo, args ...interface{})

Error 输出 ERROR 级别的日志。

func (CtxEntry) Errorf

func (e CtxEntry) Errorf(errno ErrNo, format string, args ...interface{})

Errorf 输出 ERROR 级别的日志。

func (CtxEntry) Fatal

func (e CtxEntry) Fatal(args ...interface{})

Fatal 输出 FATAL 级别的日志。

func (CtxEntry) Fatalf

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

Fatalf 输出 FATAL 级别的日志。

func (CtxEntry) GetContext

func (e CtxEntry) GetContext() context.Context

func (CtxEntry) GetErrNo

func (e CtxEntry) GetErrNo() ErrNo

func (CtxEntry) GetSkip

func (e CtxEntry) GetSkip() int

func (CtxEntry) GetTag

func (e CtxEntry) GetTag() string

func (CtxEntry) Info

func (e CtxEntry) Info(args ...interface{})

Info 输出 INFO 级别的日志。

func (CtxEntry) Infof

func (e CtxEntry) Infof(format string, args ...interface{})

Infof 输出 INFO 级别的日志。

func (CtxEntry) Panic

func (e CtxEntry) Panic(args ...interface{})

Panic 输出 PANIC 级别的日志。

func (CtxEntry) Panicf

func (e CtxEntry) Panicf(format string, args ...interface{})

Panicf 输出 PANIC 级别的日志。

func (CtxEntry) Skip

func (e CtxEntry) Skip(n int) CtxEntry

func (CtxEntry) Tag

func (e CtxEntry) Tag(tag string) CtxEntry

func (CtxEntry) Trace

func (e CtxEntry) Trace(args ...interface{})

Trace 输出 TRACE 级别的日志。

func (CtxEntry) Tracef

func (e CtxEntry) Tracef(format string, args ...interface{})

Tracef 输出 TRACE 级别的日志。

func (CtxEntry) Warn

func (e CtxEntry) Warn(args ...interface{})

Warn 输出 WARN 级别的日志。

func (CtxEntry) Warnf

func (e CtxEntry) Warnf(format string, args ...interface{})

Warnf 输出 WARN 级别的日志。

type Entry

type Entry interface {
	GetSkip() int
	GetTag() string
	GetContext() context.Context
	GetErrNo() ErrNo
}

type ErrNo

type ErrNo interface {
	Code() string
	Msg() string
}

func NewErrNo

func NewErrNo(prefix uint32, code uint16, msg string) ErrNo

type FuncOutput

type FuncOutput func(level Level, msg *Message)

FuncOutput 函数的形式自定义日志的输出格式。

func (FuncOutput) Do

func (fn FuncOutput) Do(level Level, msg *Message)

type Level

type Level uint32

Level 日志输出级别。

func GetLevel

func GetLevel() Level

GetLevel 获取日志的输出级别。

func (Level) String

func (level Level) String() string

type Message

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

Message 定义日志消息。

func (*Message) Args

func (msg *Message) Args() []interface{}

func (*Message) Ctx

func (msg *Message) Ctx() context.Context

func (*Message) ErrNo

func (msg *Message) ErrNo() ErrNo

func (*Message) File

func (msg *Message) File() string

func (*Message) Line

func (msg *Message) Line() int

func (*Message) Reuse

func (msg *Message) Reuse()

Reuse 将 *Message 放回对象池,以便重用。

func (*Message) Tag

func (msg *Message) Tag() string

func (*Message) Time

func (msg *Message) Time() time.Time

type MockOutput

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

MockOutput is a mock of Output interface.

func NewMockOutput

func NewMockOutput(ctrl *gomock.Controller) *MockOutput

NewMockOutput creates a new mock instance.

func (*MockOutput) Do

func (m *MockOutput) Do(level Level, msg *Message)

Do mocks base method.

func (*MockOutput) EXPECT

func (m *MockOutput) EXPECT() *MockOutputMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

type MockOutputMockRecorder

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

MockOutputMockRecorder is the mock recorder for MockOutput.

func (*MockOutputMockRecorder) Do

func (mr *MockOutputMockRecorder) Do(level, msg interface{}) *gomock.Call

Do indicates an expected call of Do.

type Output

type Output interface {
	Do(level Level, msg *Message)
}

Output 自定义日志的输出格式。

Jump to

Keyboard shortcuts

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