log

package
v1.1.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

README

log

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

日志级别

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

日志配置

var config = struct {
	mutex  sync.Mutex
	level  Level
	output Output
}
恢复默认的输出配置
func Reset()
设置日志的输出级别
func SetLevel(level Level)
设置日志的输出格式
func SetOutput(output Output)

标准输出

func Trace(args ...interface{})
func Tracef(format string, args ...interface{})
func Debug(args ...interface{})
func Debugf(format string, args ...interface{})
func Info(args ...interface{})
func Infof(format string, args ...interface{})
func Warn(args ...interface{})
func Warnf(format string, args ...interface{})
func Error(args ...interface{})
func Errorf(format string, args ...interface{})
func Panic(args ...interface{})
func Panicf(format string, args ...interface{})
func Fatal(args ...interface{})
func Fatalf(format string, args ...interface{})

自定义输出

func Tag(tag string) Entry
func Ctx(ctx context.Context) Entry
func (e Entry) Trace(args ...interface{})
func (e Entry) Tracef(format string, args ...interface{})
func (e Entry) Debug(args ...interface{})
func (e Entry) Debugf(format string, args ...interface{})
func (e Entry) Info(args ...interface{})
func (e Entry) Infof(format string, args ...interface{})
func (e Entry) Warn(args ...interface{})
func (e Entry) Warnf(format string, args ...interface{})
func (e Entry) Error(args ...interface{})
func (e Entry) Errorf(format string, args ...interface{})
func (e Entry) Panic(args ...interface{})
func (e Entry) Panicf(format string, args ...interface{})
func (e Entry) Fatal(args ...interface{})
func (e Entry) Fatalf(format string, args ...interface{})

自定义输出格式

type Output func(level Level, e *Entry)
func Console(level Level, e *Entry) {
	strLevel := strings.ToUpper(level.String())
	if level >= ErrorLevel {
		strLevel = console.Red.Sprint(strLevel)
	} else if level == WarnLevel {
		strLevel = console.Yellow.Sprint(strLevel)
	} else if level == TraceLevel {
		strLevel = console.Green.Sprint(strLevel)
	}
	_, _ = fmt.Printf("[%s] %s:%d %s\n", strLevel, e.file, e.line, e.msg)
}

参数延迟计算

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

Documentation

Overview

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

Index

Constants

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

Variables

This section is empty.

Functions

func Caller

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

Caller 获取调用栈的文件及行号信息,fast 为 true 时使用缓存进行加速。 基准测试表明获取调用栈的信息时使用缓存相比不使用缓存有 50% 的速度提升。

func Console

func Console(level Level, e *Entry)

Console 将日志输出到控制台。

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 Entry

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

Entry 打包日志信息。

func Ctx

func Ctx(ctx context.Context) Entry

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

func Tag

func Tag(tag string) Entry

Tag 创建包含 tag 信息的 Entry 。

func (Entry) Ctx

func (e Entry) Ctx(ctx context.Context) Entry

func (Entry) Debug added in v1.1.3

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

Debug 输出 DEBUG 级别的日志。

func (Entry) Debugf added in v1.1.3

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

Debugf 输出 DEBUG 级别的日志。

func (Entry) Error added in v1.1.3

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

Error 输出 ERROR 级别的日志。

func (Entry) Errorf added in v1.1.3

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

Errorf 输出 ERROR 级别的日志。

func (Entry) Fatal added in v1.1.3

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

Fatal 输出 FATAL 级别的日志。

func (Entry) Fatalf added in v1.1.3

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

Fatalf 输出 FATAL 级别的日志。

func (*Entry) GetCtx

func (e *Entry) GetCtx() context.Context

func (*Entry) GetFile

func (e *Entry) GetFile() string

func (*Entry) GetLine

func (e *Entry) GetLine() int

func (*Entry) GetMsg

func (e *Entry) GetMsg() string

func (*Entry) GetTag

func (e *Entry) GetTag() string

func (*Entry) GetTime

func (e *Entry) GetTime() time.Time

func (Entry) Info added in v1.1.3

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

Info 输出 INFO 级别的日志。

func (Entry) Infof added in v1.1.3

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

Infof 输出 INFO 级别的日志。

func (Entry) Panic added in v1.1.3

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

Panic 输出 PANIC 级别的日志。

func (Entry) Panicf added in v1.1.3

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

Panicf 输出 PANIC 级别的日志。

func (Entry) Tag

func (e Entry) Tag(tag string) Entry

func (Entry) Trace added in v1.1.3

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

Trace 输出 TRACE 级别的日志。

func (Entry) Tracef added in v1.1.3

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

Tracef 输出 TRACE 级别的日志。

func (Entry) Warn added in v1.1.3

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

Warn 输出 WARN 级别的日志。

func (Entry) Warnf added in v1.1.3

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

Warnf 输出 WARN 级别的日志。

type Level

type Level uint32

Level 日志输出级别。

func GetLevel

func GetLevel() Level

GetLevel 获取日志的输出级别。

func (Level) String

func (level Level) String() string

type Output

type Output func(level Level, e *Entry)

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

Jump to

Keyboard shortcuts

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