Skip to content

Instantly share code, notes, and snippets.

@ntlk
Last active August 29, 2015 14:16
Show Gist options
  • Save ntlk/c72760ad82b0733a7f9e to your computer and use it in GitHub Desktop.
Save ntlk/c72760ad82b0733a7f9e to your computer and use it in GitHub Desktop.
Ologline
let log_string = "61.135.219.2 - - [01/Mar/2015:21:42:13 +0300] \"GET /atom.xml HTTP/1.1\" 304 - \"-\" \"Mozilla/5.0 (compatible;YoudaoFeedFetcher/1.0;http://www.youdao.com/help/reader/faq/topic006/;1 subscribers;)\"";
type logline = {
remote_host: string;
client_identity: string;
userid: string;
timestamp: string;
request_line: string;
status_code: int;
object_returned_size: string;
referer: string;
user_agent: string;
};;
let convert_line_to_logline line =
let ip_regex = Str.regexp "^\\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\) \\[\\(.*\\)\\] \"\\([^\"]*\\)\" \\([0-9]+\\) \\([^ ]+\\) \"\\([^\"]+\\)\" \"\\([^\"]+\\)\"" in
let matches = Str.string_match ip_regex line 0 in
if matches then
let remote_host = Str.matched_group 1 line in
let client_identity = Str.matched_group 2 line in
let userid = Str.matched_group 3 line in
let timestamp = Str.matched_group 4 line in
let request_line = Str.matched_group 5 line in
let status_code = Str.matched_group 6 line in
let object_returned_size = Str.matched_group 7 line in
let referer = Str.matched_group 8 line in
let user_agent = Str.matched_group 9 line in
Some { remote_host = remote_host;
client_identity = client_identity;
userid = userid;
timestamp = timestamp;
request_line = request_line;
status_code = int_of_string status_code;
object_returned_size = object_returned_size;
referer = referer;
user_agent = user_agent;
}
else
None
;;
let logline_to_string line =
Printf.sprintf "IP: %s\nClient identity: %s\nUserid: %s\nTimestamp: %s\nRequest line: %s\nStatus code: %d\nObject returned size: %s\nReferer: %s\nUser agent: %s" line.remote_host line.client_identity line.userid line.timestamp line.request_line line.status_code line.object_returned_size line.referer line.user_agent
let () =
let logline_record = convert_line_to_logline log_string in
match logline_record with
| Some l -> print_string (logline_to_string l)
| None -> print_string "couldn’t parse line"
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment