circolog-tail has colors

This commit is contained in:
boyska 2019-01-02 17:29:34 +01:00
parent 6d1ddba736
commit 86243bf464
2 changed files with 33 additions and 5 deletions

View file

@ -14,6 +14,8 @@ import (
"git.lattuga.net/boyska/circolog/filtering" "git.lattuga.net/boyska/circolog/filtering"
"git.lattuga.net/boyska/circolog/formatter" "git.lattuga.net/boyska/circolog/formatter"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
isatty "github.com/mattn/go-isatty"
"github.com/mgutz/ansi"
"gopkg.in/mcuadros/go-syslog.v2/format" "gopkg.in/mcuadros/go-syslog.v2/format"
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
) )
@ -24,8 +26,21 @@ func main() {
backlogLimit := flag.Int("n", -1, "Limit the backlog length, defaults to no limit (-1)") backlogLimit := flag.Int("n", -1, "Limit the backlog length, defaults to no limit (-1)")
var filter filtering.ExprValue var filter filtering.ExprValue
flag.Var(&filter, "where", "sql-like query to filter logs") flag.Var(&filter, "where", "sql-like query to filter logs")
// TODO: change to color-mode=auto/no/always
noColor := flag.Bool("no-color", false, "disable colors")
forceColor := flag.Bool("force-color", false, "force colors even on TTY")
flag.Parse() flag.Parse()
if *noColor && *forceColor {
fmt.Fprintln(os.Stderr, "Can't use both -no-color and -force-color")
flag.Usage()
os.Exit(2)
}
if *noColor || (!isatty.IsTerminal(os.Stdout.Fd()) && !*forceColor) {
ansi.DisableColors(true)
}
interrupt := make(chan os.Signal, 1) interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt) signal.Notify(interrupt, os.Interrupt)
var d *websocket.Dialer var d *websocket.Dialer

View file

@ -7,6 +7,7 @@ import (
"text/template" "text/template"
"time" "time"
"github.com/mgutz/ansi"
"gopkg.in/mcuadros/go-syslog.v2/format" "gopkg.in/mcuadros/go-syslog.v2/format"
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
) )
@ -26,18 +27,30 @@ func init() {
return dt.Format(time.RFC822) return dt.Format(time.RFC822)
}, },
"sevName": func(s int) string { "sevName": func(s int) string {
names := []string{"emerg", "alert", "crit", "err", "warn", "notice", "info", "dbg"} names := []string{"emerg ", "alert ", "crit ", "err ", "warn ", "notice", "info ", "dbg "}
if s >= len(names) { switch {
case s < 2: // emerg..alert
return ansi.Color(names[s], "red+b")
case s < 4: // emerg..err
return ansi.Color(names[s], "red")
case s < 6: // warn..notice
return ansi.Color(names[s], "white+b")
case s >= len(names):
return "???" return "???"
} default:
return names[s] return names[s]
}
}, },
"color": func(color, text string) string {
return ansi.Color(text, color) // slow; should use colorfunc
},
"red": ansi.ColorFunc("red+b"),
} }
syslogTmpl = template.Must(template.New("syslog").Funcs(tmplFuncs).Parse( syslogTmpl = template.Must(template.New("syslog").Funcs(tmplFuncs).Parse(
"{{rfc822 (index . \"timestamp\")}} {{index . \"hostname\"}} " + "{{color \"yellow\" (rfc822 (index . \"timestamp\")) }} {{index . \"hostname\"}} " +
"{{index . \"app_name\"}}" + "{{index . \"app_name\"}}" +
"{{ 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\"}}",
)) ))
} }