From 36bb357b476e095c1c9013cb273304d07e0ee524 Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Thu, 15 Apr 2021 12:14:40 +0200 Subject: [PATCH] custom vars via -var --- main.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/main.go b/main.go index 98bb30c..c684de8 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "log" "net" "os" + "strings" "text/template" "time" @@ -31,11 +32,26 @@ func udpConnect(addr string) (conn *net.UDPConn) { return } +type mapFlag map[string]string + +func (i *mapFlag) String() string { + return "" +} +func (i *mapFlag) Set(v string) error { + s := strings.Split(v, "=") + if len(s) > 1 && s[0] != "" { + (*i)[s[0]] = strings.Join(s[1:], "=") + } + return nil +} + func main() { socketFile := flag.String("socket", "/dev/log", "socket file") stdoutTpl := flag.String("stdoutTpl", "[{{.timestamp}}] {{.content}}", "stdout line template, variables are: timestamp, content, facility, hostname, priority, severity, tag") udpAddr := flag.String("udpAddr", "", "udp address, format is host:port") udpTpl := flag.String("udpTpl", "{{ json . }}", "udp line template, see stdoutTpl for variables") + customVars := make(mapFlag) + flag.Var(&customVars, "var", "custom variable, pe. type=error, can be used multiple times") flag.Parse() @@ -76,6 +92,9 @@ func main() { if err != nil { panic(err) } + + os.Chmod(*socketFile, 0666) + err = server.Boot() if err != nil { panic(err) @@ -83,6 +102,9 @@ func main() { go func(channel syslog.LogPartsChannel) { for logParts := range channel { + for k, v := range customVars { + logParts[k] = v + } stdoutTemplate.Execute(os.Stdout, logParts) udpChannel <- logParts // fmt.Println(logParts)