logparse: xojoc.pw/logparse Index | Examples | Files

package logparse

import "xojoc.pw/logparse"

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

Index

Examples

Package Files

lex.go log.go

type Entry Uses

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 Uses

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

Code:

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 Uses

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.

Code:

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 Uses

func (e *Entry) String() string

Formats the Entry e in the combined log format.

type ExtendedDirective Uses

type ExtendedDirective struct {
}

func (*ExtendedDirective) Extended Uses

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

Package logparse imports 10 packages (graph). Updated 2020-01-23. Refresh now. Tools for package owners.