From 76f335753956dd5cf678b64c4c1ab972ecac0357 Mon Sep 17 00:00:00 2001 From: RPJosh Date: Thu, 31 Oct 2024 10:01:54 +0100 Subject: [PATCH] Do not use [fmt.Sprintf] if no replacement arguments were provided --- {main => cmd}/main.go | 2 +- colors.go | 25 ++++++++++++------------- logger.go | 9 ++++++--- 3 files changed, 19 insertions(+), 17 deletions(-) rename {main => cmd}/main.go (92%) diff --git a/main/main.go b/cmd/main.go similarity index 92% rename from main/main.go rename to cmd/main.go index cebff9e..400834a 100755 --- a/main/main.go +++ b/cmd/main.go @@ -42,7 +42,7 @@ func main() { lOther.Log(logger.LevelDebug, "Greetings from your brother") logger.Info("It's a Me, Mario") - lOther.Log(logger.LevelError, "And im your brother luigi") + lOther.Log(logger.LevelError, "And im your brother %s", "luigi") // Create a copy of the logger lOtherCloned := logger.CloneLogger(lOther) diff --git a/colors.go b/colors.go index 3d83d17..aa52bac 100755 --- a/colors.go +++ b/colors.go @@ -1,7 +1,6 @@ package logger import ( - "fmt" "os" ) @@ -16,20 +15,20 @@ type colorConfig struct { // matching color. You can also specify replace values after the string // using printf. var ( - colPurple = color("\033[1;35m%s\033[0m") - colPurpleLight = color("\033[0;35m%s\033[0m") - colRed = color("\033[1;31m%s\033[0m") - colYellow = color("\033[1;33m%s\033[0m") - colBlue = color("\033[1;34m%s\033[0m") - colBlueLight = color("\033[0;34m%s\033[0m") - colCyan = color("\033[1;36m%s\033[0m") - colGreen = color("\033[0;32m%s\033[0m") + colPurple = color("\033[1;35m", "\033[0m") + colPurpleLight = color("\033[0;35m", "\033[0m") + colRed = color("\033[1;31m", "\033[0m") + colYellow = color("\033[1;33m", "\033[0m") + colBlue = color("\033[1;34m", "\033[0m") + colBlueLight = color("\033[0;34m", "\033[0m") + colCyan = color("\033[1;36m", "\033[0m") + colGreen = color("\033[0;32m", "\033[0m") ) // Color returns a function that pads the string with the given color code -func color(colorString string) func(str string, parameters ...any) string { - return func(str string, parameters ...any) string { - return fmt.Sprintf(colorString, fmt.Sprintf(str, parameters...)) +func color(code, termination string) func(str string) string { + return func(str string) string { + return code + str + termination } } @@ -54,7 +53,7 @@ func newColorConfig(enable bool) (conf *colorConfig) { } // getColor returns the matching color for the level -func (l Level) getColor() func(str string, parameters ...any) string { +func (l Level) getColor() func(str string) string { switch l { case LevelTrace: return colPurpleLight diff --git a/logger.go b/logger.go index 05854c6..86ec754 100755 --- a/logger.go +++ b/logger.go @@ -125,14 +125,17 @@ func (l *Logger) log(level Level, message string, parameters ...any) { var levelName = fmt.Sprintf("%-5s", level) // Build the message to print - printMessage := fmt.Sprintf(message, parameters...) + printMessage := message + if len(parameters) > 0 { + printMessage = fmt.Sprintf(message, parameters...) + } if !l.OnlyPrintMessage { printMessage = "[" + levelName + "] " + time.Now().Local().Format("2006-01-02 15:04:05") + getSourceMessage(file, line, pc, l) + l.Prefix + " - " + printMessage } // Build the colored message to print - printMessageColored := l.getColored(fmt.Sprintf(message, parameters...), level.getColor()) + printMessageColored := l.getColored(printMessage, level.getColor()) if !l.OnlyPrintMessage { printMessageColored = l.getColored("["+levelName+"] ", level.getColor()) + @@ -159,7 +162,7 @@ func (l *Logger) log(level Level, message string, parameters ...any) { } // getColored returns a message padded by with a color code if coloring is supported and specified -func (l *Logger) getColored(message string, color func(str string, parameters ...any) string) string { +func (l *Logger) getColored(message string, color func(str string) string) string { if l.colorConf.enableColors { return color(message) }