Compare commits
No commits in common. "668cdf1aa52b619a9ec27d74169087ddc318ee59" and "583d9aabf21623647d77a39ecd2d10dbdb96f39d" have entirely different histories.
668cdf1aa5
...
583d9aabf2
6 changed files with 20 additions and 86 deletions
|
@ -1,5 +1,5 @@
|
||||||
# loggo
|
# loggo
|
||||||
|
|
||||||
a simple log module written in golang
|
a simple log module written in golang.
|
||||||
|
|
||||||
`go get git.velvettear.de/velvettear/loggo`
|
`go get https://git.velvettear.de/velvettear/loggo.git`
|
|
@ -16,7 +16,7 @@ const PLACEHOLDER_TIMEDIFF = "$TIMEDIFF$"
|
||||||
const PLACEHOLDER_EXTRAS = "$EXTRAS$"
|
const PLACEHOLDER_EXTRAS = "$EXTRAS$"
|
||||||
|
|
||||||
// current log format (defaults to: "$TIMESTAMP$ - [$LOGLEVEL$] > $MESSAGE ($EXTRAS$) [$TIMEDIFF$]")
|
// current log format (defaults to: "$TIMESTAMP$ - [$LOGLEVEL$] > $MESSAGE ($EXTRAS$) [$TIMEDIFF$]")
|
||||||
var logFormat = PLACEHOLDER_TIMESTAMP + " - [" + PLACEHOLDER_LOGLEVEL + "] > " + PLACEHOLDER_MESSAGE + " (" + PLACEHOLDER_EXTRAS + ") [" + PLACEHOLDER_TIMEDIFF + "]"
|
var logFormat = PLACEHOLDER_TIMESTAMP + " - [" + PLACEHOLDER_LOGLEVEL + "] > " + PLACEHOLDER_MESSAGE + "(" + PLACEHOLDER_EXTRAS + ") [" + PLACEHOLDER_TIMEDIFF + "]"
|
||||||
|
|
||||||
// current date format (defaults to: "02.01.2006 15:04:05")
|
// current date format (defaults to: "02.01.2006 15:04:05")
|
||||||
var dateFormat = "02.01.2006 15:04:05"
|
var dateFormat = "02.01.2006 15:04:05"
|
||||||
|
@ -28,7 +28,7 @@ var timediffFormat = "[" + PLACEHOLDER_TIMEDIFF + "]"
|
||||||
var extrasFormat = "(" + PLACEHOLDER_EXTRAS + ")"
|
var extrasFormat = "(" + PLACEHOLDER_EXTRAS + ")"
|
||||||
|
|
||||||
// current extras separator (defaults to: "|")
|
// current extras separator (defaults to: "|")
|
||||||
var extrasSeparator = " | "
|
var extrasSeparator = "|"
|
||||||
|
|
||||||
// get current log format
|
// get current log format
|
||||||
func GetLogFormat() string {
|
func GetLogFormat() string {
|
||||||
|
|
8
go.mod
8
go.mod
|
@ -1,11 +1,3 @@
|
||||||
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
11
go.sum
|
@ -1,11 +0,0 @@
|
||||||
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,32 +1,25 @@
|
||||||
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", *color.New(color.FgCyan)}
|
var DebugLevel = logLevel{0, "debug"}
|
||||||
|
|
||||||
// info log level (level = 1, name = "info")
|
// info log level (level = 1, name = "info")
|
||||||
var InfoLevel = logLevel{1, "info", *color.New(color.FgGreen)}
|
var InfoLevel = logLevel{1, "info"}
|
||||||
|
|
||||||
// warning log level (level = 2, name = "warning")
|
// warning log level (level = 2, name = "warning")
|
||||||
var WarningLevel = logLevel{2, "warning", *color.New(color.FgYellow)}
|
var WarningLevel = logLevel{2, "warning"}
|
||||||
|
|
||||||
// error log level (level = 3, name = "error")
|
// error log level (level = 3, name = "error")
|
||||||
var ErrorLevel = logLevel{3, "error", *color.New(color.FgRed)}
|
var ErrorLevel = logLevel{3, "error"}
|
||||||
|
|
||||||
// fatal log level (level = 4, name = "fatal")
|
// fatal log level (level = 4, name = "fatal")
|
||||||
var FatalLevel = logLevel{4, "fatal", *color.New(color.FgRed).Add(color.Bold)}
|
var FatalLevel = logLevel{4, "fatal"}
|
||||||
|
|
||||||
// current log level (defaults to: "infoLevel")
|
// current log level (defaults to: "infoLevel")
|
||||||
var currentLevel = InfoLevel
|
var currentLevel = InfoLevel
|
||||||
|
@ -40,40 +33,3 @@ 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
|
|
||||||
}
|
|
||||||
|
|
23
print.go
23
print.go
|
@ -1,6 +1,7 @@
|
||||||
package loggo
|
package loggo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -67,21 +68,17 @@ 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)
|
||||||
if len(extras) == 0 {
|
tmp := ""
|
||||||
formatted = strings.ReplaceAll(formatted, GetExtrasFormat(), "")
|
if len(extras) > 0 {
|
||||||
formatted = strings.TrimSpace(formatted)
|
tmp = strings.Join(extras, GetExtrasSeparator())
|
||||||
} else {
|
|
||||||
format := GetExtrasFormat()
|
|
||||||
formatted = strings.ReplaceAll(formatted, format, strings.ReplaceAll(format, PLACEHOLDER_EXTRAS, strings.Join(extras, GetExtrasSeparator())))
|
|
||||||
}
|
}
|
||||||
if timestamp <= 0 {
|
formatted = strings.ReplaceAll(formatted, GetExtrasFormat(), tmp)
|
||||||
formatted = strings.ReplaceAll(formatted, GetTimediffFormat(), "")
|
tmp = ""
|
||||||
formatted = strings.TrimSpace(formatted)
|
if timestamp >= 0 {
|
||||||
} else {
|
tmp = strconv.FormatInt(now.UnixMilli()-timestamp, 10) + "ms"
|
||||||
format := GetTimediffFormat()
|
|
||||||
formatted = strings.ReplaceAll(formatted, format, strings.ReplaceAll(format, PLACEHOLDER_TIMEDIFF, strconv.FormatInt(now.UnixMilli()-timestamp, 10)+"ms"))
|
|
||||||
}
|
}
|
||||||
logLevel.color.Println(strings.TrimSpace(formatted))
|
formatted = strings.ReplaceAll(formatted, GetTimediffFormat(), tmp)
|
||||||
|
fmt.Println(formatted)
|
||||||
if logLevel.level == FatalLevel.level {
|
if logLevel.level == FatalLevel.level {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue