Make warnings yellow and refactor some shit to colors

This commit is contained in:
2025-04-22 10:29:40 +02:00
parent 04f9b0db6e
commit d9564c2b98
2 changed files with 71 additions and 50 deletions

View File

@@ -18,6 +18,7 @@ const (
Purple = "\033[0;35m" // Purple Purple = "\033[0;35m" // Purple
Cyan = "\033[0;36m" // Cyan Cyan = "\033[0;36m" // Cyan
White = "\033[0;37m" // White White = "\033[0;37m" // White
Orange = "\033[38;5;208m" // Orange
// Bold // Bold
BBlack = "\033[1;30m" // Black BBlack = "\033[1;30m" // Black
@@ -28,6 +29,7 @@ const (
BPurple = "\033[1;35m" // Purple BPurple = "\033[1;35m" // Purple
BCyan = "\033[1;36m" // Cyan BCyan = "\033[1;36m" // Cyan
BWhite = "\033[1;37m" // White BWhite = "\033[1;37m" // White
BOrange = "\033[1;38;5;208m" // Bold Orange
// Underline // Underline
UBlack = "\033[4;30m" // Black UBlack = "\033[4;30m" // Black
@@ -38,6 +40,7 @@ const (
UPurple = "\033[4;35m" // Purple UPurple = "\033[4;35m" // Purple
UCyan = "\033[4;36m" // Cyan UCyan = "\033[4;36m" // Cyan
UWhite = "\033[4;37m" // White UWhite = "\033[4;37m" // White
UOrange = "\033[4;38;5;208m" // Underline Orange
// Background // Background
On_Black = "\033[40m" // Black On_Black = "\033[40m" // Black
@@ -48,6 +51,7 @@ const (
On_Purple = "\033[45m" // Purple On_Purple = "\033[45m" // Purple
On_Cyan = "\033[46m" // Cyan On_Cyan = "\033[46m" // Cyan
On_White = "\033[47m" // White On_White = "\033[47m" // White
On_Orange = "\033[48;5;208m" // Orange Background
// High Intensty // High Intensty
IBlack = "\033[0;90m" // Black IBlack = "\033[0;90m" // Black
@@ -58,6 +62,7 @@ const (
IPurple = "\033[0;95m" // Purple IPurple = "\033[0;95m" // Purple
ICyan = "\033[0;96m" // Cyan ICyan = "\033[0;96m" // Cyan
IWhite = "\033[0;97m" // White IWhite = "\033[0;97m" // White
IOrange = "\033[0;38;5;208m" // Intense Orange
// Bold High Intensty // Bold High Intensty
BIBlack = "\033[1;90m" // Black BIBlack = "\033[1;90m" // Black
@@ -68,6 +73,7 @@ const (
BIPurple = "\033[1;95m" // Purple BIPurple = "\033[1;95m" // Purple
BICyan = "\033[1;96m" // Cyan BICyan = "\033[1;96m" // Cyan
BIWhite = "\033[1;97m" // White BIWhite = "\033[1;97m" // White
BIOrange = "\033[1;38;5;208m" // Bold Intense Orange
// High Intensty backgrounds // High Intensty backgrounds
On_IBlack = "\033[0;100m" // Black On_IBlack = "\033[0;100m" // Black
@@ -78,6 +84,7 @@ const (
On_IPurple = "\033[10;95m" // Purple On_IPurple = "\033[10;95m" // Purple
On_ICyan = "\033[0;106m" // Cyan On_ICyan = "\033[0;106m" // Cyan
On_IWhite = "\033[0;107m" // White On_IWhite = "\033[0;107m" // White
On_IOrange = "\033[0;48;5;208m" // Intense Orange Background
) )
// The acceptable range is [16, 231] but here we remove some very dark colors // The acceptable range is [16, 231] but here we remove some very dark colors

114
main.go
View File

@@ -39,38 +39,53 @@ const (
LevelPrefix LevelPrefix
) )
var levelNames = map[LogLevel]string{ // LevelStyle defines the visual style for a log level
LevelError: "ERROR", type LevelStyle struct {
LevelWarning: "WARNING", Tag string // e.g., "ERROR", "INFO"
LevelInfo: "INFO", TagColor string // ANSI code for tag text
LevelDebug: "DEBUG", TagBackgroundColor string // ANSI code for tag background
LevelTrace: "TRACE", MessageColor string // ANSI code for message text
LevelLua: "LUA", MessageBackgroundColor string // ANSI code for message background
LevelPrefix: "PREFIX",
} }
var levelColors = map[LogLevel]string{ // levelStyles maps LogLevel to its display style
LevelError: BRed, var levelStyles = map[LogLevel]LevelStyle{
LevelWarning: BYellow, LevelError: {
LevelInfo: BGreen, Tag: "ERROR",
LevelDebug: BCyan, TagColor: BIRed, // Bold Intense Red
LevelTrace: BPurple, TagBackgroundColor: On_White, // White background
LevelLua: BBlue, MessageColor: White, // Bold White text
LevelPrefix: BIBlack, MessageBackgroundColor: On_IRed, // Intense Red background
},
LevelWarning: {
Tag: "WARNING",
TagColor: BIOrange, // Bold Intense Orange
TagBackgroundColor: On_White, // White background
MessageColor: White, // Bold White text
MessageBackgroundColor: On_IOrange, // Intense Orange background
},
LevelInfo: {
Tag: "INFO",
TagColor: BGreen, // Bold Green
},
LevelDebug: {
Tag: "DEBUG",
TagColor: BCyan, // Bold Cyan
},
LevelTrace: {
Tag: "TRACE",
TagColor: BPurple, // Bold Purple
},
LevelLua: {
Tag: "LUA",
TagColor: BBlue, // Bold Blue
},
LevelPrefix: {
Tag: "PREFIX", // Used for coloring the user prefix
TagColor: BIBlack, // Bold Intense Black (Dark Grey)
},
} }
// ANSI Background Colors
var levelBackgroundColors = map[LogLevel]string{
LevelError: On_IRed,
LevelWarning: On_IYellow,
}
// ANSI Foreground Colors (adjusting for readability on backgrounds)
const FgWhiteBold = "\033[1;37m"
// ResetColor is the ANSI code to reset text color
const ResetColor = "\033[0m"
// Logger is our custom logger with level support // Logger is our custom logger with level support
type Logger struct { type Logger struct {
mu sync.Mutex mu sync.Mutex
@@ -116,8 +131,8 @@ func ParseLevel(levelStr string) LogLevel {
// String returns the string representation of the log level // String returns the string representation of the log level
func (l LogLevel) String() string { func (l LogLevel) String() string {
if name, ok := levelNames[l]; ok { if name, ok := levelStyles[l]; ok {
return name return name.Tag
} }
return fmt.Sprintf("Level(%d)", l) return fmt.Sprintf("Level(%d)", l)
} }
@@ -319,22 +334,20 @@ func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{
fields = " " + strings.Join(pairs, " ") fields = " " + strings.Join(pairs, " ")
} }
var levelColor, originalBgColor, resetColor, tagFgColor, tagBgColor, messageFgColor, messageBgColor string var tagFgColor, tagBgColor, messageBgColor string
useSpecialFormatting := false // Flag for ERROR/WARNING levels useSpecialFormatting := false // Flag for levels with custom message background/foreground
if l.useColors { if l.useColors {
resetColor = ResetColor // Check if a message background color is defined for this level style
levelColor = levelColors[level] // Base color for the level text (e.g., BRed, BYellow) if levelStyles[level].MessageBackgroundColor != "" {
if bg, ok := levelBackgroundColors[level]; ok { // Check if ERROR or WARNING has a background color defined
useSpecialFormatting = true useSpecialFormatting = true
originalBgColor = bg // Store original background (e.g., On_IRed, On_IYellow) // Retrieve all style components from the map
tagFgColor = levelColor // Use original level color for tag text (BRed/BYellow) tagFgColor = levelStyles[level].TagColor // Assign directly
tagBgColor = On_White // Use White background FOR THE TAG tagBgColor = levelStyles[level].TagBackgroundColor
messageFgColor = FgWhiteBold // Use Bold White text FOR THE MESSAGE PART // messageFgColor = levelStyles[level].MessageColor
messageBgColor = originalBgColor // Use original background FOR THE MESSAGE PART messageBgColor = levelStyles[level].MessageBackgroundColor
} else { } else {
// For other levels (INFO, DEBUG, etc.) // For other levels (INFO, DEBUG, etc.), only TagColor is guaranteed
tagFgColor = levelColor // Just the standard foreground color for the tag text tagFgColor = levelStyles[level].TagColor // Use the defined tag color
// tagBgColor, messageFgColor, messageBgColor remain empty (use terminal defaults) // tagBgColor, messageFgColor, messageBgColor remain empty (use terminal defaults)
} }
} }
@@ -390,7 +403,7 @@ func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{
} }
// --- Level Tag Formatting and Padding --- // --- Level Tag Formatting and Padding ---
levelStr := levelNames[level] levelStr := levelStyles[level].Tag
visibleTagContent := fmt.Sprintf("[%s]", levelStr) visibleTagContent := fmt.Sprintf("[%s]", levelStr)
visibleTagLen := len(visibleTagContent) visibleTagLen := len(visibleTagContent)
paddingWidth := 10 // Target width for the level column (tag + padding) paddingWidth := 10 // Target width for the level column (tag + padding)
@@ -403,10 +416,10 @@ func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{
var levelTagFormatted string var levelTagFormatted string
if useSpecialFormatting { if useSpecialFormatting {
// ERROR/WARNING: Tag has specific background and foreground // ERROR/WARNING: Tag has specific background and foreground
levelTagFormatted = fmt.Sprintf("%s%s%s%s", tagBgColor, tagFgColor, visibleTagContent, resetColor) levelTagFormatted = fmt.Sprintf("%s%s%s%s", tagBgColor, tagFgColor, visibleTagContent, Reset)
} else { } else {
// Other levels: Tag has standard foreground color only // Other levels: Tag has standard foreground color only
levelTagFormatted = fmt.Sprintf("%s%s%s", tagFgColor, visibleTagContent, resetColor) levelTagFormatted = fmt.Sprintf("%s%s%s", tagFgColor, visibleTagContent, Reset)
} }
levelColumn := levelTagFormatted + padding // Combine formatted tag and padding levelColumn := levelTagFormatted + padding // Combine formatted tag and padding
@@ -436,16 +449,16 @@ func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{
if useSpecialFormatting { if useSpecialFormatting {
// ERROR/WARNING: Apply message background and foreground to User Prefix + Message // ERROR/WARNING: Apply message background and foreground to User Prefix + Message
finalMsg.WriteString(messageBgColor) finalMsg.WriteString(messageBgColor)
finalMsg.WriteString(messageFgColor) // finalMsg.WriteString(messageFgColor) // This doesn't work...? For some reason?
finalMsg.WriteString(userPrefixStr) // Write user prefix inside the colored block finalMsg.WriteString(userPrefixStr) // Write user prefix inside the colored block
finalMsg.WriteString(messageContent) finalMsg.WriteString(messageContent)
finalMsg.WriteString(resetColor) // Reset after message finalMsg.WriteString(Reset)
} else { } else {
// Other levels: User Prefix and Message content use default colors // Other levels: User Prefix and Message content use default colors
// Apply specific color to user prefix if it exists // Apply specific color to user prefix if it exists
if l.userPrefix != "" { if l.userPrefix != "" {
prefixColor := levelColors[LevelPrefix] prefixColor := levelStyles[LevelPrefix].TagColor
finalMsg.WriteString(fmt.Sprintf("%s%s%s", prefixColor, userPrefixStr, resetColor)) finalMsg.WriteString(fmt.Sprintf("%s%s%s", prefixColor, userPrefixStr, Reset))
} // No else needed, if userPrefix is empty, userPrefixStr is "" anyway } // No else needed, if userPrefix is empty, userPrefixStr is "" anyway
finalMsg.WriteString(messageContent) // Append message with default colors finalMsg.WriteString(messageContent) // Append message with default colors
} }
@@ -644,4 +657,5 @@ func main() {
// Test with custom prefix // Test with custom prefix
WithField("prefix", "custom").Info("Message with custom prefix") WithField("prefix", "custom").Info("Message with custom prefix")
Lua("This is a Lua message")
} }