logora

package
v0.0.0-...-48de36b Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FieldKeyMsg         = "msg"
	FieldKeyLevel       = "level"
	FieldKeyTime        = "time"
	FieldKeyLogrusError = "logrus_error"
	FieldKeyFunc        = "func"
	FieldKeyFile        = "file"
)

Variables

View Source
var (
	ErrorKey = "error"
)

Functions

func DeferExitHandler

func DeferExitHandler(handler func())

DeferExitHandler prepends a Logrus Exit handler to the list of handlers, call logrus.Exit to invoke all handlers. The handlers will also be invoked when any Fatal log entry is made.

This method is useful when a caller wishes to use logrus to log a fatal message but also needs to gracefully shutdown. An example usecase could be closing database connections, or sending a alert that the application is closing.

func Exit

func Exit(code int)

Exit runs all the Logrus atexit handlers and then terminates the program using os.Exit(code)

func RegisterExitHandler

func RegisterExitHandler(handler func())

RegisterExitHandler appends a Logrus Exit handler to the list of handlers, call logrus.Exit to invoke all handlers. The handlers will also be invoked when any Fatal log entry is made.

This method is useful when a caller wishes to use logrus to log a fatal message but also needs to gracefully shutdown. An example usecase could be closing database connections, or sending a alert that the application is closing.

func SetBufferPool

func SetBufferPool(bp BufferPool)

Types

type BufferPool

type BufferPool interface {
	Put(*bytes.Buffer) error
	Get() (*bytes.Buffer, error)
}

type DefaultBufferPool

type DefaultBufferPool struct {
	Pool *sync.Pool
}

func (*DefaultBufferPool) Get

func (dp *DefaultBufferPool) Get() (*bytes.Buffer, error)

func (*DefaultBufferPool) Put

func (dp *DefaultBufferPool) Put(b *bytes.Buffer) error

type DefaultFormatter

type DefaultFormatter struct {
}

func (*DefaultFormatter) Format

func (df *DefaultFormatter) Format(entry *Entry)

type Entry

type Entry struct {
	Logger *Logger
	Data   Fields
	Time   time.Time
	Level  Level
	// 栈帧
	Caller *runtime.Frame

	// 用于简单输出
	Message string
	// 用于格式化输出
	Buffer  *bytes.Buffer
	Context context.Context
	// contains filtered or unexported fields
}

func NewEntry

func NewEntry(logger *Logger) *Entry

工厂方法

func (*Entry) Bytes

func (entry *Entry) Bytes() ([]byte, error)

func (*Entry) Debug

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

func (*Entry) Debugf

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

func (*Entry) Debugln

func (entry *Entry) Debugln(args ...interface{})

func (*Entry) Dup

func (entry *Entry) Dup() *Entry

func (*Entry) Error

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

func (*Entry) Errorf

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

func (*Entry) Errorln

func (entry *Entry) Errorln(args ...interface{})

func (*Entry) Fatal

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

func (*Entry) Fatalf

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

func (*Entry) Fatalln

func (entry *Entry) Fatalln(args ...interface{})

func (*Entry) HasCaller

func (entry *Entry) HasCaller() bool

func (*Entry) Info

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

func (*Entry) Infof

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

func (*Entry) Infoln

func (entry *Entry) Infoln(args ...interface{})

func (*Entry) Log

func (entry *Entry) Log(level Level, args ...interface{})

func (*Entry) Logf

func (entry *Entry) Logf(level Level, format string, args ...interface{})

func (*Entry) Logln

func (entry *Entry) Logln(level Level, args ...interface{})

func (*Entry) Panic

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

func (*Entry) Panicf

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

func (*Entry) Panicln

func (entry *Entry) Panicln(args ...interface{})

func (*Entry) Print

func (entry *Entry) Print(args ...interface{})

func (*Entry) Printf

func (entry *Entry) Printf(format string, args ...interface{})

func (*Entry) Println

func (entry *Entry) Println(args ...interface{})

func (*Entry) String

func (entry *Entry) String() (string, error)

func (*Entry) Trace

func (entry *Entry) Trace(args ...interface{})

func (*Entry) Tracef

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

func (*Entry) Traceln

func (entry *Entry) Traceln(args ...interface{})

func (*Entry) Warn

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

func (*Entry) Warnf

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

func (*Entry) Warning

func (entry *Entry) Warning(args ...interface{})

func (*Entry) Warningf

func (entry *Entry) Warningf(format string, args ...interface{})

func (*Entry) Warningln

func (entry *Entry) Warningln(args ...interface{})

func (*Entry) Warnln

func (entry *Entry) Warnln(args ...interface{})

func (*Entry) WithContext

func (entry *Entry) WithContext(context context.Context) *Entry

// 用于简单输出 Message string // 用于格式化输出 Buffer *bytes.Buffer Context context.Context err string

func (*Entry) WithError

func (entry *Entry) WithError(err error) *Entry

func (*Entry) WithField

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

func (*Entry) WithFields

func (entry *Entry) WithFields(fields Fields) *Entry

func (*Entry) WithTime

func (entry *Entry) WithTime(time time.Time) *Entry

type ExitFunc

type ExitFunc func(code int)

type FieldLogger

type FieldLogger interface {
	WithField(key string, value interface{}) *Entry
	WithFields(fields Fields) *Entry
	WithError(err error) *Entry

	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Printf(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Panicf(format string, args ...interface{})

	Debug(args ...interface{})
	Info(args ...interface{})
	Print(args ...interface{})
	Warn(args ...interface{})
	Warning(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})

	Debugln(args ...interface{})
	Infoln(args ...interface{})
	Println(args ...interface{})
	Warnln(args ...interface{})
	Warningln(args ...interface{})
	Errorln(args ...interface{})
	Fatalln(args ...interface{})
	Panicln(args ...interface{})
}

type FieldMap

type FieldMap map[fieldKey]string

FieldMap allows customization of the key names for default fields.

type Fields

type Fields map[string]interface{}

type Formatter

type Formatter interface {
	Format(entry *Entry) ([]byte, error)
}

格式化接口

type Hook

type Hook interface {
	Levels() []Level
	Fire(entry *Entry) error
}

type JSONFormatter

type JSONFormatter struct {
	// TimestampFormat sets the format used for marshaling timestamps.
	// The format to use is the same than for time.Format or time.Parse from the standard
	// library.
	// The standard Library already provides a set of predefined format.
	TimestampFormat string

	// DisableTimestamp allows disabling automatic timestamps in output
	DisableTimestamp bool

	// DisableHTMLEscape allows disabling html escaping in output
	DisableHTMLEscape bool

	// DataKey allows users to put all the log entry parameters into a nested dictionary at a given key.
	DataKey string

	// FieldMap allows users to customize the names of keys for default fields.
	// As an example:
	// formatter := &JSONFormatter{
	//   	FieldMap: FieldMap{
	// 		 FieldKeyTime:  "@timestamp",
	// 		 FieldKeyLevel: "@level",
	// 		 FieldKeyMsg:   "@message",
	// 		 FieldKeyFunc:  "@caller",
	//    },
	// }
	FieldMap FieldMap

	// CallerPrettyfier can be set by the user to modify the content
	// of the function and file keys in the json data when ReportCaller is
	// activated. If any of the returned value is the empty string the
	// corresponding key will be removed from json fields.
	CallerPrettyfier func(*runtime.Frame) (function string, file string)

	// PrettyPrint will indent all json logs
	PrettyPrint bool
}

JSONFormatter formats logs into parsable json

func (*JSONFormatter) Format

func (f *JSONFormatter) Format(entry *Entry) ([]byte, error)

Format renders a single log entry

type Level

type Level uint32
const (
	// PanicLevel level, highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	PanicLevel Level = iota
	// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
	// logging level is set to Panic.
	FatalLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
	// TraceLevel level. Designates finer-grained informational events than the Debug.
	TraceLevel
)

func (Level) MarshalText

func (level Level) MarshalText() ([]byte, error)

func (Level) String

func (level Level) String() string

type LevelHook

type LevelHook map[Level][]Hook

func (LevelHook) AddHook

func (hooks LevelHook) AddHook(hook Hook)

func (LevelHook) Fire

func (hooks LevelHook) Fire(level Level, entry *Entry) error

type LogFunction

type LogFunction func() []interface{}

type Logger

type Logger struct {
	// 输出地址
	Out       io.Writer
	Hooks     LevelHook
	Formatter Formatter
	// 是否上报调用者
	ReportCaller bool
	// 当前日志登记,过滤作用
	Level Level

	ExitFunc   ExitFunc
	BufferPool BufferPool
	// contains filtered or unexported fields
}

func (*Logger) AddHook

func (logger *Logger) AddHook(hook Hook)

func (*Logger) AddHooks

func (logger *Logger) AddHooks(hooks ...Hook)

2-3个参数这样更容易写

func (*Logger) AddHooksWithSlice

func (logger *Logger) AddHooksWithSlice(hooks []Hook)

func (*Logger) Debug

func (logger *Logger) Debug(args ...interface{})

func (*Logger) DebugFn

func (logger *Logger) DebugFn(fn LogFunction)

func (*Logger) Debugf

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

func (*Logger) Debugln

func (logger *Logger) Debugln(args ...interface{})

func (*Logger) Error

func (logger *Logger) Error(args ...interface{})

func (*Logger) ErrorFn

func (logger *Logger) ErrorFn(fn LogFunction)

func (*Logger) Errorf

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

func (*Logger) Errorln

func (logger *Logger) Errorln(args ...interface{})

func (*Logger) Exit

func (logger *Logger) Exit(code int)

func (*Logger) Fatal

func (logger *Logger) Fatal(args ...interface{})

func (*Logger) FatalFn

func (logger *Logger) FatalFn(fn LogFunction)

func (*Logger) Fatalf

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

func (*Logger) Fatalln

func (logger *Logger) Fatalln(args ...interface{})

func (*Logger) Info

func (logger *Logger) Info(args ...interface{})

func (*Logger) InfoFn

func (logger *Logger) InfoFn(fn LogFunction)

func (*Logger) Infof

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

func (*Logger) Infoln

func (logger *Logger) Infoln(args ...interface{})

func (*Logger) IsLevelEnable

func (logger *Logger) IsLevelEnable(level Level) bool

func (*Logger) LoadLoggerLevel

func (logger *Logger) LoadLoggerLevel() Level

func (*Logger) Log

func (logger *Logger) Log(level Level, args ...interface{})

func (*Logger) LogFn

func (logger *Logger) LogFn(level Level, fn LogFunction)

func (*Logger) Logf

func (logger *Logger) Logf(level Level, format string, args ...interface{})

func (*Logger) Logln

func (logger *Logger) Logln(level Level, args ...interface{})

func (*Logger) NewEntry

func (logger *Logger) NewEntry() *Entry

func (*Logger) Panic

func (logger *Logger) Panic(args ...interface{})

func (*Logger) PanicFn

func (logger *Logger) PanicFn(fn LogFunction)

func (*Logger) Panicf

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

func (*Logger) Panicln

func (logger *Logger) Panicln(args ...interface{})

func (*Logger) Print

func (logger *Logger) Print(args ...interface{})

func (*Logger) PrintFn

func (logger *Logger) PrintFn(fn LogFunction)

func (*Logger) Printf

func (logger *Logger) Printf(format string, args ...interface{})

func (*Logger) Println

func (logger *Logger) Println(args ...interface{})

func (*Logger) ReplaceHooks

func (logger *Logger) ReplaceHooks(hooks LevelHook)

func (*Logger) SetBufferPool

func (logger *Logger) SetBufferPool(bp BufferPool)

func (*Logger) SetFormatter

func (logger *Logger) SetFormatter(formatter Formatter)

func (*Logger) SetLoggerLevel

func (logger *Logger) SetLoggerLevel(level Level)

func (*Logger) SetNoLock

func (logger *Logger) SetNoLock()

设置logora的工作模式

func (*Logger) SetOutPut

func (logger *Logger) SetOutPut(out io.Writer)

func (*Logger) SetReportCaller

func (logger *Logger) SetReportCaller(reportCaller bool)

func (*Logger) Trace

func (logger *Logger) Trace(args ...interface{})

func (*Logger) TraceFn

func (logger *Logger) TraceFn(fn LogFunction)

func (*Logger) Tracef

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

func (*Logger) Traceln

func (logger *Logger) Traceln(args ...interface{})

func (*Logger) Warn

func (logger *Logger) Warn(args ...interface{})

func (*Logger) WarnFn

func (logger *Logger) WarnFn(fn LogFunction)

func (*Logger) Warnf

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

func (*Logger) Warning

func (logger *Logger) Warning(args ...interface{})

func (*Logger) WarningFn

func (logger *Logger) WarningFn(fn LogFunction)

func (*Logger) Warningf

func (logger *Logger) Warningf(format string, args ...interface{})

func (*Logger) Warningln

func (logger *Logger) Warningln(args ...interface{})

func (*Logger) Warnln

func (logger *Logger) Warnln(args ...interface{})

func (*Logger) WithContext

func (logger *Logger) WithContext(context context.Context) *Entry

func (*Logger) WithError

func (logger *Logger) WithError(err error) *Entry

func (*Logger) WithField

func (logger *Logger) WithField(key string, value interface{}) *Entry

func (*Logger) WithFields

func (logger *Logger) WithFields(fields Fields) *Entry

func (*Logger) WithTime

func (logger *Logger) WithTime(time time.Time) *Entry

type Mutex

type Mutex interface {
	Lock()
	Unlock()
}

type MutexWrap

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

func NewMutexWrap

func NewMutexWrap() *MutexWrap

工厂方式创建一个MutexWrap

func (*MutexWrap) Disabled

func (mw *MutexWrap) Disabled()

func (*MutexWrap) Enabled

func (mw *MutexWrap) Enabled()

func (*MutexWrap) Lock

func (mw *MutexWrap) Lock()

func (*MutexWrap) Unlock

func (mw *MutexWrap) Unlock()

type StdLogger

type StdLogger interface {
	Print(...interface{})
	Printf(string, ...interface{})
	Println(...interface{})

	Fatal(...interface{})
	Fatalf(string, ...interface{})
	Fatalln(...interface{})

	Panic(...interface{})
	Panicf(string, ...interface{})
	Panicln(...interface{})
}

简单定义一下自己的StdLogger接口类型 只要和打印相关的basic功能

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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