Do not use [fmt.Sprintf] if no replacement arguments were provided

main v1.3.4
Jonas Letzbor 2024-10-31 10:01:54 +01:00
parent 994105e94e
commit 76f3357539
Signed by: RPJosh
GPG Key ID: 43ACB900522EA740
3 changed files with 19 additions and 17 deletions

View File

@ -42,7 +42,7 @@ func main() {
lOther.Log(logger.LevelDebug, "Greetings from your brother") lOther.Log(logger.LevelDebug, "Greetings from your brother")
logger.Info("It's a Me, Mario") 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 // Create a copy of the logger
lOtherCloned := logger.CloneLogger(lOther) lOtherCloned := logger.CloneLogger(lOther)

View File

@ -1,7 +1,6 @@
package logger package logger
import ( import (
"fmt"
"os" "os"
) )
@ -16,20 +15,20 @@ type colorConfig struct {
// matching color. You can also specify replace values after the string // matching color. You can also specify replace values after the string
// using printf. // using printf.
var ( var (
colPurple = color("\033[1;35m%s\033[0m") colPurple = color("\033[1;35m", "\033[0m")
colPurpleLight = color("\033[0;35m%s\033[0m") colPurpleLight = color("\033[0;35m", "\033[0m")
colRed = color("\033[1;31m%s\033[0m") colRed = color("\033[1;31m", "\033[0m")
colYellow = color("\033[1;33m%s\033[0m") colYellow = color("\033[1;33m", "\033[0m")
colBlue = color("\033[1;34m%s\033[0m") colBlue = color("\033[1;34m", "\033[0m")
colBlueLight = color("\033[0;34m%s\033[0m") colBlueLight = color("\033[0;34m", "\033[0m")
colCyan = color("\033[1;36m%s\033[0m") colCyan = color("\033[1;36m", "\033[0m")
colGreen = color("\033[0;32m%s\033[0m") colGreen = color("\033[0;32m", "\033[0m")
) )
// Color returns a function that pads the string with the given color code // Color returns a function that pads the string with the given color code
func color(colorString string) func(str string, parameters ...any) string { func color(code, termination string) func(str string) string {
return func(str string, parameters ...any) string { return func(str string) string {
return fmt.Sprintf(colorString, fmt.Sprintf(str, parameters...)) return code + str + termination
} }
} }
@ -54,7 +53,7 @@ func newColorConfig(enable bool) (conf *colorConfig) {
} }
// getColor returns the matching color for the level // 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 { switch l {
case LevelTrace: case LevelTrace:
return colPurpleLight return colPurpleLight

View File

@ -125,14 +125,17 @@ func (l *Logger) log(level Level, message string, parameters ...any) {
var levelName = fmt.Sprintf("%-5s", level) var levelName = fmt.Sprintf("%-5s", level)
// Build the message to print // Build the message to print
printMessage := fmt.Sprintf(message, parameters...) printMessage := message
if len(parameters) > 0 {
printMessage = fmt.Sprintf(message, parameters...)
}
if !l.OnlyPrintMessage { if !l.OnlyPrintMessage {
printMessage = "[" + levelName + "] " + time.Now().Local().Format("2006-01-02 15:04:05") + printMessage = "[" + levelName + "] " + time.Now().Local().Format("2006-01-02 15:04:05") +
getSourceMessage(file, line, pc, l) + l.Prefix + " - " + printMessage getSourceMessage(file, line, pc, l) + l.Prefix + " - " + printMessage
} }
// Build the colored message to print // Build the colored message to print
printMessageColored := l.getColored(fmt.Sprintf(message, parameters...), level.getColor()) printMessageColored := l.getColored(printMessage, level.getColor())
if !l.OnlyPrintMessage { if !l.OnlyPrintMessage {
printMessageColored = printMessageColored =
l.getColored("["+levelName+"] ", level.getColor()) + 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 // 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 { if l.colorConf.enableColors {
return color(message) return color(message)
} }