From b11c2edfc08f1d0a977abc4502117bd96f445f24 Mon Sep 17 00:00:00 2001 From: boyska Date: Sun, 11 Nov 2018 20:54:10 +0100 Subject: [PATCH] [tail] format locally JSON is actually unfit to send structured log messages, because date/time is not well supported. So we are using BSON, which supports those. BSON is, among the dozen serializers available, really a random choice and might be changed in the future. --- cmd/circolog-tail/main.go | 16 ++++++++++++++-- formatter/format.go | 13 +++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/cmd/circolog-tail/main.go b/cmd/circolog-tail/main.go index 5b25ba7..ce653a7 100644 --- a/cmd/circolog-tail/main.go +++ b/cmd/circolog-tail/main.go @@ -11,7 +11,10 @@ import ( "strconv" "time" + "git.lattuga.net/boyska/circolog/formatter" "github.com/gorilla/websocket" + "gopkg.in/mcuadros/go-syslog.v2/format" + "gopkg.in/mgo.v2/bson" ) func main() { @@ -28,6 +31,7 @@ func main() { Path: "/ws", } q := u.Query() + q.Set("fmt", "bson") if *backlogLimit >= 0 { q.Set("l", strconv.Itoa(*backlogLimit)) } @@ -58,12 +62,20 @@ func main() { go func() { defer close(done) for { - _, message, err := c.ReadMessage() + _, serialized, err := c.ReadMessage() if err != nil { log.Println("close:", err) return } - fmt.Println(string(message)) + var parsed format.LogParts + if err := bson.Unmarshal(serialized, &parsed); err != nil { + log.Println("invalid YAML", err) + continue + } + if err := formatter.WriteFormatted(os.Stdout, formatter.FormatSyslog, parsed); err != nil { + log.Println("error printing", err) + } + fmt.Println() } }() diff --git a/formatter/format.go b/formatter/format.go index a386d5b..94a4176 100644 --- a/formatter/format.go +++ b/formatter/format.go @@ -8,6 +8,7 @@ import ( "time" "gopkg.in/mcuadros/go-syslog.v2/format" + "gopkg.in/mgo.v2/bson" ) // Formatter is an interface, so that multiple implementations can exist @@ -50,6 +51,8 @@ func (rf Format) String() string { return "json" case FormatSyslog: return "syslog" + case FormatBSON: + return "bson" } return "" } @@ -64,6 +67,8 @@ func parseFormat(s string) (Format, error) { return FormatJSON, nil case "syslog": return FormatSyslog, nil + case "bson": + return FormatBSON, nil default: return 0, fmt.Errorf("Undefined format `%s`", s) } @@ -72,6 +77,7 @@ func parseFormat(s string) (Format, error) { const ( FormatSyslog = iota // 0 FormatJSON = iota + FormatBSON = iota ) func WriteFormatted(w io.Writer, f Format, msg format.LogParts) error { @@ -82,6 +88,13 @@ func WriteFormatted(w io.Writer, f Format, msg format.LogParts) error { enc := json.NewEncoder(w) enc.SetIndent("", " ") return enc.Encode(msg) + case FormatBSON: + enc, err := bson.Marshal(msg) + if err != nil { + return err + } + _, err = w.Write(enc) + return err } return nil }