Last active
August 29, 2015 14:08
-
-
Save falzm/465ddda65fbd1f8732bf to your computer and use it in GitHub Desktop.
Quick'n dirty HTTP combined access log JSON formatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"regexp" | |
) | |
func main() { | |
scanner := bufio.NewScanner(os.Stdin) | |
lineJson := []byte{} | |
re := regexp.MustCompile(`^(?P<remote_addr>[0-9a-f.:]+)\s` + | |
`(?P<clientid>[\w.-]+)\s` + | |
`(?P<remote_user>[\w.-]+)\s` + | |
`\[(?P<date>[^\[\]]+)\]\s` + | |
`"(?:(?P<method>[A-Z]+)\s` + | |
`(?P<request_uri>.+)\s` + | |
`(?P<protocol>HTTP/[0-9.]+)|-)"\s` + | |
`(?P<status>\d{3})\s` + | |
`(?P<size>\d+|-)\s` + | |
`"(?P<referrer>(?:[^"]|\")+)"\s` + | |
`"(?P<ua>(?:[^"]|\")*)"$`) | |
for scanner.Scan() { | |
line := scanner.Bytes() | |
fmt.Println(string(re.Expand(lineJson, []byte(`{`+ | |
`"request_method":"${method}",`+ | |
`"request_uri":"${request_uri}",`+ | |
`"server_protocol":"${protocol}",`+ | |
`"remote_addr":"${remoteaddr}",`+ | |
`"remote_user":"${userid}",`+ | |
`"date":"${date}",`+ | |
`"status":${status},`+ | |
`"body_bytes_sent":${size}`+ | |
`}`), line, re.FindSubmatchIndex(line)))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: pipe your HTTP access logs in "combined" format to it and it will output the JSON-formatted version to
stdout
:Feel free to hack the format of the output JSON, mine was pretty much tailored to my needs.