custom vars via -var
This commit is contained in:
parent
429eb6b339
commit
36bb357b47
22
main.go
22
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)
|
||||
|
Loading…
Reference in New Issue
Block a user