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
Cyan = "\033[0;36m" // Cyan
White = "\033[0;37m" // White
Orange = "\033[38;5;208m" // Orange
// Bold
BBlack = "\033[1;30m" // Black
@@ -28,6 +29,7 @@ const (
BPurple = "\033[1;35m" // Purple
BCyan = "\033[1;36m" // Cyan
BWhite = "\033[1;37m" // White
BOrange = "\033[1;38;5;208m" // Bold Orange
// Underline
UBlack = "\033[4;30m" // Black
@@ -38,6 +40,7 @@ const (
UPurple = "\033[4;35m" // Purple
UCyan = "\033[4;36m" // Cyan
UWhite = "\033[4;37m" // White
UOrange = "\033[4;38;5;208m" // Underline Orange
// Background
On_Black = "\033[40m" // Black
@@ -48,6 +51,7 @@ const (
On_Purple = "\033[45m" // Purple
On_Cyan = "\033[46m" // Cyan
On_White = "\033[47m" // White
On_Orange = "\033[48;5;208m" // Orange Background
// High Intensty
IBlack = "\033[0;90m" // Black
@@ -58,6 +62,7 @@ const (
IPurple = "\033[0;95m" // Purple
ICyan = "\033[0;96m" // Cyan
IWhite = "\033[0;97m" // White
IOrange = "\033[0;38;5;208m" // Intense Orange
// Bold High Intensty
BIBlack = "\033[1;90m" // Black
@@ -68,6 +73,7 @@ const (
BIPurple = "\033[1;95m" // Purple
BICyan = "\033[1;96m" // Cyan
BIWhite = "\033[1;97m" // White
BIOrange = "\033[1;38;5;208m" // Bold Intense Orange
// High Intensty backgrounds
On_IBlack = "\033[0;100m" // Black
@@ -78,6 +84,7 @@ const (
On_IPurple = "\033[10;95m" // Purple
On_ICyan = "\033[0;106m" // Cyan
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

114
main.go
View File

@@ -39,38 +39,53 @@ const (
LevelPrefix
)
var levelNames = map[LogLevel]string{
LevelError: "ERROR",
LevelWarning: "WARNING",
LevelInfo: "INFO",
LevelDebug: "DEBUG",
LevelTrace: "TRACE",
LevelLua: "LUA",
LevelPrefix: "PREFIX",
// LevelStyle defines the visual style for a log level
type LevelStyle struct {
Tag string // e.g., "ERROR", "INFO"
TagColor string // ANSI code for tag text
TagBackgroundColor string // ANSI code for tag background
MessageColor string // ANSI code for message text
MessageBackgroundColor string // ANSI code for message background
}
var levelColors = map[LogLevel]string{
LevelError: BRed,
LevelWarning: BYellow,
LevelInfo: BGreen,
LevelDebug: BCyan,
LevelTrace: BPurple,
LevelLua: BBlue,
LevelPrefix: BIBlack,
// levelStyles maps LogLevel to its display style
var levelStyles = map[LogLevel]LevelStyle{
LevelError: {
Tag: "ERROR",
TagColor: BIRed, // Bold Intense Red
TagBackgroundColor: On_White, // White background
MessageColor: White, // Bold White text
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
type Logger struct {
mu sync.Mutex
@@ -116,8 +131,8 @@ func ParseLevel(levelStr string) LogLevel {
// String returns the string representation of the log level
func (l LogLevel) String() string {
if name, ok := levelNames[l]; ok {
return name
if name, ok := levelStyles[l]; ok {
return name.Tag
}
return fmt.Sprintf("Level(%d)", l)
}
@@ -319,22 +334,20 @@ func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{
fields = " " + strings.Join(pairs, " ")
}
var levelColor, originalBgColor, resetColor, tagFgColor, tagBgColor, messageFgColor, messageBgColor string
useSpecialFormatting := false // Flag for ERROR/WARNING levels
var tagFgColor, tagBgColor, messageBgColor string
useSpecialFormatting := false // Flag for levels with custom message background/foreground
if l.useColors {
resetColor = ResetColor
levelColor = levelColors[level] // Base color for the level text (e.g., BRed, BYellow)
if bg, ok := levelBackgroundColors[level]; ok { // Check if ERROR or WARNING has a background color defined
// Check if a message background color is defined for this level style
if levelStyles[level].MessageBackgroundColor != "" {
useSpecialFormatting = true
originalBgColor = bg // Store original background (e.g., On_IRed, On_IYellow)
tagFgColor = levelColor // Use original level color for tag text (BRed/BYellow)
tagBgColor = On_White // Use White background FOR THE TAG
messageFgColor = FgWhiteBold // Use Bold White text FOR THE MESSAGE PART
messageBgColor = originalBgColor // Use original background FOR THE MESSAGE PART
// Retrieve all style components from the map
tagFgColor = levelStyles[level].TagColor // Assign directly
tagBgColor = levelStyles[level].TagBackgroundColor
// messageFgColor = levelStyles[level].MessageColor
messageBgColor = levelStyles[level].MessageBackgroundColor
} else {
// For other levels (INFO, DEBUG, etc.)
tagFgColor = levelColor // Just the standard foreground color for the tag text
// For other levels (INFO, DEBUG, etc.), only TagColor is guaranteed
tagFgColor = levelStyles[level].TagColor // Use the defined tag color
// 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 ---
levelStr := levelNames[level]
levelStr := levelStyles[level].Tag
visibleTagContent := fmt.Sprintf("[%s]", levelStr)
visibleTagLen := len(visibleTagContent)
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
if useSpecialFormatting {
// 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 {
// 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
@@ -436,16 +449,16 @@ func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{
if useSpecialFormatting {
// ERROR/WARNING: Apply message background and foreground to User Prefix + Message
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(messageContent)
finalMsg.WriteString(resetColor) // Reset after message
finalMsg.WriteString(Reset)
} else {
// Other levels: User Prefix and Message content use default colors
// Apply specific color to user prefix if it exists
if l.userPrefix != "" {
prefixColor := levelColors[LevelPrefix]
finalMsg.WriteString(fmt.Sprintf("%s%s%s", prefixColor, userPrefixStr, resetColor))
prefixColor := levelStyles[LevelPrefix].TagColor
finalMsg.WriteString(fmt.Sprintf("%s%s%s", prefixColor, userPrefixStr, Reset))
} // No else needed, if userPrefix is empty, userPrefixStr is "" anyway
finalMsg.WriteString(messageContent) // Append message with default colors
}
@@ -644,4 +657,5 @@ func main() {
// Test with custom prefix
WithField("prefix", "custom").Info("Message with custom prefix")
Lua("This is a Lua message")
}