added color support

This commit is contained in:
Daniel Sommer 2023-11-02 12:37:06 +01:00
parent e6bf61483b
commit 8e3cf9378d
4 changed files with 36 additions and 8 deletions

8
go.mod
View file

@ -1,3 +1,11 @@
module git.velvettear.de/velvettear/loggo module git.velvettear.de/velvettear/loggo
go 1.21 go 1.21
require github.com/fatih/color v1.15.0
require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.13.0 // indirect
)

11
go.sum Normal file
View file

@ -0,0 +1,11 @@
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View file

@ -1,25 +1,30 @@
package loggo package loggo
import (
"github.com/fatih/color"
)
// struct for log levels // struct for log levels
type logLevel struct { type logLevel struct {
level int level int
name string name string
color color.Color
} }
// debug log level (level = 0, name = "debug") // debug log level (level = 0, name = "debug")
var DebugLevel = logLevel{0, "debug"} var DebugLevel = logLevel{0, "debug", *color.New(color.FgCyan)}
// info log level (level = 1, name = "info") // info log level (level = 1, name = "info")
var InfoLevel = logLevel{1, "info"} var InfoLevel = logLevel{1, "info", *color.New(color.FgGreen)}
// warning log level (level = 2, name = "warning") // warning log level (level = 2, name = "warning")
var WarningLevel = logLevel{2, "warning"} var WarningLevel = logLevel{2, "warning", *color.New(color.FgYellow)}
// error log level (level = 3, name = "error") // error log level (level = 3, name = "error")
var ErrorLevel = logLevel{3, "error"} var ErrorLevel = logLevel{3, "error", *color.New(color.FgRed)}
// fatal log level (level = 4, name = "fatal") // fatal log level (level = 4, name = "fatal")
var FatalLevel = logLevel{4, "fatal"} var FatalLevel = logLevel{4, "fatal", *color.New(color.FgRed).Add(color.Bold)}
// current log level (defaults to: "infoLevel") // current log level (defaults to: "infoLevel")
var currentLevel = InfoLevel var currentLevel = InfoLevel
@ -33,3 +38,8 @@ func GetLogLevel() logLevel {
func SetLogLevel(level logLevel) { func SetLogLevel(level logLevel) {
currentLevel = level currentLevel = level
} }
// set the color of a log level
func (level *logLevel) SetColor(color color.Color) {
level.color = color
}

View file

@ -1,7 +1,6 @@
package loggo package loggo
import ( import (
"fmt"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -80,7 +79,7 @@ func print(logLevel logLevel, message string, timestamp int64, extras ...string)
format := GetTimediffFormat() format := GetTimediffFormat()
formatted = strings.ReplaceAll(formatted, format, strings.ReplaceAll(format, PLACEHOLDER_TIMEDIFF, strconv.FormatInt(now.UnixMilli()-timestamp, 10)+"ms")) formatted = strings.ReplaceAll(formatted, format, strings.ReplaceAll(format, PLACEHOLDER_TIMEDIFF, strconv.FormatInt(now.UnixMilli()-timestamp, 10)+"ms"))
} }
fmt.Println(strings.TrimSpace(formatted)) logLevel.color.Println(strings.TrimSpace(formatted))
if logLevel.level == FatalLevel.level { if logLevel.level == FatalLevel.level {
os.Exit(1) os.Exit(1)
} }