package main import ( "container/ring" "flag" "fmt" "net/http" "os" syslog "gopkg.in/mcuadros/go-syslog.v2" "gopkg.in/mcuadros/go-syslog.v2/format" ) var circbuf *ring.Ring func syslogdHandler(channel syslog.LogPartsChannel) { for logParts := range channel { fmt.Println(logParts) circbuf.Value = logParts circbuf = circbuf.Next() } } func httpHandler(w http.ResponseWriter, r *http.Request) { circbuf.Do(func(x interface{}) { if x == nil { return } logmsg := x.(format.LogParts) if logmsg["message"] == nil { return } c := logmsg["message"].(string) w.Write([]byte(c)) w.Write([]byte("\n")) }) } func main() { syslogSocketPath := flag.String("syslogd-socket", "", "The socket to listen to syslog addresses") // dumpSocketPath := flag.String("dump-socket", "/run/buffer.sock", "The socket that user will connect to in order to receive logs") bufsize := flag.Int("buffer-size", 1000, "Number of messages to keep") syslogAddr := flag.String("syslog-addr", "127.0.0.1:9514", "Address:port where to listen for syslog messages") queryAddr := flag.String("query-addr", "127.0.0.1:9080", "Address:port where to bind the query service") flag.Parse() channel := make(chan format.LogParts) handler := syslog.NewChannelHandler(channel) server := syslog.NewServer() server.SetFormat(syslog.RFC5424) server.SetHandler(handler) if *syslogSocketPath != "" { server.ListenUnixgram(*syslogSocketPath) fmt.Printf("Binding socket `%s` [syslog]\n", *syslogSocketPath) } else { fmt.Printf("Binding address `%s` [syslog]\n", *syslogAddr) server.ListenUDP(*syslogAddr) } circbuf = ring.New(*bufsize) server.Boot() go syslogdHandler(channel) http.HandleFunc("/", httpHandler) fmt.Printf("Binding address `%s` [http]\n", *queryAddr) http.ListenAndServe(*queryAddr, nil) server.Wait() }