Compare commits

..

10 commits

6 changed files with 86 additions and 20 deletions

View file

@ -1,5 +1,5 @@
# loggo # loggo
a simple log module written in golang. a simple log module written in golang
`go get https://git.velvettear.de/velvettear/loggo.git` `go get git.velvettear.de/velvettear/loggo`

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,32 @@
package loggo package loggo
import (
"strings"
"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 +40,40 @@ func GetLogLevel() logLevel {
func SetLogLevel(level logLevel) { func SetLogLevel(level logLevel) {
currentLevel = level currentLevel = level
} }
// 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)
}
}
// 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"
@ -68,17 +67,21 @@ func print(logLevel logLevel, message string, timestamp int64, extras ...string)
formatted = strings.ReplaceAll(formatted, PLACEHOLDER_TIMESTAMP, now.Format(GetDateFormat())) formatted = strings.ReplaceAll(formatted, PLACEHOLDER_TIMESTAMP, now.Format(GetDateFormat()))
formatted = strings.ReplaceAll(formatted, PLACEHOLDER_LOGLEVEL, logLevel.name) formatted = strings.ReplaceAll(formatted, PLACEHOLDER_LOGLEVEL, logLevel.name)
formatted = strings.ReplaceAll(formatted, PLACEHOLDER_MESSAGE, message) formatted = strings.ReplaceAll(formatted, PLACEHOLDER_MESSAGE, message)
tmp := "" if len(extras) == 0 {
if len(extras) > 0 { formatted = strings.ReplaceAll(formatted, GetExtrasFormat(), "")
tmp = strings.Join(extras, GetExtrasSeparator()) formatted = strings.TrimSpace(formatted)
} else {
format := GetExtrasFormat()
formatted = strings.ReplaceAll(formatted, format, strings.ReplaceAll(format, PLACEHOLDER_EXTRAS, strings.Join(extras, GetExtrasSeparator())))
} }
formatted = strings.ReplaceAll(formatted, GetExtrasFormat(), tmp) if timestamp <= 0 {
tmp = "" formatted = strings.ReplaceAll(formatted, GetTimediffFormat(), "")
if timestamp >= 0 { formatted = strings.TrimSpace(formatted)
tmp = strconv.FormatInt(now.UnixMilli()-timestamp, 10) + "ms" } else {
format := GetTimediffFormat()
formatted = strings.ReplaceAll(formatted, format, strings.ReplaceAll(format, PLACEHOLDER_TIMEDIFF, strconv.FormatInt(now.UnixMilli()-timestamp, 10)+"ms"))
} }
formatted = strings.ReplaceAll(formatted, GetTimediffFormat(), tmp) logLevel.color.Println(strings.TrimSpace(formatted))
fmt.Println(formatted)
if logLevel.level == FatalLevel.level { if logLevel.level == FatalLevel.level {
os.Exit(1) os.Exit(1)
} }