Compare commits
10 commits
583d9aabf2
...
668cdf1aa5
Author | SHA1 | Date | |
---|---|---|---|
668cdf1aa5 | |||
48c056db23 | |||
980a00b4e0 | |||
cb89138842 | |||
0b5381f82e | |||
20a4d89240 | |||
8e3cf9378d | |||
e6bf61483b | |||
a8ccd953bb | |||
1f1ec29f7c |
6 changed files with 86 additions and 20 deletions
|
@ -1,5 +1,5 @@
|
|||
# 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
8
go.mod
|
@ -1,3 +1,11 @@
|
|||
module git.velvettear.de/velvettear/loggo
|
||||
|
||||
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
11
go.sum
Normal 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=
|
54
loglevel.go
54
loglevel.go
|
@ -1,25 +1,32 @@
|
|||
package loggo
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
// struct for log levels
|
||||
type logLevel struct {
|
||||
level int
|
||||
name string
|
||||
color color.Color
|
||||
}
|
||||
|
||||
// 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")
|
||||
var InfoLevel = logLevel{1, "info"}
|
||||
var InfoLevel = logLevel{1, "info", *color.New(color.FgGreen)}
|
||||
|
||||
// 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")
|
||||
var ErrorLevel = logLevel{3, "error"}
|
||||
var ErrorLevel = logLevel{3, "error", *color.New(color.FgRed)}
|
||||
|
||||
// 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")
|
||||
var currentLevel = InfoLevel
|
||||
|
@ -33,3 +40,40 @@ func GetLogLevel() logLevel {
|
|||
func SetLogLevel(level logLevel) {
|
||||
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
|
||||
}
|
||||
|
|
23
print.go
23
print.go
|
@ -1,7 +1,6 @@
|
|||
package loggo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"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_LOGLEVEL, logLevel.name)
|
||||
formatted = strings.ReplaceAll(formatted, PLACEHOLDER_MESSAGE, message)
|
||||
tmp := ""
|
||||
if len(extras) > 0 {
|
||||
tmp = strings.Join(extras, GetExtrasSeparator())
|
||||
if len(extras) == 0 {
|
||||
formatted = strings.ReplaceAll(formatted, GetExtrasFormat(), "")
|
||||
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)
|
||||
tmp = ""
|
||||
if timestamp >= 0 {
|
||||
tmp = strconv.FormatInt(now.UnixMilli()-timestamp, 10) + "ms"
|
||||
if timestamp <= 0 {
|
||||
formatted = strings.ReplaceAll(formatted, GetTimediffFormat(), "")
|
||||
formatted = strings.TrimSpace(formatted)
|
||||
} 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)
|
||||
fmt.Println(formatted)
|
||||
logLevel.color.Println(strings.TrimSpace(formatted))
|
||||
if logLevel.level == FatalLevel.level {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue