-
Notifications
You must be signed in to change notification settings - Fork 86
/
reader.go
51 lines (45 loc) · 1.22 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package gonx
import (
"io"
)
// Reader is a log file reader. Use specific constructors to create it.
type Reader struct {
file io.Reader
parser StringParser
entries chan *Entry
}
// NewReader creates a reader for a custom log format.
func NewReader(logFile io.Reader, format string) *Reader {
return NewParserReader(logFile, NewParser(format))
}
// NewParserReader creates a reader with the given parser
func NewParserReader(logFile io.Reader, parser StringParser) *Reader {
return &Reader{
file: logFile,
parser: parser,
}
}
// NewNginxReader creates a reader for the nginx log format. Nginx config parser will be used
// to get particular format from the conf file.
func NewNginxReader(logFile io.Reader, nginxConf io.Reader, formatName string) (reader *Reader, err error) {
parser, err := NewNginxParser(nginxConf, formatName)
if err != nil {
return nil, err
}
reader = &Reader{
file: logFile,
parser: parser,
}
return
}
// Read next parsed Entry from the log file. Return EOF if there are no Entries to read.
func (r *Reader) Read() (entry *Entry, err error) {
if r.entries == nil {
r.entries = MapReduce(r.file, r.parser, new(ReadAll))
}
entry, ok := <-r.entries
if !ok {
err = io.EOF
}
return
}