gonx

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2013 License: MIT Imports: 4 Imported by: 0

README

gonx

gonx is Nginx access log reader libriry for Go. In fact you can use it for any format.

Usage

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.

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.

reader := gonx.NewNginxReader(file, nginxConfig, format_name)

Reader implements io.Reader. Here is example usage

for {
	rec, err := reader.Read()
	if err == io.EOF {
		break
	}
	// Process the record... e.g.
}

See more examples in example/*.go sources.

Format

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 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 Entry, err error)

Contributing

Fork the repo, create a feature branch then send me pull request. Feel free to create new issues or contact me using email.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry map[string]string

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

func NewNginxReader

func NewNginxReader(logFile io.Reader, nginxConf io.Reader, formatName string) (reader *Reader, err error)

func NewReader

func NewReader(logFile io.Reader, format string) *Reader

func (*Reader) GetFormat

func (r *Reader) GetFormat() string

func (*Reader) GetFormatRegexp

func (r *Reader) GetFormatRegexp() *regexp.Regexp

func (*Reader) Read

func (r *Reader) Read() (record Entry, err error)

Read next line from log file, and return parsed record. If all lines read method return ni, io.EOF

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL