Nginx日誌格式正則表達式

public static void main(String[] args) {
		String mydata = "222.173.59.186 - [222.173.59.86] - - [08/Aug/2019:09:28:38 +0800] \"GET /test/ HTTP/1.1\" 200 4 \"-\" \"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko\" 283 0.003 [default-echoheader-10001] 172.20.1.8:10001 4 0.002 200 8f8190173bbb43862c1b8b43124d67c0";
		// [\\d\\.]+) -> 222.173.59.186
		// \\[([\\d\\.]+)\\] -> 222.173.59.86

		Pattern pattern = Pattern.compile(
				"([\\d\\.]+) - \\[([\\d\\.]+)\\] - - \\[(\\S+) \\S+\\] \"(\\w+) ([^\\\"]*) ([^\\\"]*)\" (\\d+) (\\d+) \"([^\\\"]*)\" \"([^\\\"]*)\" (\\d+) ([\\d\\.]+) \\[([^\\\"]*)\\] ([\\d\\.]+):(\\d+) (\\d+) ([\\d\\.]+) (\\d+) (\\w+)");
		Matcher matcher = pattern.matcher(mydata);
		if (matcher.find()) {
			System.out.println(String.format("the real ip:%s", matcher.group(1)));// the real ip
			System.out.println(String.format("the real ip:%s", matcher.group(2)));// the real ip
			System.out.println(String.format("time_local:%s", matcher.group(3)));
			System.out.println(String.format("method:%s", matcher.group(4)));
			System.out.println(String.format("path:%s", matcher.group(5)));
			System.out.println(String.format("protocol:%s", matcher.group(6)));
			System.out.println(String.format("status:%s", matcher.group(7)));
			System.out.println(String.format("body_bytes_sent:%s", matcher.group(8)));
			System.out.println(String.format("http_referer:%s", matcher.group(9)));// http_referer
			System.out.println(String.format("http_user_agent:%s", matcher.group(10)));// http_user_agent
			System.out.println(String.format("request_length:%s", matcher.group(11)));// request_length
			System.out.println(String.format("request_time:%s", matcher.group(12)));// request_time
			System.out.println(String.format("proxy_upstream_name:%s", matcher.group(13)));// proxy_upstream_name
			System.out.println(String.format("upstream_addr_host:%s", matcher.group(14)));// upstream_addr_host
			System.out.println(String.format("upstream_addr_port:%s", matcher.group(15)));// upstream_addr_port
			System.out.println(String.format("upstream_response_length:%s", matcher.group(16)));// upstream_response_length
			System.out.println(String.format("upstream_response_time:%s", matcher.group(17)));// upstream_response_time
			System.out.println(String.format("upstream_status:%s", matcher.group(18)));// upstream_status
			System.out.println(String.format("req_id:%s", matcher.group(19)));// req_id

		}
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章