From 2bf83b6c336766b7f88dd3016be9f7b80dd5a564 Mon Sep 17 00:00:00 2001 From: boyska Date: Sun, 11 Nov 2018 20:42:26 +0100 Subject: [PATCH] refactor syslog formatting the goal is making circolog-tail apply formatting "locally", receiving structured messages instead --- cmd/circologd/http_log.go | 15 ++++----- {cmd/circologd => formatter}/format.go | 43 ++++++++++++++++++++------ 2 files changed, 41 insertions(+), 17 deletions(-) rename {cmd/circologd => formatter}/format.go (61%) diff --git a/cmd/circologd/http_log.go b/cmd/circologd/http_log.go index d9d21ee..37077de 100644 --- a/cmd/circologd/http_log.go +++ b/cmd/circologd/http_log.go @@ -9,6 +9,7 @@ import ( "time" "git.lattuga.net/boyska/circolog" + "git.lattuga.net/boyska/circolog/formatter" "github.com/gorilla/websocket" "gopkg.in/mcuadros/go-syslog.v2/format" ) @@ -56,7 +57,7 @@ func parseParameters(r *http.Request) (circolog.ClientOptions, error) { } type renderOptions struct { // those are options relevant to the rendered (that is, the HTTP side of circologd) - Format renderFormat + Format formatter.Format } func parseRenderParameters(r *http.Request) (renderOptions, error) { @@ -70,11 +71,10 @@ func parseRenderParameters(r *http.Request) (renderOptions, error) { if len(val) != 1 { return opts, errors.New("Format repeated multiple times") } - format, err := parseRenderFormat(val[0]) + err := opts.Format.Set(val[0]) if err != nil { return opts, err } - opts.Format = format } return opts, nil } @@ -106,9 +106,10 @@ func getHTTPHandler(hub circolog.Hub) http.HandlerFunc { hub.Register <- client for x := range client.Messages { - writeFormatted(w, render_opts.Format, x) - if render_opts.Format != formatJSON { // bleah - w.Write([]byte("\n")) + if err := render_opts.Format.WriteFormatted(w, x); err == nil { + if render_opts.Format != formatter.FormatJSON { // bleah + w.Write([]byte("\n")) + } } } } @@ -166,7 +167,7 @@ func getWSHandler(hub circolog.Hub) http.HandlerFunc { if err != nil { return } - writeFormatted(w, render_opts.Format, message) + render_opts.Format.WriteFormatted(w, message) if err := w.Close(); err != nil { return diff --git a/cmd/circologd/format.go b/formatter/format.go similarity index 61% rename from cmd/circologd/format.go rename to formatter/format.go index 4818d2f..a386d5b 100644 --- a/cmd/circologd/format.go +++ b/formatter/format.go @@ -1,4 +1,4 @@ -package main +package formatter import ( "encoding/json" @@ -33,29 +33,52 @@ func init() { )) } -type renderFormat int +type Format int -func parseRenderFormat(s string) (renderFormat, error) { +func (rf *Format) Set(v string) error { + newvalue, err := parseFormat(v) + if err != nil { + return err + } + *rf = newvalue + return nil +} + +func (rf Format) String() string { + switch rf { + case FormatJSON: + return "json" + case FormatSyslog: + return "syslog" + } + return "" +} + +func (rf Format) WriteFormatted(w io.Writer, msg format.LogParts) error { + return WriteFormatted(w, rf, msg) +} + +func parseFormat(s string) (Format, error) { switch s { case "json": - return formatJSON, nil + return FormatJSON, nil case "syslog": - return formatSyslog, nil + return FormatSyslog, nil default: return 0, fmt.Errorf("Undefined format `%s`", s) } } const ( - formatSyslog = iota // 0 - formatJSON = iota + FormatSyslog = iota // 0 + FormatJSON = iota ) -func writeFormatted(w io.Writer, f renderFormat, msg format.LogParts) error { +func WriteFormatted(w io.Writer, f Format, msg format.LogParts) error { switch f { - case formatSyslog: + case FormatSyslog: return syslogTmpl.Execute(w, msg) - case formatJSON: + case FormatJSON: enc := json.NewEncoder(w) enc.SetIndent("", " ") return enc.Encode(msg)