influxdata / go-syslog

Blazing fast syslog parser
MIT License
476 stars 69 forks source link

Unclear how to get actual data out of the Message interface #33

Closed Minnozz closed 3 years ago

Minnozz commented 4 years ago

Hi. I'm trying to use this library to parse messages that rsyslog sends over a TCP connection. All examples in the repository do a "dump" of the syslog.Result type, which shows the data that is contained within the concrete types, but there is no example that shows how to get (for instance) the message payload out of the syslog.Message interface.

I assume you need to typecast Result.Message into its concrete type to access the actual data, but this is not explained or shown anywhere in the documentation. Could this be clarified?

william20111 commented 4 years ago

This confused me. Im doing this to access the message, v2 -> v3 changed the interface and removed the Message() func on the interface. That help at all?

if r.Error != nil {
    return nil, r.Error
}
syslogMsg := r.Message.(*rfc5424.SyslogMessage)
structData := syslogMsg.StructuredData
msg := syslogMsg.Message
Minnozz commented 4 years ago

Yes, thank you for the snippet. I ended up with very similar code.

Initially it was not clear to me that the Result.Message.(*rfc5424.SyslogMessage) typecast was necessary to use the data inside Result.Message, so I think adding something like your snippet to the example_test.go and/or the documentation would be very helpful.

tonimelisma commented 3 years ago

Indeed, seems like a pretty big gaping bug that the library which parses syslog messages doesn't actually provide access to the parsed messages 😄

Can someone provide any guidance on below:

    message, err := parser.Parse(syslogData)

message is of type syslog.Message which provides only FacilityLevel, FacilityMessage etc but I don't have access to the actual message, hostname etc information.

Any pointers, how to access the parsed data?

leodido commented 3 years ago

I'm about to send a PR to show how to access message, hostname etc. from a parsing result message.

i := []byte(`<165>4 2018-10-11T22:14:15.003Z mymach.it e - 1 [ex@32473 iut="3"] An application event log entry...`)
p := NewParser()
m, _ := p.Parse(i)
msg := m.(*SyslogMessage) // cast
// Since a syslog message may not contain all data, I choose pointer types (that can be nil) to represent them
fmt.Println(*msg.Message)
fmt.Println(*msg.Hostname)
leodido commented 3 years ago

In case you need other clarifications, please let me know :)