Skip to content

Commit

Permalink
Improved adaptative log. (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
aurc authored Jul 11, 2022
1 parent 19f2f5b commit dee5aa5
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 60 deletions.
186 changes: 129 additions & 57 deletions internal/config/adpatative_log_confiig.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,66 +28,26 @@ import (
)

func MakeConfigFromSample(sample []map[string]interface{}) *Config {
const cTimestamp = "timestamp"
const cSeverity = "severity"
keyMap := make(map[string]*Key)
for _, m := range sample {
for k, v := range m {
if _, ok := keyMap[k]; ok {
continue
}
if strings.Index(k, "/") != -1 || k == ParseErr || k == TextPayload {
if strings.Index(k, "/") != -1 || k == ParseErr {
continue
}
if k == cTimestamp {
keyMap[k] = &Key{
Name: k,
Type: TypeDateTime,
Color: Color{
Foreground: "purple",
Background: "black",
},
}
if timestamp.Contains(k) {
keyMap[k] = timestamp.keyConfig(k)
continue
} else if k == cSeverity {
keyMap[k] = &Key{
Name: k,
Type: TypeString,
Color: Color{
Foreground: "white",
Background: "black",
},
ColorWhen: []ColorWhen{
{
MatchValue: "(?i)error",
Color: Color{
Foreground: "red",
Background: "black",
},
},
{
MatchValue: "(?i)info",
Color: Color{
Foreground: "green",
Background: "black",
},
},
{
MatchValue: "(?i)warn",
Color: Color{
Foreground: "orange",
Background: "black",
},
},
{
MatchValue: "(?i)debug",
Color: Color{
Foreground: "blue",
Background: "black",
},
},
},
}
} else if logType.Contains(k) {
keyMap[k] = logType.keyConfig(k)
continue
} else if message.Contains(k) {
keyMap[k] = message.keyConfig(k)
continue
} else if errorKey.Contains(k) {
keyMap[k] = errorKey.keyConfig(k)
continue
}
if _, ok := v.(map[string]interface{}); ok {
Expand All @@ -108,15 +68,20 @@ func MakeConfigFromSample(sample []map[string]interface{}) *Config {
c := &Config{
Keys: []Key{},
}
if v, ok := keyMap[cTimestamp]; ok {
c.Keys = append(c.Keys, *v)
}
if v, ok := keyMap[cSeverity]; ok {
c.Keys = append(c.Keys, *v)
var orderedKeys []string
orderedKeys = append(orderedKeys, timestamp.Keys()...)
orderedKeys = append(orderedKeys, logType.Keys()...)
orderedKeys = append(orderedKeys, message.Keys()...)
orderedKeys = append(orderedKeys, errorKey.Keys()...)
for _, v := range orderedKeys {
if v, ok := keyMap[v]; ok {
c.Keys = append(c.Keys, *v)
}
}

var sk []string
for k := range keyMap {
if k != cTimestamp && k != cSeverity {
if !timestamp.Contains(k) && !message.Contains(k) && !logType.Contains(k) && !errorKey.Contains(k) {
sk = append(sk, k)
}
}
Expand All @@ -127,3 +92,110 @@ func MakeConfigFromSample(sample []map[string]interface{}) *Config {

return c
}

type preBakedRule struct {
keyMatchesAny map[string]bool
keyConfig func(keyName string) *Key
}

func (p preBakedRule) Contains(key string) bool {
if _, ok := p.keyMatchesAny[key]; ok {
return ok
}
return false
}

func (p preBakedRule) Keys() []string {
var arr []string
for k := range p.keyMatchesAny {
arr = append(arr, k)
}
return arr
}

var (
timestamp = preBakedRule{
keyMatchesAny: map[string]bool{"timestamp": true, "time": true},
keyConfig: func(keyName string) *Key {
return &Key{
Name: keyName,
Type: TypeDateTime,
Color: Color{
Foreground: "purple",
Background: "black",
},
}
},
}
logType = preBakedRule{
keyMatchesAny: map[string]bool{"level": true, "severity": true},
keyConfig: func(keyName string) *Key {
return &Key{
Name: keyName,
Type: TypeString,
Color: Color{
Foreground: "white",
Background: "black",
},
ColorWhen: []ColorWhen{
{
MatchValue: "(?i)error",
Color: Color{
Foreground: "red",
Background: "black",
},
},
{
MatchValue: "(?i)info",
Color: Color{
Foreground: "green",
Background: "black",
},
},
{
MatchValue: "(?i)warn",
Color: Color{
Foreground: "orange",
Background: "black",
},
},
{
MatchValue: "(?i)debug",
Color: Color{
Foreground: "blue",
Background: "black",
},
},
},
}
},
}
message = preBakedRule{
keyMatchesAny: map[string]bool{"message": true},
keyConfig: func(keyName string) *Key {
return &Key{
Name: keyName,
Type: TypeString,
MaxWidth: 50,
Color: Color{
Foreground: "yellow",
Background: "black",
},
}
},
}
errorKey = preBakedRule{
keyMatchesAny: map[string]bool{"error": true},
keyConfig: func(keyName string) *Key {
return &Key{
Name: keyName,
Type: TypeString,
MaxWidth: 30,
Color: Color{
Foreground: "red",
Background: "black",
},
}
},
}
)
2 changes: 1 addition & 1 deletion internal/config/log_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

const (
ParseErr = "$_parseErr"
TextPayload = "Raw Log Entry"
TextPayload = "message"
)

type Config struct {
Expand Down
4 changes: 3 additions & 1 deletion internal/loggo/json_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,9 @@ func (j *JsonView) setJson() *JsonView {
sb.WriteString(" ")
}
}
j.textView.SetText(sb.String())
j.wordWrap = true
j.textView.SetWrap(j.wordWrap)
j.textView.SetText(sb.String()).SetTextColor(tcell.ColorRed)
} else {
text := &strings.Builder{}
text.WriteString("{" + j.newLine())
Expand Down
13 changes: 12 additions & 1 deletion internal/loggo/log_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ func (l *LogView) makeUIComponents() {
l.makeLayoutsWithJsonView()
}, l.makeLayouts)
l.jsonView.SetBorder(true).SetTitle("Log Entry")
b, _ := json.Marshal(l.inSlice[row-1])
var b []byte
if _, ok := l.inSlice[row-1][config.ParseErr]; ok {
b = []byte(fmt.Sprintf(`%v`, l.inSlice[row-1][config.TextPayload]))
} else {
b, _ = json.Marshal(l.inSlice[row-1])
}
l.jsonView.SetJson(b)
l.makeLayoutsWithJsonView()
} else {
Expand Down Expand Up @@ -430,6 +435,12 @@ func (d *LogData) GetCell(row, column int) *tview.TableCell {
tc.MaxWidth = k.MaxWidth
}

if k.Name == config.TextPayload {
if _, ok := d.logView.inSlice[row-1][config.ParseErr]; ok {
fgColor = tcell.ColorBlue
}
}

return tc.
SetBackgroundColor(bgColor).
SetTextColor(fgColor).
Expand Down

0 comments on commit dee5aa5

Please sign in to comment.