each app has its color

it is picked from a palette based on its hash: it is pseudorandom, but
still consistent across different lines and different runs.
The palette is a bit too vivid, but let's stick with it for now.

fixes #10
This commit is contained in:
boyska 2019-01-03 12:05:04 +01:00
parent ef4059c144
commit 46e3f6c883

View file

@ -3,6 +3,7 @@ package formatter
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"hash/fnv"
"io" "io"
"text/template" "text/template"
"time" "time"
@ -45,10 +46,18 @@ func init() {
return ansi.Color(text, color) // slow; should use colorfunc return ansi.Color(text, color) // slow; should use colorfunc
}, },
"red": ansi.ColorFunc("red+b"), "red": ansi.ColorFunc("red+b"),
"autoColor": func(s string) string {
// from https://weechat.org/blog/post/2011/08/28/Beautify-your-WeeChat
palette := []string{"31", "35", "38", "40", "49", "63", "70", "80", "92", "99", "112", "126", "130", "138", "142", "148", "167", "169", "174", "176", "178", "184", "186", "210", "212", "215", "247"}
hash := fnv.New32()
hash.Write([]byte(s))
picked := palette[int(hash.Sum32())%len(palette)]
return ansi.Color(s, picked)
},
} }
syslogTmpl = template.Must(template.New("syslog").Funcs(tmplFuncs).Parse( syslogTmpl = template.Must(template.New("syslog").Funcs(tmplFuncs).Parse(
"{{color \"yellow\" (rfc822 (index . \"timestamp\")) }} {{index . \"hostname\"}} " + "{{color \"yellow\" (rfc822 (index . \"timestamp\")) }} {{index . \"hostname\"}} " +
"{{index . \"app_name\"}}" + "{{index . \"app_name\" | autoColor}}" +
"{{ if (ne (index . \"proc_id\") \"-\")}}[{{index . \"proc_id\"}}]{{end}}: " + "{{ if (ne (index . \"proc_id\") \"-\")}}[{{index . \"proc_id\"}}]{{end}}: " +
"{{ sevName (index . \"severity\") }} " + "{{ sevName (index . \"severity\") }} " +
"{{index . \"message\"}}", "{{index . \"message\"}}",