custom vars via -var

This commit is contained in:
Sebastian Frank 2021-04-15 12:14:40 +02:00
parent 429eb6b339
commit 36bb357b47
Signed by: apairon
GPG Key ID: A0E05A8199CE3F57

22
main.go
View File

@ -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)