1 Star 0 Fork 0

sy_183 / go-common

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
options.go 4.04 KB
一键复制 编辑 原始数据 按行查看 历史
sy_183 提交于 2022-10-30 19:18 . Initial commit
package log
import (
"fmt"
)
// An Option configures a Logger.
type Option interface {
apply(*Logger)
}
// optionFunc wraps a func, so it satisfies the Option interface.
type optionFunc func(*Logger)
func (f optionFunc) apply(log *Logger) {
f(log)
}
// WrapCore wraps or replaces the Logger's underlying Core.
func WrapCore(f func(Core) Core) Option {
return optionFunc(func(log *Logger) {
log.core = f(log.core)
})
}
func WithName(name string) Option {
return optionFunc(func(log *Logger) {
log.name = name
})
}
func AddSubName(name string) Option {
return optionFunc(func(log *Logger) {
if log.name == "" {
log.name = name
} else {
log.name = log.name + "." + name
}
})
}
// Hooks registers functions which will be called each time the Logger writes
// out an Entry. Repeated use of Hooks is additive.
//
// Hooks are useful for simple side effects, like capturing metrics for the
// number of emitted logs. More complex side effects, including anything that
// requires access to the Entry's structured fields, should be implemented as
// a Core instead. See RegisterHooks for details.
func Hooks(hooks ...func(Entry) error) Option {
return optionFunc(func(log *Logger) {
log.core = RegisterHooks(log.core, hooks...)
})
}
// Fields adds fields to the Logger.
func Fields(fs ...Field) Option {
return optionFunc(func(log *Logger) {
log.core = log.core.With(fs)
})
}
// ErrorOutput sets the destination for errors generated by the Logger. Note
// that this option only affects internal errors; for sample code that sends
// error-level logs to a different location from info- and debug-level logs,
// see the package-level AdvancedConfiguration example.
//
// The supplied WriteSyncer must be safe for concurrent use. The Open and
// Lock functions are the simplest ways to protect files with a mutex.
func ErrorOutput(w WriteSyncer) Option {
return optionFunc(func(log *Logger) {
log.errorOutput = w
})
}
// Development puts the logger in development mode, which makes DPanic-level
// logs panic instead of simply logging an error.
func Development() Option {
return optionFunc(func(log *Logger) {
log.development = true
})
}
// AddCaller configures the Logger to annotate each message with the filename,
// line number, and function name of log's caller. See also WithCaller.
func AddCaller() Option {
return WithCaller(true)
}
// WithCaller configures the Logger to annotate each message with the filename,
// line number, and function name of log's caller, or not, depending on the
// value of enabled. This is a generalized form of AddCaller.
func WithCaller(enabled bool) Option {
return optionFunc(func(log *Logger) {
log.addCaller = enabled
})
}
// AddCallerSkip increases the number of callers skipped by caller annotation
// (as enabled by the AddCaller option). When building wrappers around the
// Logger and SugaredLogger, supplying this Option prevents log from always
// reporting the wrapper code as the caller.
func AddCallerSkip(skip int) Option {
return optionFunc(func(log *Logger) {
log.callerSkip += skip
})
}
// AddStacktrace configures the Logger to record a stack trace for all messages at
// or above a given level.
func AddStacktrace(lvl LevelEnabler) Option {
return optionFunc(func(log *Logger) {
log.addStack = lvl
})
}
// IncreaseLevel increase the level of the logger. It has no effect if
// the passed in level tries to decrease the level of the logger.
func IncreaseLevel(lvl LevelEnabler) Option {
return optionFunc(func(log *Logger) {
core, err := NewIncreaseLevelCore(log.core, lvl)
if err != nil {
fmt.Fprintf(log.errorOutput, "failed to IncreaseLevel: %v\n", err)
} else {
log.core = core
}
})
}
// OnFatal sets the action to take on fatal logs.
func OnFatal(action CheckWriteAction) Option {
return optionFunc(func(log *Logger) {
log.onFatal = action
})
}
// WithClock specifies the clock used by the logger to determine the current
// time for logged entries. Defaults to the system clock with time.Now.
func WithClock(clock Clock) Option {
return optionFunc(func(log *Logger) {
log.clock = clock
})
}
1
https://gitee.com/sy_183/go-common.git
git@gitee.com:sy_183/go-common.git
sy_183
go-common
go-common
v1.0.4

搜索帮助