diff --git a/main.go b/main.go index faf263c..d2a7522 100644 --- a/main.go +++ b/main.go @@ -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") diff --git a/parse.go b/parse.go index 25a61dd..51a6a88 100644 --- a/parse.go +++ b/parse.go @@ -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) { @@ -48,7 +47,7 @@ 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]) @@ -56,7 +55,7 @@ func parseLine(raw []byte, promOnly bool) (*LogLine, error) { ll.MsgPos = curPos if !ll.Valid() { - return nil, errTemplate + return nil, fmt.Errorf("Invalid rsyslog template format in %s", raw) } if !promOnly { diff --git a/parse_test.go b/parse_test.go index 384ceef..23e2c7a 100644 --- a/parse_test.go +++ b/parse_test.go @@ -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) } } }