Skip to content

Commit

Permalink
Improve logging: Add colored symbols for info, warning, and error mes…
Browse files Browse the repository at this point in the history
…sages

- Use green check marks (✓) for info messages
- Use yellow exclamation marks (!) for warning messages
- Use red crosses (✗) for error messages
- Keep message text white for better readability
  • Loading branch information
stcrestrada committed May 28, 2024
1 parent 32ba9b9 commit cbfa2cc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func main() {
if err != nil {
switch err.(type) {
case *util.InfoError:
util.Info("Information: %s", err.Error())
util.Info("%s", err.Error())
case *util.WarningError:
util.Warning("Warning: %s", err.Error())
util.Warning("%s", err.Error())
default:
util.CheckIfError(err)
}
Expand Down
28 changes: 14 additions & 14 deletions pkg/util/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,39 @@ func NewInfo(message string) error {
}
}

// logMessage is a helper function to print messages with specific color attributes
func logMessage(colorAttribute color.Attribute, format string, args ...interface{}) {
c := color.New(colorAttribute, color.Bold)
c.Printf("%s\n", fmt.Sprintf(format, args...))
// logMessage is a helper function to print messages with specific color attributes and symbols
func logMessage(symbolColor color.Attribute, symbol string, messageColor color.Attribute, format string, args ...interface{}) {
symbolColored := color.New(symbolColor, color.Bold).Sprintf(symbol)
messageColored := color.New(messageColor, color.Bold).Sprintf(format, args...)
fmt.Printf("%s %s\n", symbolColored, messageColored)
}

// Info logs an info message with green text and bold style
// Info logs an info message with a green check mark and white message
func Info(format string, args ...interface{}) {
logMessage(color.FgGreen, format, args...)
logMessage(color.FgGreen, "✓", color.FgWhite, format, args...)
}

// VerboseLog logs a verbose message if verbose logging is enabled
func VerboseLog(format string, args ...interface{}) {
if Verbose {
logMessage(color.FgCyan, format, args...)
logMessage(color.FgCyan, "✓", color.FgWhite, format, args...)
}
}

// CheckIfError logs an error message and panics if the error is not nil
// CheckIfError logs an error message with a red cross and panics if the error is not nil
func CheckIfError(err error) {
if err == nil {
return
}
logMessage(color.FgHiRed, "error: %s", err)
//panic(err)
logMessage(color.FgHiRed, "✗", color.FgWhite, "error: %s", err)
}

// Warning logs a warning message with yellow text and bold style
// Warning logs a warning message with a yellow exclamation mark and white message
func Warning(format string, args ...interface{}) {
logMessage(color.FgYellow, format, args...)
logMessage(color.FgYellow, "!", color.FgWhite, format, args...)
}

// WarningRed logs a warning message with red text and bold style
// WarningRed logs a warning message with a red exclamation mark and white message
func WarningRed(format string, args ...interface{}) {
logMessage(color.FgRed, format, args...)
logMessage(color.FgRed, "✗", color.FgWhite, format, args...)
}

0 comments on commit cbfa2cc

Please sign in to comment.