stuve-it-backend/logger/main.go

48 lines
1.0 KiB
Go
Raw Normal View History

2023-10-26 20:34:59 +00:00
/*
Package logger provides a simple logger for the application.
*/
package logger
import (
"fmt"
"github.com/fatih/color"
"log"
"strings"
)
// LogSuccessF LogSuccess logs a success message to stdout
func LogSuccessF(format string, a ...interface{}) {
logger("success", format, a...)
}
// LogInfoF LogInfo logs an info message to stdout
func LogInfoF(format string, a ...interface{}) {
logger("info", format, a...)
}
// LogErrorF LogError logs an error message to stdout
func LogErrorF(format string, a ...interface{}) {
logger("error", format, a...)
}
// logger logger is a helper function to log messages to stdout
func logger(level string, format string, a ...interface{}) {
var c *color.Color
switch level {
case "success":
c = color.New(color.Bold).Add(color.FgGreen)
case "info":
c = color.New(color.FgBlue)
case "error":
c = color.New(color.FgRed)
}
date := new(strings.Builder)
log.New(date, "", log.LstdFlags).Print()
c.Printf("%s - %s\n",
strings.TrimSpace(date.String()),
fmt.Sprintf(format, a...),
)
}