From 583d9aabf21623647d77a39ecd2d10dbdb96f39d Mon Sep 17 00:00:00 2001 From: velvettear Date: Thu, 2 Nov 2023 12:07:38 +0100 Subject: [PATCH] fixed some formats --- format.go | 26 ++++++++++++++++++++++++++ print.go | 8 ++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/format.go b/format.go index ba5df7c..4834363 100644 --- a/format.go +++ b/format.go @@ -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 +} diff --git a/print.go b/print.go index 7eebccc..f5037d5 100644 --- a/print.go +++ b/print.go @@ -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)