logparse

package module
v0.0.0-...-e2e713e Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2020 License: GPL-3.0 Imports: 10 Imported by: 0

README

Log format parser

logparse is a library written in golang to parse the most common log formats.

Usage

First install the library with:

go get xojoc.pw/logparse

logparse is simple to use. First parse a string with either logparse.Common or logparse.Combined and then access the field of logparse.Entry for the required information. Examples:

see godoc for the complete documentation.

Log formats

Right now logparse can parse the common and combined log formats. Support is under way for the extended log format.

Who?

logparse was written by Alexandru Cojocaru.

License

logparse is released under the GPLv3 or later, see COPYING.

Documentation

Overview

Package logparse parses a log entry in the most common formats.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	// The IP of the client which made the request (nil if unknown).
	Host net.IP
	// The username of the logged in user making the request (empty if anonymous).
	User string
	// The time the request was made (zero value if unknown, check with IsZero).
	Time time.Time
	// The HTTP request line from the client (nil if unknown).
	Request *http.Request
	// The HTTP status code returned to the client (-1 if unknown).
	Status int
	// The size in bytes of the data sent to the client (0 if no data sent).
	Bytes int
	// The URL of the host the client comes from (nil if unknown).
	Referer *url.URL
	// The user agent of the client (nil if unknown).
	UserAgent *useragent.UserAgent
}

An Entry represents a log entry.

func Combined

func Combined(line string) (*Entry, error)

Combined parses a log line containing a log entry in the combined log format.

An entry in the combined log format has the form:

Host - User Time Request Status Bytes Referer UserAgent

basicaly it's the same as the common log format with the added fields:

Referer the URL of the host the client comes from
UserAgent the user agent of the client
Example
l, err := Combined(`:: - xojoc [10/Feb/2015:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "http://xojoc.pw" "Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0"`)
if err != nil {
	log.Fatal(err)
}
fmt.Println(l)
fmt.Println(l.Host)
fmt.Println(l.User)
fmt.Println(l.Time.Format("02/Jan/2006"))
fmt.Println(l.Request.URL)
fmt.Println(l.Status)
fmt.Println(l.Bytes)
fmt.Println(l.Referer)
fmt.Println(l.UserAgent.Name)
fmt.Println(l.UserAgent.OS)
Output:

:: - xojoc [10/Feb/2015:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "http://xojoc.pw" "Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0"
::
xojoc
10/Feb/2015
/apache_pb.gif
200
2326
http://xojoc.pw
Firefox
GNU/Linux

func Common

func Common(line string) (*Entry, error)

Common parses a log line containing a log entry in the common log format.

An entry in the common log format has the form:

Host - User Time Request Status Bytes

where:

Host is the ip of the client which made the request.
- this field never is used.
User is the name of the logged in user doing the request.
Time is the date/time/zone the request was made.
Request is the HTTP request line from the client.
Status is the status code returned to the client.
Bytes is the size in bytes of the data sent to the client.
Example
l, err := Common(`:: - xojoc [10/Feb/2015:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326`)
if err != nil {
	log.Fatal(err)
}

fmt.Println(l)
fmt.Println(l.Host)
fmt.Println(l.User)
fmt.Println(l.Time.Format("02/Jan/2006"))
fmt.Println(l.Request.URL)
fmt.Println(l.Status)
fmt.Println(l.Bytes)
Output:

:: - xojoc [10/Feb/2015:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 - -
::
xojoc
10/Feb/2015
/apache_pb.gif
200
2326

func (*Entry) String

func (e *Entry) String() string

Formats the Entry e in the combined log format.

type ExtendedDirective

type ExtendedDirective struct {
}

func (*ExtendedDirective) Extended

func (x *ExtendedDirective) Extended(line string) *Entry

Jump to

Keyboard shortcuts

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