Skip to content

Commit 0253458

Browse files
committed
added color themes for terminal formatters
1 parent 53ea1cc commit 0253458

File tree

14 files changed

+205
-607
lines changed

14 files changed

+205
-607
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
14+
.idea/
15+
.vscode/
16+

.idea/dictionaries/jcdiaz.xml

Lines changed: 0 additions & 7 deletions
This file was deleted.

.idea/go-logger-lib.iml

Lines changed: 0 additions & 10 deletions
This file was deleted.

.idea/misc.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

.idea/modules.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.

.idea/vcs.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

.idea/workspace.xml

Lines changed: 0 additions & 483 deletions
This file was deleted.

log/entry.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package log
22

33
import (
4+
"io"
45
"time"
56
)
67

8+
const (
9+
FieldLevel = "level"
10+
FieldTimestamp = "timestamp"
11+
FieldMessage = "message"
12+
)
13+
714
// Entry represents a log entry.
815
type Entry struct {
916
// Contains all the fields set by the user. TODO
@@ -19,6 +26,7 @@ type Entry struct {
1926
Message string
2027

2128
metadata map[string]interface{}
29+
writer io.Writer
2230
}
2331

2432
func (e *Entry) AddMetadata(key string, val interface{}) {
@@ -27,3 +35,21 @@ func (e *Entry) AddMetadata(key string, val interface{}) {
2735
}
2836
e.metadata[key] = val
2937
}
38+
39+
func (e *Entry) getField(name string) interface{} {
40+
switch name {
41+
case FieldMessage:
42+
return e.Message
43+
case FieldLevel:
44+
return e.Level
45+
case FieldTimestamp:
46+
return e.Timestamp
47+
}
48+
if e.Data == nil {
49+
return nil
50+
}
51+
if ret, ok := e.Data[name]; ok {
52+
return ret
53+
}
54+
return nil
55+
}

log/formatter.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package log
22

33
import (
44
"io"
5-
"os"
6-
"strings"
75
"text/template"
86
)
97

@@ -40,41 +38,3 @@ func (f *BaseFormatter) SetTemplate(tmpl string) error {
4038
f.templateHandler = t
4139
return nil
4240
}
43-
44-
// BaseTerminalFormatter base structure to create formatters for a terminal
45-
type BaseTerminalFormatter struct {
46-
BaseFormatter
47-
// Set to true to bypass checking for a TTY before outputting colors.
48-
ForceColors bool
49-
50-
// Force disabling colors.
51-
DisableColors bool
52-
53-
// Override coloring based on CLICOLOR and CLICOLOR_FORCE. - https://bixense.com/clicolors/
54-
EnvironmentOverrideColors bool
55-
supportsColor *bool
56-
colorScheme LevelColorScheme
57-
}
58-
59-
func (f *BaseTerminalFormatter) isColored() bool {
60-
if f.supportsColor == nil {
61-
supportsColor := f.ForceColors
62-
63-
if force, ok := os.LookupEnv("CLICOLOR_FORCE"); ok && force != "0" {
64-
supportsColor = true
65-
} else if ok && force == "0" {
66-
supportsColor = false
67-
} else if os.Getenv("CLICOLOR") == "0" {
68-
supportsColor = false
69-
} else if strings.Contains(os.Getenv("TERM"), "color") {
70-
supportsColor = true
71-
}
72-
f.supportsColor = &supportsColor
73-
}
74-
75-
return *f.supportsColor && !f.DisableColors
76-
}
77-
78-
func (f *BaseTerminalFormatter) SetColorScheme(scheme LevelColorScheme) {
79-
f.colorScheme = scheme
80-
}

log/formatter_helpers.go

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ func getDefaultHelpers() template.FuncMap {
2323
"level": coloredLevelFn,
2424
"timestamp": timestampFn,
2525
"colorCode": colorCodeFn,
26-
"color": colorFn,
26+
"colorName": colorNameFn,
27+
"colored": colorFieldFn,
28+
"scheme": colorSchemeFn,
2729
}
2830
}
2931

30-
func stringFn(arg interface{}) string {
31-
return fmt.Sprintf("%+v", arg)
32+
func stringFn(arg ...interface{}) string {
33+
return fmt.Sprint(arg...)
3234
}
3335

3436
func uppercaseFn(arg string) string {
@@ -44,30 +46,51 @@ func formatFn(format string, args ...interface{}) string {
4446
}
4547

4648
func coloredLevelFn(entry Entry) string {
49+
return colorFieldFn(FieldLevel, entry, " %s ")
50+
}
51+
52+
func timestampFn(format string, entry Entry, disableColor ...bool) string {
53+
timeStr := iso8601.TimeToString(entry.Timestamp, format)
54+
if len(disableColor) > 0 && disableColor[0] {
55+
return timeStr
56+
}
57+
return colorSchemeFn(FieldTimestamp, timeStr, entry)
58+
}
59+
60+
func colorFieldFn(field string, entry Entry, format ...string) string {
61+
if len(format) > 0 {
62+
return colorSchemeFn(field, fmt.Sprintf(format[0], entry.getField(field)), entry)
63+
}
64+
return colorSchemeFn(field, fmt.Sprint(entry.getField(field)), entry)
65+
}
66+
67+
func colorSchemeFn(schemeName, value string, entry Entry) string {
4768
if v, ok := entry.metadata[metadataColorEnabled]; ok && !v.(bool) {
48-
return entry.Level.String()
69+
return value
4970
}
50-
if _, ok := entry.metadata[metadataColorScheme]; !ok {
51-
return entry.Level.String()
71+
if v, ok := entry.metadata[metadataColorScheme]; !ok || v == nil {
72+
return value
5273
}
5374

54-
scheme := entry.metadata[metadataColorScheme].(LevelColorScheme)
55-
colors := scheme[entry.Level]
75+
scheme := entry.metadata[metadataColorScheme].(TerminalColorScheme)
76+
var colors []fmtc.Color
5677

57-
return fmtc.New().Print(fmt.Sprintf(" %s ", strings.ToUpper(entry.Level.String())), colors...).String()
58-
}
78+
if v, ok := scheme[schemeName]; !ok || v == nil {
79+
return value
80+
} else {
81+
colors = scheme[schemeName][entry.Level]
82+
}
5983

60-
func timestampFn(entry Entry, format string) string {
61-
return iso8601.TimeToString(entry.Timestamp, format)
84+
return fmtc.WithColors(colors...).Sprint(value)
6285
}
6386

6487
func colorCodeFn(arg interface{}, colors ...fmtc.Color) string {
65-
return fmtc.New().Print(arg, colors...).String()
88+
return fmtc.WithColors(colors...).Sprint(arg)
6689
}
6790

68-
func colorFn(arg interface{}, colors ...string) string {
69-
return fmtc.New().Print(arg, streams.From(colors).Map(func(i interface{}) interface{} {
91+
func colorNameFn(arg interface{}, colors ...string) string {
92+
return fmtc.WithColors(streams.From(colors).Map(func(i interface{}) interface{} {
7093
ret, _ := fmtc.Parse(i.(string))
7194
return ret
72-
}).ToArray().([]fmtc.Color)...).String()
95+
}).ToArray().([]fmtc.Color)...).Sprint(arg)
7396
}

0 commit comments

Comments
 (0)