Skip to content

Commit 971ac8a

Browse files
committed
added better formatting functions
1 parent 0253458 commit 971ac8a

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

log/formatter_helpers.go

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,38 @@ const (
1616

1717
func getDefaultHelpers() template.FuncMap {
1818
return template.FuncMap{
19-
"string": stringFn,
20-
"upper": uppercaseFn,
21-
"lower": lowercaseFn,
22-
"fmt": formatFn,
23-
"level": coloredLevelFn,
24-
"timestamp": timestampFn,
25-
"colorCode": colorCodeFn,
26-
"colorName": colorNameFn,
27-
"colored": colorFieldFn,
28-
"scheme": colorSchemeFn,
19+
"Sprint": fmt.Sprint,
20+
"Sprintf": fmt.Sprintf,
21+
"ToUpper": strings.ToUpper,
22+
"ToLower": strings.ToLower,
23+
"Replace": strings.Replace,
24+
"TimeFormat": iso8601.TimeToString,
25+
"level": levelFn,
26+
"timestamp": timestampFn,
27+
"message": messageFn,
28+
"colorCode": colorCodeFn,
29+
"colorName": colorNameFn,
30+
"colored": colorFieldFn,
31+
"scheme": colorSchemeFn,
2932
}
3033
}
3134

32-
func stringFn(arg ...interface{}) string {
33-
return fmt.Sprint(arg...)
34-
}
35-
36-
func uppercaseFn(arg string) string {
37-
return strings.ToUpper(stringFn(arg))
38-
}
39-
40-
func lowercaseFn(arg interface{}) string {
41-
return strings.ToLower(stringFn(arg))
42-
}
43-
44-
func formatFn(format string, args ...interface{}) string {
45-
return fmt.Sprintf(format, args...)
46-
}
47-
48-
func coloredLevelFn(entry Entry) string {
35+
func levelFn(entry Entry) string {
4936
return colorFieldFn(FieldLevel, entry, " %s ")
5037
}
5138

52-
func timestampFn(format string, entry Entry, disableColor ...bool) string {
39+
func timestampFn(format string, entry Entry) string {
5340
timeStr := iso8601.TimeToString(entry.Timestamp, format)
54-
if len(disableColor) > 0 && disableColor[0] {
55-
return timeStr
56-
}
5741
return colorSchemeFn(FieldTimestamp, timeStr, entry)
5842
}
5943

44+
func messageFn(entry Entry, newLinePadding ...string) string {
45+
if len(newLinePadding) > 0 && newLinePadding[0] != "" {
46+
return colorSchemeFn(FieldMessage, strings.Replace(entry.Message, "\n", "\n"+newLinePadding[0], -1), entry)
47+
}
48+
return colorFieldFn(FieldMessage, entry)
49+
}
50+
6051
func colorFieldFn(field string, entry Entry, format ...string) string {
6152
if len(format) > 0 {
6253
return colorSchemeFn(field, fmt.Sprintf(format[0], entry.getField(field)), entry)

log/formatter_themes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var (
6060
}
6161

6262
TerminalThemeCliApp = &TerminalTheme{
63-
Template: `{{ timestamp " HH:mm:ss " . }} {{ colored "message" . }}`,
63+
Template: `{{ timestamp " HH:mm:ss " . }} {{ message . " " }}`,
6464
Schemes: TerminalColorScheme{
6565
"timestamp": LevelColorScheme{
6666
DebugLevel: []fmtc.Color{fmtc.Gray},

0 commit comments

Comments
 (0)