diff --git a/CHANGES.txt b/CHANGES.txt index 8ba47c79f..e2e6216fc 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,9 @@ Bug Handling ------------ +* FileOutput no longer panics when using `format = "text"` and payload is nil + (issue #843). + * Fix SandboxDecoder pass-through case so decoders that only use write_message and not inject_message will emit messages correctly (issue #844). diff --git a/plugins/file/file_output.go b/plugins/file/file_output.go index befd052c5..cc85363e0 100644 --- a/plugins/file/file_output.go +++ b/plugins/file/file_output.go @@ -267,7 +267,7 @@ func (o *FileOutput) handleMessage(pack *PipelinePack, outBytes *[]byte) (err er err = fmt.Errorf("Can't encode to JSON: %s", err) } case "text": - *outBytes = append(*outBytes, *pack.Message.Payload...) + *outBytes = append(*outBytes, pack.Message.GetPayload()...) *outBytes = append(*outBytes, NEWLINE) case "protobufstream": if err = ProtobufEncodeMessage(pack, &*outBytes); err != nil { diff --git a/plugins/file/file_output_test.go b/plugins/file/file_output_test.go index 6e6092c4b..e9de8bae6 100644 --- a/plugins/file/file_output_test.go +++ b/plugins/file/file_output_test.go @@ -29,6 +29,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "sync" "time" ) @@ -68,13 +69,15 @@ func FileOutputSpec(c gs.Context) { outData := make([]byte, 0, 20) c.Specify("by default", func() { - fileOutput.handleMessage(pack, &outData) + err = fileOutput.handleMessage(pack, &outData) + c.Expect(err, gs.IsNil) c.Expect(toString(&outData), gs.Equals, *msg.Payload+"\n") }) c.Specify("w/ a prepended timestamp when specified", func() { fileOutput.prefix_ts = true - fileOutput.handleMessage(pack, &outData) + err = fileOutput.handleMessage(pack, &outData) + c.Expect(err, gs.IsNil) // Test will fail if date flips btn handleMessage call and // todayStr calculation... should be extremely rare. todayStr := time.Now().Format("[2006/Jan/02:") @@ -83,6 +86,27 @@ func FileOutputSpec(c gs.Context) { c.Expect(strContents, pipeline_ts.StringContains, payload) c.Expect(strContents, pipeline_ts.StringStartsWith, todayStr) }) + + c.Specify("even when payload is nil", func() { + pack.Message.Payload = nil + err = fileOutput.handleMessage(pack, &outData) + c.Expect(err, gs.IsNil) + strContents := toString(&outData) + c.Expect(strContents, gs.Equals, "\n") + }) + + c.Specify("payload is nil and with a timestamp", func() { + pack.Message.Payload = nil + fileOutput.prefix_ts = true + err = fileOutput.handleMessage(pack, &outData) + c.Expect(err, gs.IsNil) + // Test will fail if date flips btn handleMessage call and + // todayStr calculation... should be extremely rare. + todayStr := time.Now().Format("[2006/Jan/02:") + strContents := toString(&outData) + c.Expect(strings.HasPrefix(strContents, todayStr), gs.IsTrue) + c.Expect(strings.HasSuffix(strContents, " \n"), gs.IsTrue) + }) }) c.Specify("correctly formats JSON output", func() {