fixed some formats

This commit is contained in:
Daniel Sommer 2023-11-02 12:07:38 +01:00
parent 3c1cba2e81
commit 583d9aabf2
2 changed files with 32 additions and 2 deletions

View file

@ -21,6 +21,12 @@ var logFormat = PLACEHOLDER_TIMESTAMP + " - [" + PLACEHOLDER_LOGLEVEL + "] > " +
// current date format (defaults to: "02.01.2006 15:04:05")
var dateFormat = "02.01.2006 15:04:05"
// current time diff format (defaults to: "[$TIMEDIFF$]")
var timediffFormat = "[" + PLACEHOLDER_TIMEDIFF + "]"
// current extras format (defaults to: "($EXTRAS$)")
var extrasFormat = "(" + PLACEHOLDER_EXTRAS + ")"
// current extras separator (defaults to: "|")
var extrasSeparator = "|"
@ -53,3 +59,23 @@ func GetExtrasSeparator() string {
func SetExtrasSeparator(separator string) {
extrasSeparator = separator
}
// get current extras format
func GetExtrasFormat() string {
return extrasFormat
}
// set current extras format
func SetExtrasFormat(format string) {
extrasFormat = format
}
// get current time diff format
func GetTimediffFormat() string {
return timediffFormat
}
// set current time diff format
func SetTimediffFormat(format string) {
timediffFormat = format
}

View file

@ -68,12 +68,16 @@ 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 {
formatted = strings.ReplaceAll(formatted, PLACEHOLDER_EXTRAS, strings.Join(extras, GetExtrasSeparator()))
tmp = strings.Join(extras, GetExtrasSeparator())
}
formatted = strings.ReplaceAll(formatted, GetExtrasFormat(), tmp)
tmp = ""
if timestamp >= 0 {
formatted = strings.ReplaceAll(formatted, PLACEHOLDER_TIMEDIFF, strconv.FormatInt(now.UnixMilli()-timestamp, 10)+"ms")
tmp = strconv.FormatInt(now.UnixMilli()-timestamp, 10) + "ms"
}
formatted = strings.ReplaceAll(formatted, GetTimediffFormat(), tmp)
fmt.Println(formatted)
if logLevel.level == FatalLevel.level {
os.Exit(1)