goo_log

package
v1.0.58 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2022 License: Apache-2.0 Imports: 12 Imported by: 5

README

日志对象

  • 默认
init() 初始化了默认日志对象 __log
  • 新建(基于控制台日志)
l := goo_log.New(goo_log.NewConsoleAdapter())
l.Debug("this is debug")
  • 新建(基于文件日志)
l := goo_log.New(goo_log.NewFileAdapter(
    goo_log.FileTagOption("sql"),
    goo_log.FileDirnameOption("logs-logs/"),
    goo_log.FileMaxSizeOption(100*1024*1024),
))
l.Debug("this is debug")

设置适配器

文件适配器 (默认)

  • 默认参数
goo_log.SetAdapter(goo_log.NewFileAdapter())
  • 自定义参数
goo_log.SetAdapter(goo_log.NewFileAdapter(
    goo_log.FileTagOption("sql"),
    goo_log.FileDirnameOption("logs-logs/"),
    goo_log.FileMaxSizeOption(100*1024*1024),
))

设置文件适配器参数

goo_log.SetFileAdapterOptions(
    goo_log.FileTagOption("sql"),
    goo_log.FileDirnameOption("logs-logs/"),
    goo_log.FileMaxSizeOption(100*1024*1024),
)

命令行适配器

goo_log.SetAdapter(goo_log.NewConsoleAdapter())

设置过滤路径

goo_log.SetTrimPath("/a/b/c")

添加钩子函数

goo_log.AddHook(func(msg goo_log.Message) {
    fmt.Println(msg.Level, msg.String()
})

添加日志字段

goo_log.WithField("name", "hnatao").Debug()

日志级别输出

goo_log.Debug("this is debug")
goo_log.Info("this is info")
goo_log.Warn("this is warn")
goo_log.Error("this is error")
goo_log.Panic("this is panic")
goo_log.Fatal("this is fatal")

Documentation

Index

Constants

View Source
const (
	FileTag     = "tag"
	FileDirname = "dirname"
	FileMaxSize = "max-size"
)

Variables

View Source
var (
	// 定义日志级别文本
	LevelText = map[Level]string{
		DEBUG: "DEBUG",
		INFO:  "INFO",
		WARN:  "WARN",
		ERROR: "ERROR",
		PANIC: "PANIC",
		FATAL: "FATAL",
	}
)

Functions

func AddHook

func AddHook(fn func(msg Message))

func Debug

func Debug(v ...interface{})

func DebugF added in v1.0.34

func DebugF(format string, v ...interface{})

func Error

func Error(v ...interface{})

func ErrorF added in v1.0.34

func ErrorF(format string, v ...interface{})

func Fatal

func Fatal(v ...interface{})

func FatalF added in v1.0.34

func FatalF(format string, v ...interface{})

func Info

func Info(v ...interface{})

func InfoF added in v1.0.34

func InfoF(format string, v ...interface{})

func Panic

func Panic(v ...interface{})

func PanicF added in v1.0.34

func PanicF(format string, v ...interface{})

func SetAdapter

func SetAdapter(adapter Adapter)

func SetFileAdapterOptions

func SetFileAdapterOptions(opts ...Option)

func SetTrimPath

func SetTrimPath(trimPaths ...string)

func Warn

func Warn(v ...interface{})

func WarnF added in v1.0.34

func WarnF(format string, v ...interface{})

Types

type Adapter

type Adapter interface {
	Write(msg *Message)
}

type ConsoleAdapter

type ConsoleAdapter struct {
}

控制台日志适配器

func NewConsoleAdapter

func NewConsoleAdapter() *ConsoleAdapter

func (ConsoleAdapter) Write

func (ca ConsoleAdapter) Write(msg *Message)

type Entry

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

func NewEntry

func NewEntry(l *Logger) *Entry

func WithField

func WithField(field string, value interface{}) *Entry

func WithTag added in v1.0.15

func WithTag(tags ...string) *Entry

func WithTrace added in v1.0.43

func WithTrace() *Entry

func (*Entry) Debug

func (entry *Entry) Debug(v ...interface{})

func (*Entry) DebugF added in v1.0.26

func (entry *Entry) DebugF(format string, v ...interface{})

func (*Entry) Error

func (entry *Entry) Error(v ...interface{})

func (*Entry) ErrorF added in v1.0.26

func (entry *Entry) ErrorF(format string, v ...interface{})

func (*Entry) Fatal

func (entry *Entry) Fatal(v ...interface{})

func (*Entry) FatalF added in v1.0.26

func (entry *Entry) FatalF(format string, v ...interface{})

func (*Entry) Info

func (entry *Entry) Info(v ...interface{})

func (*Entry) InfoF added in v1.0.26

func (entry *Entry) InfoF(format string, v ...interface{})

func (*Entry) Panic

func (entry *Entry) Panic(v ...interface{})

func (*Entry) PanicF added in v1.0.26

func (entry *Entry) PanicF(format string, v ...interface{})

func (*Entry) Warn

func (entry *Entry) Warn(v ...interface{})

func (*Entry) WarnF added in v1.0.26

func (entry *Entry) WarnF(format string, v ...interface{})

func (*Entry) WithField

func (entry *Entry) WithField(field string, value interface{}) *Entry

func (*Entry) WithTag added in v1.0.15

func (entry *Entry) WithTag(tags ...string) *Entry

func (*Entry) WithTrace added in v1.0.43

func (entry *Entry) WithTrace(args ...int) *Entry

type FileAdapter

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

文件日志适配器

func NewFileAdapter

func NewFileAdapter(opts ...Option) *FileAdapter

func (*FileAdapter) SetOptions

func (fa *FileAdapter) SetOptions(opts ...Option)

func (*FileAdapter) Write

func (fa *FileAdapter) Write(msg *Message)

type Level

type Level int
const (
	DEBUG Level = iota
	INFO
	WARN
	ERROR
	PANIC
	FATAL
)

定义日志级别

type Logger

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

func Default

func Default() *Logger

func New

func New(adapter Adapter) *Logger

func (*Logger) AddHook

func (l *Logger) AddHook(fn func(msg Message))

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

func (*Logger) DebugF added in v1.0.26

func (l *Logger) DebugF(format string, v ...interface{})

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

func (*Logger) ErrorF added in v1.0.26

func (l *Logger) ErrorF(format string, v ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

func (*Logger) FatalF added in v1.0.26

func (l *Logger) FatalF(format string, v ...interface{})

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

func (*Logger) InfoF added in v1.0.26

func (l *Logger) InfoF(format string, v ...interface{})

func (*Logger) Panic

func (l *Logger) Panic(v ...interface{})

func (*Logger) PanicF added in v1.0.26

func (l *Logger) PanicF(format string, v ...interface{})

func (*Logger) SetAdapter

func (l *Logger) SetAdapter(adapter Adapter)

func (*Logger) SetFileAdapterOptions

func (l *Logger) SetFileAdapterOptions(opts ...Option)

func (*Logger) SetTrimPath

func (l *Logger) SetTrimPath(trimPaths ...string)

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

func (*Logger) WarnF added in v1.0.26

func (l *Logger) WarnF(format string, v ...interface{})

func (*Logger) WithField

func (l *Logger) WithField(field string, value interface{}) *Entry

func (*Logger) WithTag added in v1.0.15

func (l *Logger) WithTag(tags ...string) *Entry

func (*Logger) WithTrace added in v1.0.43

func (l *Logger) WithTrace() *Entry

type Message

type Message struct {
	Level   Level
	Tags    []string
	Time    time.Time
	Content string
	Data    map[string]interface{}
}

func (*Message) JSON

func (msg *Message) JSON() []byte

func (*Message) String

func (msg *Message) String() string

func (*Message) WithField

func (msg *Message) WithField(field string, value interface{})

func (*Message) WithTag added in v1.0.15

func (msg *Message) WithTag(tags ...string)

type Option

type Option struct {
	Name  string
	Value interface{}
}

func FileDirnameOption

func FileDirnameOption(dirname string) Option

func FileMaxSizeOption

func FileMaxSizeOption(maxSize int64) Option

func FileTagOption

func FileTagOption(tag string) Option

func NewOption

func NewOption(name string, value interface{}) Option

Jump to

Keyboard shortcuts

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