mirror of
https://github.com/spf13/viper
synced 2025-05-07 20:57:18 +00:00
Add name field for viper and logger.
This commit is contained in:
parent
027007221d
commit
de37278785
2 changed files with 34 additions and 7 deletions
26
logger.go
26
logger.go
|
@ -38,30 +38,44 @@ type Logger interface {
|
|||
// Loggers commonly provide Fatal and Panic levels above Error level,
|
||||
// but exiting and panicing is out of scope for a logging library.
|
||||
Error(msg string, keyvals ...interface{})
|
||||
|
||||
// SetName sets a name for logger, the logger name will be the prefix of all the log entry.
|
||||
SetName(string)
|
||||
}
|
||||
|
||||
type jwwLogger struct{}
|
||||
type jwwLogger struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (jwwLogger) Trace(msg string, keyvals ...interface{}) {
|
||||
func (j *jwwLogger) Trace(msg string, keyvals ...interface{}) {
|
||||
msg = fmt.Sprintf("%s | %s", j.name, msg)
|
||||
jww.TRACE.Printf(jwwLogMessage(msg, keyvals...))
|
||||
}
|
||||
|
||||
func (jwwLogger) Debug(msg string, keyvals ...interface{}) {
|
||||
func (j *jwwLogger) Debug(msg string, keyvals ...interface{}) {
|
||||
msg = fmt.Sprintf("%s | %s", j.name, msg)
|
||||
jww.DEBUG.Printf(jwwLogMessage(msg, keyvals...))
|
||||
}
|
||||
|
||||
func (jwwLogger) Info(msg string, keyvals ...interface{}) {
|
||||
func (j *jwwLogger) Info(msg string, keyvals ...interface{}) {
|
||||
msg = fmt.Sprintf("%s | %s", j.name, msg)
|
||||
jww.INFO.Printf(jwwLogMessage(msg, keyvals...))
|
||||
}
|
||||
|
||||
func (jwwLogger) Warn(msg string, keyvals ...interface{}) {
|
||||
func (j *jwwLogger) Warn(msg string, keyvals ...interface{}) {
|
||||
msg = fmt.Sprintf("%s | %s", j.name, msg)
|
||||
jww.WARN.Printf(jwwLogMessage(msg, keyvals...))
|
||||
}
|
||||
|
||||
func (jwwLogger) Error(msg string, keyvals ...interface{}) {
|
||||
func (j *jwwLogger) Error(msg string, keyvals ...interface{}) {
|
||||
msg = fmt.Sprintf("%s | %s", j.name, msg)
|
||||
jww.ERROR.Printf(jwwLogMessage(msg, keyvals...))
|
||||
}
|
||||
|
||||
func (j *jwwLogger) SetName(name string) {
|
||||
j.name = name
|
||||
}
|
||||
|
||||
func jwwLogMessage(msg string, keyvals ...interface{}) string {
|
||||
out := msg
|
||||
|
||||
|
|
15
viper.go
15
viper.go
|
@ -180,6 +180,10 @@ func DecodeHook(hook mapstructure.DecodeHookFunc) DecoderConfigOption {
|
|||
//
|
||||
// Note: Vipers are not safe for concurrent Get() and Set() operations.
|
||||
type Viper struct {
|
||||
|
||||
// Will use as both the viper name and the logger name, will be printed as the prefix for each log entry.
|
||||
name string
|
||||
|
||||
// Delimiter that separates a list of keys
|
||||
// used to access a nested value in one go
|
||||
keyDelim string
|
||||
|
@ -231,6 +235,7 @@ type Viper struct {
|
|||
// New returns an initialized Viper instance.
|
||||
func New() *Viper {
|
||||
v := new(Viper)
|
||||
v.name = "default"
|
||||
v.keyDelim = "."
|
||||
v.configName = "config"
|
||||
v.configPermissions = os.FileMode(0o644)
|
||||
|
@ -244,7 +249,7 @@ func New() *Viper {
|
|||
v.env = make(map[string][]string)
|
||||
v.aliases = make(map[string]string)
|
||||
v.typeByDefValue = false
|
||||
v.logger = jwwLogger{}
|
||||
v.logger = &jwwLogger{name: v.name}
|
||||
v.registered = make(map[string]RegisteredConfig)
|
||||
|
||||
v.resetEncoding()
|
||||
|
@ -642,6 +647,14 @@ func (v *Viper) AddConfigPath(in string) {
|
|||
}
|
||||
}
|
||||
|
||||
// SetName Updates the name of a Viper object.
|
||||
func SetName(name string) { v.SetName(name) }
|
||||
|
||||
func (v *Viper) SetName(name string) {
|
||||
v.name = name
|
||||
v.logger.SetName(name)
|
||||
}
|
||||
|
||||
// AddRemoteProvider adds a remote configuration source.
|
||||
// Remote Providers are searched in the order they are added.
|
||||
// provider is a string value: "etcd", "etcd3", "consul" or "firestore" are currently supported.
|
||||
|
|
Loading…
Add table
Reference in a new issue