Make warnings yellow and refactor some shit to colors
This commit is contained in:
114
main.go
114
main.go
@@ -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")
|
||||
}
|
||||
|
Reference in New Issue
Block a user