2023-11-02 11:24:33 +01:00
|
|
|
package loggo
|
|
|
|
|
2023-11-02 12:37:06 +01:00
|
|
|
import (
|
2023-11-02 13:30:17 +01:00
|
|
|
"strings"
|
|
|
|
|
2023-11-02 12:37:06 +01:00
|
|
|
"github.com/fatih/color"
|
|
|
|
)
|
|
|
|
|
2023-11-02 11:24:33 +01:00
|
|
|
// struct for log levels
|
|
|
|
type logLevel struct {
|
|
|
|
level int
|
|
|
|
name string
|
2023-11-02 12:37:06 +01:00
|
|
|
color color.Color
|
2023-11-02 11:24:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// debug log level (level = 0, name = "debug")
|
2023-11-02 12:37:06 +01:00
|
|
|
var DebugLevel = logLevel{0, "debug", *color.New(color.FgCyan)}
|
2023-11-02 11:24:33 +01:00
|
|
|
|
|
|
|
// info log level (level = 1, name = "info")
|
2023-11-02 12:37:06 +01:00
|
|
|
var InfoLevel = logLevel{1, "info", *color.New(color.FgGreen)}
|
2023-11-02 11:24:33 +01:00
|
|
|
|
|
|
|
// warning log level (level = 2, name = "warning")
|
2023-11-02 12:37:06 +01:00
|
|
|
var WarningLevel = logLevel{2, "warning", *color.New(color.FgYellow)}
|
2023-11-02 11:24:33 +01:00
|
|
|
|
|
|
|
// error log level (level = 3, name = "error")
|
2023-11-02 12:37:06 +01:00
|
|
|
var ErrorLevel = logLevel{3, "error", *color.New(color.FgRed)}
|
2023-11-02 11:24:33 +01:00
|
|
|
|
|
|
|
// fatal log level (level = 4, name = "fatal")
|
2023-11-02 12:37:06 +01:00
|
|
|
var FatalLevel = logLevel{4, "fatal", *color.New(color.FgRed).Add(color.Bold)}
|
2023-11-02 11:24:33 +01:00
|
|
|
|
|
|
|
// current log level (defaults to: "infoLevel")
|
|
|
|
var currentLevel = InfoLevel
|
|
|
|
|
|
|
|
// return current set log level
|
|
|
|
func GetLogLevel() logLevel {
|
|
|
|
return currentLevel
|
|
|
|
}
|
|
|
|
|
|
|
|
// set current log level
|
|
|
|
func SetLogLevel(level logLevel) {
|
|
|
|
currentLevel = level
|
|
|
|
}
|
2023-11-02 12:37:06 +01:00
|
|
|
|
2023-11-02 13:30:17 +01:00
|
|
|
// set current log level by name
|
|
|
|
func SetLogLevelByName(name string) {
|
|
|
|
switch strings.ToLower(name) {
|
|
|
|
case DebugLevel.name:
|
|
|
|
SetLogLevel(DebugLevel)
|
|
|
|
case InfoLevel.name:
|
|
|
|
SetLogLevel(InfoLevel)
|
|
|
|
case WarningLevel.name:
|
|
|
|
SetLogLevel(WarningLevel)
|
|
|
|
case ErrorLevel.name:
|
|
|
|
SetLogLevel(ErrorLevel)
|
|
|
|
case FatalLevel.name:
|
|
|
|
SetLogLevel(FatalLevel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set current log level by level
|
|
|
|
func SetLogLevelByLevel(level int) {
|
|
|
|
switch level {
|
|
|
|
case DebugLevel.level:
|
|
|
|
SetLogLevel(DebugLevel)
|
|
|
|
case InfoLevel.level:
|
|
|
|
SetLogLevel(InfoLevel)
|
|
|
|
case WarningLevel.level:
|
|
|
|
SetLogLevel(WarningLevel)
|
|
|
|
case ErrorLevel.level:
|
|
|
|
SetLogLevel(ErrorLevel)
|
|
|
|
case FatalLevel.level:
|
|
|
|
SetLogLevel(FatalLevel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 12:37:06 +01:00
|
|
|
// set the color of a log level
|
|
|
|
func (level *logLevel) SetColor(color color.Color) {
|
|
|
|
level.color = color
|
|
|
|
}
|