reader.go 1021 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package gonx
  2. import (
  3. "io"
  4. )
  5. // Log file reader. Use specific constructors to create it.
  6. type Reader struct {
  7. file io.Reader
  8. parser *Parser
  9. entries chan *Entry
  10. }
  11. // Creates reader for custom log format.
  12. func NewReader(logFile io.Reader, format string) *Reader {
  13. return &Reader{
  14. file: logFile,
  15. parser: NewParser(format),
  16. }
  17. }
  18. // Creates reader for nginx log format. Nginx config parser will be used
  19. // to get particular format from the conf file.
  20. func NewNginxReader(logFile io.Reader, nginxConf io.Reader, formatName string) (reader *Reader, err error) {
  21. parser, err := NewNginxParser(nginxConf, formatName)
  22. if err != nil {
  23. return nil, err
  24. }
  25. reader = &Reader{
  26. file: logFile,
  27. parser: parser,
  28. }
  29. return
  30. }
  31. // Get next parsed Entry from the log file. Return EOF if there is no Entries to read.
  32. func (r *Reader) Read() (entry *Entry, err error) {
  33. if r.entries == nil {
  34. r.entries = MapReduce(r.file, r.parser, new(ReadAll))
  35. }
  36. entry, ok := <-r.entries
  37. if !ok {
  38. err = io.EOF
  39. }
  40. return
  41. }