From de37278785d4b89c80af69bea087c48d8f9b2aad Mon Sep 17 00:00:00 2001 From: cyprinus Date: Sat, 17 Jun 2023 23:31:29 +0800 Subject: [PATCH] Add name field for viper and logger. --- logger.go | 26 ++++++++++++++++++++------ viper.go | 15 ++++++++++++++- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/logger.go b/logger.go index a64e144..252407c 100644 --- a/logger.go +++ b/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 diff --git a/viper.go b/viper.go index 259328c..aa8436a 100644 --- a/viper.go +++ b/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.