Last active
September 6, 2020 00:11
-
-
Save leodido/b243ab302077734b2d31917e4002f291 to your computer and use it in GitHub Desktop.
Usage example of go-syslog to parse a stream of syslog messages with octet-counting framing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io" | |
"time" | |
"github.com/davecgh/go-spew/spew" | |
syslog "github.com/influxdata/go-syslog/v2" | |
"github.com/influxdata/go-syslog/v2/octetcounting" | |
) | |
func main() { | |
// Messages containing MSGLEN token at their start | |
messages := []string{ | |
"16 <1>1 - - - - - -", | |
"17 <2>12 A B C D E -", | |
"16 <1>1", | |
} | |
r, w := io.Pipe() | |
// Goroutine sending one syslog messages per second in the tube .. | |
go func() { | |
defer w.Close() | |
for _, m := range messages { | |
w.Write([]byte(m)) | |
time.Sleep(time.Second) | |
} | |
}() | |
// Custom listener pushing parsing results on a channel | |
// Register them using syslog.WithListener | |
c := make(chan syslog.Result) | |
emit := func(res *syslog.Result) { | |
c <- *res | |
} | |
// Parser taking generic options (best effort and custom listener) | |
parser := octetcounting.NewParser(syslog.WithBestEffort(), syslog.WithListener(emit)) | |
// Goroutine that parses syslog messages as it receives them | |
go func() { | |
defer close(c) | |
parser.Parse(r) | |
}() | |
// Printing out syslog.Message instances | |
for r := range c { | |
spew.Dump(r) | |
} | |
r.Close() | |
} | |
// (syslog.Result) { | |
// Message: (*rfc5424.SyslogMessage)(0xc0000a2060)({ | |
// priority: (*uint8)(0xc0000a6003)(1), | |
// facility: (*uint8)(0xc0000a6004)(0), | |
// severity: (*uint8)(0xc0000a6005)(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>) | |
// }), | |
// Error: (error) <nil> | |
// } | |
// (syslog.Result) { | |
// Message: (*rfc5424.SyslogMessage)(0xc0000a2120)({ | |
// priority: (*uint8)(0xc0000a6230)(2), | |
// facility: (*uint8)(0xc0000a6231)(0), | |
// severity: (*uint8)(0xc0000a6232)(2), | |
// version: (uint16) 12, | |
// 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>) | |
// }), | |
// Error: (*errors.errorString)(0xc0000b0050)(expecting a RFC3339MICRO timestamp or a nil value [col 6]) | |
// } | |
// (syslog.Result) { | |
// Message: (*rfc5424.SyslogMessage)(0xc0000e6000)({ | |
// priority: (*uint8)(0xc0000d8028)(1), | |
// facility: (*uint8)(0xc0000d8029)(0), | |
// severity: (*uint8)(0xc0000d802a)(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>) | |
// }), | |
// Error: (*errors.errorString)(0xc0000d2020)(parsing error [col 4]) | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment