devlogforward/main.go
2021-04-09 12:36:20 +02:00

50 lines
1.1 KiB
Go

package main
import (
"flag"
"fmt"
"os"
"text/template"
"gopkg.in/mcuadros/go-syslog.v2"
)
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")
flag.Parse()
stdoutTemplate := template.Must(template.New("stdout").Parse(*stdoutTpl + "\n"))
channel := make(syslog.LogPartsChannel)
handler := syslog.NewChannelHandler(channel)
server := syslog.NewServer()
// server.SetFormat(syslog.RFC5424)
server.SetFormat(syslog.Automatic)
server.SetHandler(handler)
//err := server.ListenUDP("0.0.0.0:514")
if _, err := os.Stat(*socketFile); err == nil {
os.Remove(*socketFile)
}
err := server.ListenUnixgram(*socketFile)
if err != nil {
panic(err)
}
err = server.Boot()
if err != nil {
panic(err)
}
go func(channel syslog.LogPartsChannel) {
for logParts := range channel {
stdoutTemplate.Execute(os.Stdout, logParts)
fmt.Println(logParts)
}
}(channel)
server.Wait()
}