Skip to content

Commit

Permalink
Log error with raw message
Browse files Browse the repository at this point in the history
  • Loading branch information
negbie committed Sep 16, 2020
1 parent ba26018 commit b67dba7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

const version = "1.8"
const version = "1.9"
const scanSize = 24

func main() {
fs := flag.NewFlagSet("fancy", flag.ExitOnError)
fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Use fancy %s like: %s [option]\n", version, os.Args[0])
fs.PrintDefaults()
}

var (
cmd = fs.String("cmd", "", "Send input msg to external command and use it's output as new msg")
lokiURL = fs.String("loki-url", "http://localhost:3100", "Loki Server URL")
Expand Down
11 changes: 5 additions & 6 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
const seperator = ' '

var (
errTemplate = fmt.Errorf("Unexpected rsyslog template format")
errTime = fmt.Errorf("Unexpected rsyslog time format")
errLevel = fmt.Errorf("Unexpected rsyslog level format")
errLength = fmt.Errorf("Unexpected rsyslog message length")
errTime = fmt.Errorf("Unexpected rsyslog time format")
errLevel = fmt.Errorf("Unexpected rsyslog level format")
errLength = fmt.Errorf("Unexpected rsyslog message length")
)

func parseLine(raw []byte, promOnly bool) (*LogLine, error) {
Expand Down Expand Up @@ -48,15 +47,15 @@ func parseLine(raw []byte, promOnly bool) (*LogLine, error) {

endPos = bytes.IndexRune(ll.Raw[curPos:], seperator)
if endPos == -1 {
return nil, errTemplate
return nil, fmt.Errorf("Unexpected rsyslog template format in %s", raw)
}
endPos += curPos
ll.Program = string(ll.Raw[curPos:endPos])
curPos = endPos + 1
ll.MsgPos = curPos

if !ll.Valid() {
return nil, errTemplate
return nil, fmt.Errorf("Invalid rsyslog template format in %s", raw)
}

if !promOnly {
Expand Down
13 changes: 4 additions & 9 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,27 @@ type TestCase struct {

func Test_parseLine(t *testing.T) {
cases := []TestCase{
TestCase{
{
input: []byte("2019-10-29T16:21:22.230666+01:00 6 pad fancy {\"key1\":\"val1\", \"key2\":\"val2\"}"),
want: "6 pad fancy {\"key1\":\"val1\", \"key2\":\"val2\"}",
err: nil,
},
TestCase{
{
input: []byte("2019-10-29T16:21:22.230666+01:00 6 pad fancy"),
want: "",
err: errLength,
},
TestCase{
{
input: []byte("2019-10-29T16:21:22.230666+01:00 9 pad fancy {\"key1\":\"val1\", \"key2\":\"val2\"}"),
want: "",
err: errLevel,
},
TestCase{
input: []byte("2019-10-29T16:21:22.230666+01:00 6padfancy {\"key1\":\"val1\", \"key2\":\"val2\"}"),
want: "",
err: errTemplate,
},
}

for _, c := range cases {
got, err := parseLine(c.input, false)
if err != c.err || got.String() != c.want {
t.Errorf("got %q,%v but want %q,%v", got.String(), err, c.want, c.err)
t.Errorf("\nrecv %q,%s\nwant %q,%s\n", got.String(), err, c.want, c.err)
}
}
}
Expand Down

0 comments on commit b67dba7

Please sign in to comment.