Skip to content

Commit

Permalink
Merge pull request #10 from satyrius/parsng_fix
Browse files Browse the repository at this point in the history
Parsing fix
  • Loading branch information
satyrius committed Nov 5, 2014
2 parents 6214927 + 964c605 commit a167515
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.env
src/
*.swp
*.sublime-project

# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: go
go:
- 1.1
- 1.2
- 1.3
- tip
install: make deps
script: go test -v -bench .
5 changes: 3 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"regexp"
"strings"
)

// Log record parser. Use specific constructors to initialize it.
Expand All @@ -17,8 +18,8 @@ type Parser struct {
// strings parsing regexp.
func NewParser(format string) *Parser {
re := regexp.MustCompile(`\\\$([a-z_]+)(\\?(.))`).ReplaceAllString(
regexp.QuoteMeta(format), "(?P<$1>[^$3]*)$2")
return &Parser{format, regexp.MustCompile(fmt.Sprintf("^%v$", re))}
regexp.QuoteMeta(format + " "), "(?P<$1>[^$3]*)$2")
return &Parser{format, regexp.MustCompile(fmt.Sprintf("^%v$", strings.Trim(re, " ")))}
}

// Parse log file line using internal format regexp. If line do not match
Expand Down
10 changes: 6 additions & 4 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type ParserTestSuite struct {
}

func (suite *ParserTestSuite) SetupTest() {
suite.format = "$remote_addr [$time_local] \"$request\""
suite.format = "$remote_addr [$time_local] \"$request\" $status"
suite.parser = NewParser(suite.format)
}

Expand All @@ -29,15 +29,16 @@ func (suite *ParserTestSuite) TestFormatSaved() {
func (suite *ParserTestSuite) TestRegexp() {
assert.Equal(suite.T(),
suite.parser.regexp.String(),
`^(?P<remote_addr>[^ ]*) \[(?P<time_local>[^]]*)\] "(?P<request>[^"]*)"$`)
`^(?P<remote_addr>[^ ]*) \[(?P<time_local>[^]]*)\] "(?P<request>[^"]*)" (?P<status>[^ ]*)$`)
}

func (suite *ParserTestSuite) TestParseString() {
line := `89.234.89.123 [08/Nov/2013:13:39:18 +0000] "GET /api/foo/bar HTTP/1.1"`
line := `89.234.89.123 [08/Nov/2013:13:39:18 +0000] "GET /api/foo/bar HTTP/1.1" 200`
expected := NewEntry(Fields{
"remote_addr": "89.234.89.123",
"time_local": "08/Nov/2013:13:39:18 +0000",
"request": "GET /api/foo/bar HTTP/1.1",
"status": "200",
})
entry, err := suite.parser.ParseString(line)
assert.NoError(suite.T(), err)
Expand All @@ -51,11 +52,12 @@ func (suite *ParserTestSuite) TestParseInvalidString() {
}

func (suite *ParserTestSuite) TestEmptyValue() {
line := `89.234.89.123 [08/Nov/2013:13:39:18 +0000] ""`
line := `89.234.89.123 [08/Nov/2013:13:39:18 +0000] "" 200`
expected := NewEntry(Fields{
"remote_addr": "89.234.89.123",
"time_local": "08/Nov/2013:13:39:18 +0000",
"request": "",
"status": "200",
})
entry, err := suite.parser.ParseString(line)
assert.NoError(suite.T(), err)
Expand Down

0 comments on commit a167515

Please sign in to comment.