mirror of
				https://github.com/nabbar/golib.git
				synced 2025-10-31 19:12:35 +08:00 
			
		
		
		
	Fix linter
This commit is contained in:
		| @@ -35,36 +35,40 @@ import ( | ||||
| 	"github.com/spf13/jwalterweatherman" | ||||
| ) | ||||
|  | ||||
| //Level a uint8 type customized with function to log message with the current log level | ||||
| //Level a uint8 type customized with function to log message with the current log level. | ||||
| type Level uint8 | ||||
|  | ||||
| const ( | ||||
| 	// PanicLevel Panic level for entry log, will result on a Panic() call (trace + fatal) | ||||
| 	// PanicLevel Panic level for entry log, will result on a Panic() call (trace + fatal). | ||||
| 	PanicLevel Level = iota | ||||
| 	// FatalLevel Fatal level for entry log, will result on os.Exit with error | ||||
| 	// FatalLevel Fatal level for entry log, will result on os.Exit with error. | ||||
| 	FatalLevel | ||||
| 	// ErrorLevel Error level for entry log who's meaning the caller stop his process and return to the pre caller | ||||
| 	// ErrorLevel Error level for entry log who's meaning the caller stop his process and return to the pre caller. | ||||
| 	ErrorLevel | ||||
| 	// WarnLevel Warning level for entry log who's meaning the caller don't stop his process and try to continue it | ||||
| 	// WarnLevel Warning level for entry log who's meaning the caller don't stop his process and try to continue it. | ||||
| 	WarnLevel | ||||
| 	// InfoLevel Info level for entry log who's meaning it is just an information who's have no impact on caller's process but can be useful to inform human of a state, event, success, ... | ||||
| 	InfoLevel | ||||
| 	// DebugLevel Debug level for entry log who's meaning the caller has no problem and the information is only useful to identify a potential problem who's can arrive later | ||||
| 	// DebugLevel Debug level for entry log who's meaning the caller has no problem and the information is only useful to identify a potential problem who's can arrive later. | ||||
| 	DebugLevel | ||||
| 	// NilLevel Nil level will never log anything and is used to completely disable current log entry. It cannot be used in the SetLogLevel function | ||||
| 	// NilLevel Nil level will never log anything and is used to completely disable current log entry. It cannot be used in the SetLogLevel function. | ||||
| 	NilLevel | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	curLevel = InfoLevel | ||||
| 	curLevel = NilLevel | ||||
| ) | ||||
|  | ||||
| //GetCurrentLevel return the current loglevel setting in the logger. All log entry matching this level or below will be logged | ||||
| func init() { | ||||
| 	SetLevel(InfoLevel) | ||||
| } | ||||
|  | ||||
| //GetCurrentLevel return the current loglevel setting in the logger. All log entry matching this level or below will be logged. | ||||
| func GetCurrentLevel() Level { | ||||
| 	return curLevel | ||||
| } | ||||
|  | ||||
| // GetLevelListString return a list ([]string) of all string loglevel available | ||||
| // GetLevelListString return a list ([]string) of all string loglevel available. | ||||
| func GetLevelListString() []string { | ||||
| 	return []string{ | ||||
| 		strings.ToLower(PanicLevel.String()), | ||||
| @@ -76,14 +80,15 @@ func GetLevelListString() []string { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // SetLevel Change the Level of all log entry with the Level type given in parameter. The change is apply for next log entry only | ||||
| // | ||||
| // SetLevel Change the Level of all log entry with the Level type given in parameter. The change is apply for next log entry only. | ||||
| // If the given Level type is not matching a correct Level type, no change will be apply. | ||||
| /* | ||||
| 	level a Level type to use to specify the new level of logger message | ||||
| */ | ||||
| func SetLevel(level Level) { | ||||
| 	//nolint exhaustive | ||||
| 	switch level { | ||||
|  | ||||
| 	case PanicLevel: | ||||
| 		curLevel = PanicLevel | ||||
| 		logrus.SetLevel(logrus.PanicLevel) | ||||
| @@ -107,6 +112,9 @@ func SetLevel(level Level) { | ||||
| 	case DebugLevel: | ||||
| 		curLevel = DebugLevel | ||||
| 		logrus.SetLevel(logrus.DebugLevel) | ||||
|  | ||||
| 	case NilLevel: | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	DebugLevel.Logf("Change Log Level to %s", logrus.GetLevel().String()) | ||||
| @@ -126,6 +134,7 @@ func setViperLogTrace() { | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	//nolint exhaustive | ||||
| 	switch curLevel { | ||||
| 	case PanicLevel: | ||||
| 		jwalterweatherman.SetStdoutThreshold(jwalterweatherman.LevelCritical) | ||||
| @@ -144,10 +153,13 @@ func setViperLogTrace() { | ||||
|  | ||||
| 	case DebugLevel: | ||||
| 		jwalterweatherman.SetStdoutThreshold(jwalterweatherman.LevelDebug) | ||||
|  | ||||
| 	case NilLevel: | ||||
| 		return | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // GetLevelString return a valid Level Type matching the given string parameter. If the given parameter don't represent a valid level, the InfoLevel will be return | ||||
| // GetLevelString return a valid Level Type matching the given string parameter. If the given parameter don't represent a valid level, the InfoLevel will be return. | ||||
| /* | ||||
| 	level the string representation of a Level type | ||||
| */ | ||||
| @@ -165,12 +177,14 @@ func GetLevelString(level string) Level { | ||||
| 	case strings.ToLower(WarnLevel.String()): | ||||
| 		return WarnLevel | ||||
|  | ||||
| 	case strings.ToLower(InfoLevel.String()): | ||||
| 		return InfoLevel | ||||
|  | ||||
| 	case strings.ToLower(DebugLevel.String()): | ||||
| 		return DebugLevel | ||||
|  | ||||
| 	default: | ||||
| 		return InfoLevel | ||||
| 	} | ||||
|  | ||||
| 	return InfoLevel | ||||
| } | ||||
|  | ||||
| // Uint8 Convert the current Level type to a uint8 value. E.g. FatalLevel becomes 1. | ||||
| @@ -180,6 +194,7 @@ func (level Level) Uint8() uint8 { | ||||
|  | ||||
| // String Convert the current Level type to a string. E.g. PanicLevel becomes "Critical Error". | ||||
| func (level Level) String() string { | ||||
| 	//nolint exhaustive | ||||
| 	switch level { | ||||
| 	case DebugLevel: | ||||
| 		return "Debug" | ||||
| @@ -193,12 +208,14 @@ func (level Level) String() string { | ||||
| 		return "Fatal Error" | ||||
| 	case PanicLevel: | ||||
| 		return "Critical Error" | ||||
| 	case NilLevel: | ||||
| 		return "" | ||||
| 	} | ||||
|  | ||||
| 	return "unknown" | ||||
| } | ||||
|  | ||||
| // Log Simple function to log directly the given message with the attached log Level | ||||
| // Log Simple function to log directly the given message with the attached log Level. | ||||
| /* | ||||
| 	message a string message to be logged with the attached log Level | ||||
| */ | ||||
| @@ -206,7 +223,7 @@ func (level Level) Log(message string) { | ||||
| 	level.logDetails(message, nil, nil, nil) | ||||
| } | ||||
|  | ||||
| // Logf Simple function to log (to the attached log Level) with a fmt function a given pattern and arguments in parameters | ||||
| // Logf Simple function to log (to the attached log Level) with a fmt function a given pattern and arguments in parameters. | ||||
| /* | ||||
| 	format a string pattern for fmt function | ||||
| 	args a list of interface to match the references in the pattern | ||||
| @@ -215,7 +232,7 @@ func (level Level) Logf(format string, args ...interface{}) { | ||||
| 	level.logDetails(fmt.Sprintf(format, args...), nil, nil, nil) | ||||
| } | ||||
|  | ||||
| // LogData Simple function to log directly the given message with given data with the attached log Level | ||||
| // LogData Simple function to log directly the given message with given data with the attached log Level. | ||||
| /* | ||||
| 	message a string message to be logged with the attached log Level | ||||
| 	data an interface of data to be logged with the message. (In Text format, the data will be json marshaled) | ||||
| @@ -224,7 +241,7 @@ func (level Level) LogData(message string, data interface{}) { | ||||
| 	level.logDetails(message, data, nil, nil) | ||||
| } | ||||
|  | ||||
| // WithFields Simple function to log directly the given message with given fields with the attached log Level | ||||
| // WithFields Simple function to log directly the given message with given fields with the attached log Level. | ||||
| /* | ||||
| 	message a string message to be logged with the attached log Level | ||||
| 	fields a map of string key and interfaces value for a complete list of field ("field name" => value interface) | ||||
| @@ -248,7 +265,7 @@ func (level Level) LogError(err error) bool { | ||||
| 	return level.LogGinErrorCtx(NilLevel, "", err, nil) | ||||
| } | ||||
|  | ||||
| // LogErrorCtx Function to test, log and inform about the given error object | ||||
| // LogErrorCtx Function to test, log and inform about the given error object. | ||||
| // | ||||
| // How iot works : | ||||
| //  + when the err is a valid error, this function will : | ||||
| @@ -266,7 +283,7 @@ func (level Level) LogErrorCtx(levelElse Level, context string, err error) bool | ||||
| 	return level.LogGinErrorCtx(levelElse, context, err, nil) | ||||
| } | ||||
|  | ||||
| // LogErrorCtxf Function to test, log and inform about the given error object, but with a context based on a pattern and matching args | ||||
| // LogErrorCtxf Function to test, log and inform about the given error object, but with a context based on a pattern and matching args. | ||||
| // | ||||
| // How iot works : | ||||
| //  + when the err is a valid error, this function will : | ||||
| @@ -307,7 +324,7 @@ func (level Level) LogGinErrorCtxf(levelElse Level, contextPattern string, err e | ||||
| 	return level.LogGinErrorCtx(levelElse, fmt.Sprintf(contextPattern, args...), err, c) | ||||
| } | ||||
|  | ||||
| // LogGinErrorCtx Function to test, log and inform about the given error object | ||||
| // LogGinErrorCtx Function to test, log and inform about the given error object. | ||||
| // This function will also add an Gin Tonic Error if the c parameters is a valid GinTonic Context reference. | ||||
| // | ||||
| // How iot works : | ||||
| @@ -322,7 +339,7 @@ func (level Level) LogGinErrorCtxf(levelElse Level, contextPattern string, err e | ||||
| levelElse level used if the err is nil before returning a False result | ||||
| context a string for the context of the current test of the error | ||||
| err a error object to be log with the attached log level before return true, if the err is nil, the levelElse is used to log there are no error and return false | ||||
| c a valid Go GinTonic Context reference to add current error to the Gin Tonic Error Context | ||||
| c a valid Go GinTonic Context reference to add current error to the Gin Tonic Error Context. | ||||
| */ | ||||
| func (level Level) LogGinErrorCtx(levelElse Level, context string, err error, c *gin.Context) bool { | ||||
| 	if err != nil { | ||||
| @@ -373,6 +390,7 @@ func (level Level) logDetails(message string, data interface{}, err error, field | ||||
| 		ent.WithFields(fields) | ||||
| 	} | ||||
|  | ||||
| 	//nolint exhaustive | ||||
| 	switch curFormat { | ||||
| 	case TextFormat: | ||||
| 		if _, ok := tags[tagStack]; ok { | ||||
| @@ -409,9 +427,16 @@ func (level Level) logDetails(message string, data interface{}, err error, field | ||||
| 	case JsonFormat: | ||||
| 		ent.WithFields(tags) | ||||
| 		msg = tags[tagMsg].(string) | ||||
|  | ||||
| 	case nilFormat: | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	//nolint exhaustive | ||||
| 	switch level { | ||||
| 	case NilLevel: | ||||
| 		return | ||||
|  | ||||
| 	case DebugLevel: | ||||
| 		ent.Debugln(msg) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Nicolas JUHEL
					Nicolas JUHEL