| 1 | Syslogparser |
| 2 | ============ |
| 3 | |
| 4 | This is a syslog parser for the Go programming language. |
| 5 | |
| 6 | https://pkg.go.dev/github.com/jeromer/syslogparser |
| 7 | |
| 8 | Installing |
| 9 | ---------- |
| 10 | |
| 11 | go get github.com/jeromer/syslogparser |
| 12 | |
| 13 | Supported RFCs |
| 14 | -------------- |
| 15 | |
| 16 | - [RFC 3164][RFC 3164] |
| 17 | - [RFC 5424][RFC 5424] |
| 18 | |
| 19 | Not all features described in RFCs above are supported but only the most |
| 20 | part of it. For exaple `SDID`s are not supported in [RFC 5424][RFC 5424] and |
| 21 | `STRUCTURED-DATA` are parsed as a whole string. |
| 22 | |
| 23 | This parser should solve 80% of use cases. If your use cases are in the |
| 24 | 20% remaining ones I would recommend you to fully test what you want to |
| 25 | achieve and provide a patch if you want. |
| 26 | |
| 27 | Parsing an RFC 3164 syslog message |
| 28 | ---------------------------------- |
| 29 | |
| 30 | b := "<34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8" |
| 31 | buff := []byte(b) |
| 32 | |
| 33 | p := rfc3164.NewParser(buff) |
| 34 | err := p.Parse() |
| 35 | if err != nil { |
| 36 | panic(err) |
| 37 | } |
| 38 | |
| 39 | for k, v := range p.Dump() { |
| 40 | fmt.Println(k, ":", v) |
| 41 | } |
| 42 | |
| 43 | You should see |
| 44 | |
| 45 | timestamp : 2013-10-11 22:14:15 +0000 UTC |
| 46 | hostname : mymachine |
| 47 | tag : su |
| 48 | content : 'su root' failed for lonvick on /dev/pts/8 |
| 49 | priority : 34 |
| 50 | facility : 4 |
| 51 | severity : 2 |
| 52 | |
| 53 | Parsing an RFC 5424 syslog message |
| 54 | ---------------------------------- |
| 55 | |
| 56 | b := `<165>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"] An application event log entry...` |
| 57 | buff := []byte(b) |
| 58 | |
| 59 | p := rfc5424.NewParser(buff) |
| 60 | err := p.Parse() |
| 61 | if err != nil { |
| 62 | panic(err) |
| 63 | } |
| 64 | |
| 65 | for k, v := range p.Dump() { |
| 66 | fmt.Println(k, ":", v) |
| 67 | } |
| 68 | |
| 69 | You should see |
| 70 | |
| 71 | version : 1 |
| 72 | timestamp : 2003-10-11 22:14:15.003 +0000 UTC |
| 73 | app_name : evntslog |
| 74 | msg_id : ID47 |
| 75 | message : An application event log entry... |
| 76 | priority : 165 |
| 77 | facility : 20 |
| 78 | severity : 5 |
| 79 | hostname : mymachine.example.com |
| 80 | proc_id : - |
| 81 | structured_data : [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"] |
| 82 | |
| 83 | Detecting message format |
| 84 | ------------------------ |
| 85 | |
| 86 | You can use the `DetectRFC()` function. Like this: |
| 87 | |
| 88 | b := []byte(`<165>1 2003-10-11T22:14:15.003Z ...`) |
| 89 | rfc, err := syslogparser.DetectRFC(b) |
| 90 | if err != nil { |
| 91 | panic(err) |
| 92 | } |
| 93 | |
| 94 | switch rfc { |
| 95 | case RFC_UNKNOWN: |
| 96 | fmt.Println("unknown") |
| 97 | case RFC_3164: |
| 98 | fmt.Println("3164") |
| 99 | case RFC_5424: |
| 100 | fmt.Println("5424") |
| 101 | } |
| 102 | |
| 103 | Running tests |
| 104 | ------------- |
| 105 | |
| 106 | Run `make test` |
| 107 | |
| 108 | Running benchmarks |
| 109 | ------------------ |
| 110 | |
| 111 | Run `make benchmark` |
| 112 | |
| 113 | go test -bench=. -benchmem |
| 114 | goos: linux |
| 115 | goarch: amd64 |
| 116 | pkg: github.com/jeromer/syslogparser |
| 117 | BenchmarkDetectRFC-8 81994480 14.7 ns/op 0 B/op 0 allocs/op |
| 118 | PASS |
| 119 | ok github.com/jeromer/syslogparser 2.145s |
| 120 | |
| 121 | cd rfc3164 && go test -bench=. -benchmem |
| 122 | goos: linux |
| 123 | goarch: amd64 |
| 124 | pkg: github.com/jeromer/syslogparser/rfc3164 |
| 125 | BenchmarkParseTimestamp-8 2823901 416 ns/op 16 B/op 1 allocs/op |
| 126 | BenchmarkParseHostname-8 34796552 35.4 ns/op 16 B/op 1 allocs/op |
| 127 | BenchmarkParseTag-8 20954252 59.3 ns/op 8 B/op 1 allocs/op |
| 128 | BenchmarkParseHeader-8 2276569 596 ns/op 80 B/op 3 allocs/op |
| 129 | BenchmarkParsemessage-8 6751579 192 ns/op 104 B/op 4 allocs/op |
| 130 | BenchmarkParseFull-8 1445076 838 ns/op 336 B/op 10 allocs/op |
| 131 | PASS |
| 132 | |
| 133 | ok github.com/jeromer/syslogparser/rfc3164 9.601s |
| 134 | cd rfc5424 && go test -bench=. -benchmem |
| 135 | goos: linux |
| 136 | goarch: amd64 |
| 137 | pkg: github.com/jeromer/syslogparser/rfc5424 |
| 138 | BenchmarkParseTimestamp-8 790478 1488 ns/op 432 B/op 21 allocs/op |
| 139 | BenchmarkParseHeader-8 1000000 1043 ns/op 336 B/op 18 allocs/op |
| 140 | BenchmarkParseFull-8 980828 1306 ns/op 672 B/op 21 allocs/op |
| 141 | PASS |
| 142 | ok github.com/jeromer/syslogparser/rfc5424 4.356s |
| 143 | |
| 144 | |
| 145 | [RFC 5424]: https://tools.ietf.org/html/rfc5424 |
| 146 | [RFC 3164]: https://tools.ietf.org/html/rfc3164 |