Skip to content

Instantly share code, notes, and snippets.

@leodido
Last active March 5, 2019 00:26
Show Gist options
  • Save leodido/021a9f643bf1efa64f61284ebe69d664 to your computer and use it in GitHub Desktop.
Save leodido/021a9f643bf1efa64f61284ebe69d664 to your computer and use it in GitHub Desktop.
Usage example of go-syslog to parse RFC5424 message with best effort mode on
package main
import (
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/influxdata/go-syslog/v2/rfc5424"
)
func main() {
// Obtain our parser
// Notice that when best effort is in use both message and error return values can be non nil ...
p := rfc5424.NewParser(rfc5424.WithBestEffort())
// Let's parse a complete and valid message just for fun
input1 := []byte(`<165>1 2018-10-11T22:14:15.003Z mymach.it e - 1 [ex@32473 iut="3"] An app event...`)
spew.Dump(p.Parse(input1))
// Let's parse the minimally valid syslog message
input2 := []byte(`<1>1`)
fmt.Println("We capture it until error on column 4 ...")
spew.Dump(p.Parse(input2))
// Create a strict parser to notice what results it gives us with input2
strict := rfc5424.NewParser()
fmt.Println("No syslog.Message instance as result, only the error on column 4 ...")
spew.Dump(strict.Parse(input2))
}
// (*rfc5424.SyslogMessage)(0xc0000522a0)({
// priority: (*uint8)(0xc00001223b)(165),
// facility: (*uint8)(0xc00001223c)(20),
// severity: (*uint8)(0xc00001223d)(5),
// version: (uint16) 1,
// timestamp: (*time.Time)(0xc00008e008)(2018-10-11 22:14:15.003 +0000 UTC),
// hostname: (*string)(0xc00008e020)((len=9) "mymach.it"),
// appname: (*string)(0xc00008e030)((len=1) "e"),
// procID: (*string)(<nil>),
// msgID: (*string)(0xc00008e050)((len=1) "1"),
// structuredData: (*map[string]map[string]string)(0xc00008e060)((len=1) {
// (string) (len=8) "ex@32473": (map[string]string) (len=1) {
// (string) (len=3) "iut": (string) (len=1) "3"
// }
// }),
// message: (*string)(0xc00008e068)((len=15) "An app event...")
// })
// (interface {}) <nil>
// We capture it until error on column 4 ...
// (*rfc5424.SyslogMessage)(0xc000052420)({
// priority: (*uint8)(0xc000012600)(1),
// facility: (*uint8)(0xc000012601)(0),
// severity: (*uint8)(0xc000012602)(1),
// version: (uint16) 1,
// timestamp: (*time.Time)(<nil>),
// hostname: (*string)(<nil>),
// appname: (*string)(<nil>),
// procID: (*string)(<nil>),
// msgID: (*string)(<nil>),
// structuredData: (*map[string]map[string]string)(<nil>),
// message: (*string)(<nil>)
// })
// (*errors.errorString)(0xc00000e380)(parsing error [col 4])
// No syslog.Message instance as result, only the error on column 4 ...
// (interface {}) <nil>
// (*errors.errorString)(0xc00000e3a0)(parsing error [col 4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment