Skip to content

Commit

Permalink
Entry type
Browse files Browse the repository at this point in the history
  • Loading branch information
satyrius committed Nov 11, 2013
1 parent 7ee6baa commit c23255b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
The library provides `Reader` type and two constructors for it.

Common constructor `NewReader` gets opened file (`io.Reader`) and log format (`string`) as argumets. format is in form os nginx `log_format` string.

```go
reader := gonx.NewReader(file, format)
```

`NewNginxReader` provides mo magic. It gets nginx config file (`io.Reader`) as second argument and `log_format` name (`string`) a third.

```go
Expand All @@ -37,20 +37,20 @@ See more examples in `example/*.go` sources.
As I said above this library is primary for nginx access log parsing, but it can be configured to parse any other format. `NewReader` accepts `format` argument, it will be transformed to regular expression and used for log line by line parsing. Format is nginx-like, here is example

`$remote_addr [$time_local] "$request"`

It should contain variables inn form `$name`. The regular expression will be created using this string format representation

`^(?P<remote_addr>[^ ]+) \[(?P<time_local>[^]]+)\] "(?P<request>[^"]+)"$`
If log line does not match this format, the `Reader.Read` returns an `error`. Otherwise you will get the record of type `map[string][string]` with `remote_addr`, `time_local` and `request` keys filled with parsed values.

If log line does not match this format, the `Reader.Read` returns an `error`. Otherwise you will get the record of type `Entry` (which is customized `map[string][string]`) with `remote_addr`, `time_local` and `request` keys filled with parsed values.

## Stability

This library API and internal representation can be changed at any moment, but I guarantee that backward capability will be supported for the following public interfaces.

* `func NewReader(logFile io.Reader, format string) *Reader`
* `func NewNginxReader(logFile io.Reader, nginxConf io.Reader, formatName string) (reader *Reader, err error)`
* `func (r *Reader) Read() (record map[string]string, err error)`
* `func (r *Reader) Read() (record Entry, err error)`

## Contributing

Expand Down
8 changes: 5 additions & 3 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ func (r *Reader) GetFormatRegexp() *regexp.Regexp {
return r.re
}

type Entry map[string]string

// Read next line from log file, and return parsed record. If all lines read
// method return ni, io.EOF
func (r *Reader) Read() (record map[string]string, err error) {
func (r *Reader) Read() (record Entry, err error) {
if r.scanner.Scan() {
record, err = r.parseRecord(r.scanner.Text())
} else {
Expand All @@ -86,7 +88,7 @@ func (r *Reader) Read() (record map[string]string, err error) {
return
}

func (r *Reader) parseRecord(line string) (record map[string]string, err error) {
func (r *Reader) parseRecord(line string) (record Entry, err error) {
// Parse line to fill map record. Return error if a line does not match given format
re := r.GetFormatRegexp()
fields := re.FindStringSubmatch(line)
Expand All @@ -96,7 +98,7 @@ func (r *Reader) parseRecord(line string) (record map[string]string, err error)
}

// Iterate over subexp foung and fill the map record
record = make(map[string]string)
record = make(Entry)
for i, name := range re.SubexpNames() {
if i == 0 {
continue
Expand Down
2 changes: 1 addition & 1 deletion reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestGetRecord(t *testing.T) {
format := "$remote_addr [$time_local] \"$request\""
file := strings.NewReader(`89.234.89.123 [08/Nov/2013:13:39:18 +0000] "GET /api/foo/bar HTTP/1.1"`)
reader := NewReader(file, format)
expected := map[string]string{
expected := Entry{
"remote_addr": "89.234.89.123",
"time_local": "08/Nov/2013:13:39:18 +0000",
"request": "GET /api/foo/bar HTTP/1.1",
Expand Down

0 comments on commit c23255b

Please sign in to comment.