From d1f26741bddf6f512d62c0100d42c52be8d37e76 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期六, 06 二月 2021 15:35:40 +0800
Subject: [PATCH] 工具类优化

---
 utils/src/main/java/org/yeshi/utils/statistic/http/LocalhostAccessLogUtil.java |  169 ++++++++++++++++++++++++++++++++------------------------
 1 files changed, 96 insertions(+), 73 deletions(-)

diff --git a/utils/src/main/java/org/yeshi/utils/statistic/http/LocalhostAccessLogUtil.java b/utils/src/main/java/org/yeshi/utils/statistic/http/LocalhostAccessLogUtil.java
index 5a9b63a..6790ca6 100644
--- a/utils/src/main/java/org/yeshi/utils/statistic/http/LocalhostAccessLogUtil.java
+++ b/utils/src/main/java/org/yeshi/utils/statistic/http/LocalhostAccessLogUtil.java
@@ -1,73 +1,96 @@
-package org.yeshi.utils.statistic.http;
-
-import java.util.List;
-
-public class LocalhostAccessLogUtil {
-
-	/**
-	 * 灏嗗崟琛屾棩蹇楁枃浠惰В鏋愪负瀵硅薄
-	 * 
-	 * @param content
-	 * @return
-	 */
-	public static HttpRequestInfo parseTomcatSingleLine(String content) {
-
-		try {
-			HttpRequestInfo info = new HttpRequestInfo();
-			int start = content.indexOf("\"");
-			content = content.substring(start, content.length());
-			start = content.indexOf("\"", 1);
-			String url = content.substring(1, start);
-			info.setMethod(url.split(" ")[0]);
-			// 閾炬帴
-			info.setUrl(url.split(" ")[1].split("\\?")[0]);
-			// 鏃堕棿
-			info.setProtocal(url.split(" ")[2]);
-			String[] sts = content.substring(start + 1, content.length()).trim().split(" ");
-			info.setStateCode(Integer.parseInt(sts[0]));
-			info.setTime(sts[1].trim().equalsIgnoreCase("-") ? Integer.MAX_VALUE : Integer.parseInt(sts[1]));
-			return info;
-		} catch (Exception e) {
-		}
-
-		return null;
-	}
-
-	/**
-	 * 姹傝姹傛椂闂寸殑骞冲潎鍊�
-	 * 
-	 * @param list
-	 * @return
-	 */
-	public static int getTimeAvg(List<HttpRequestInfo> list) {
-		int count = 0;
-		long total = 0;
-		for (HttpRequestInfo info : list) {
-			// 鍘婚櫎鏈�澶ф暟鍊间笌鏈�灏忔暟鍊�
-			if (info.getTime() > 0 && info.getTime() < Integer.MAX_VALUE) {
-				total += info.getTime();
-				count++;
-			}
-		}
-		if (count <= 0)
-			return 0;
-
-		return (int) (total / count);
-	}
-
-	/**
-	 * 鑾峰彇鏂瑰樊
-	 * 
-	 * @param list
-	 * @return
-	 */
-	public static int getVariance(List<HttpRequestInfo> list) {
-		int avg = getTimeAvg(list);
-		long total = 0;
-		for (HttpRequestInfo info : list) {
-			total += Math.pow(info.getTime() - avg, 2);
-		}
-		return (int) (total / list.size());
-	}
-
-}
+package org.yeshi.utils.statistic.http;
+
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+
+public class LocalhostAccessLogUtil {
+
+	/**
+	 * 灏嗗崟琛屾棩蹇楁枃浠惰В鏋愪负瀵硅薄
+	 * 
+	 * @param content
+	 * @return
+	 */
+	public static HttpRequestInfo parseTomcatSingleLine(String content) {
+
+		try {
+			HttpRequestInfo info = new HttpRequestInfo();
+			info.setIp(content.split("- -")[0]);
+			content = content.split("- -")[1];
+			
+
+			int start = content.indexOf("\"");
+			String time = content.substring(0, start).trim().replace("[", "").replace("]", "");
+			info.setDate(parseDate(time));
+			content = content.substring(start, content.length());
+			start = content.indexOf("\"", 1);
+			String url = content.substring(1, start);
+			info.setMethod(url.split(" ")[0]);
+			// 閾炬帴
+			info.setUrl(url.split(" ")[1].split("\\?")[0]);
+			// 鏃堕棿
+			info.setProtocal(url.split(" ")[2]);
+			String[] sts = content.substring(start + 1, content.length()).trim().split(" ");
+			info.setStateCode(Integer.parseInt(sts[0]));
+			info.setTime(sts[1].trim().equalsIgnoreCase("-") ? Integer.MAX_VALUE : Integer.parseInt(sts[1]));
+
+			return info;
+		} catch (Exception e) {
+		}
+
+		return null;
+	}
+
+	private static Date parseDate(String timeDesc) {
+		DateFormat format = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z", Locale.US);
+		try {
+			Date date = format.parse(timeDesc);
+			return date;
+		} catch (ParseException e) {
+			e.printStackTrace();
+		}
+		return null;
+	}
+
+	/**
+	 * 姹傝姹傛椂闂寸殑骞冲潎鍊�
+	 * 
+	 * @param list
+	 * @return
+	 */
+	public static int getTimeAvg(List<HttpRequestInfo> list) {
+		int count = 0;
+		long total = 0;
+		for (HttpRequestInfo info : list) {
+			// 鍘婚櫎鏈�澶ф暟鍊间笌鏈�灏忔暟鍊�
+			if (info.getTime() > 0 && info.getTime() < Integer.MAX_VALUE) {
+				total += info.getTime();
+				count++;
+			}
+		}
+		if (count <= 0)
+			return 0;
+
+		return (int) (total / count);
+	}
+
+	/**
+	 * 鑾峰彇鏂瑰樊
+	 * 
+	 * @param list
+	 * @return
+	 */
+	public static int getVariance(List<HttpRequestInfo> list) {
+		int avg = getTimeAvg(list);
+		long total = 0;
+		for (HttpRequestInfo info : list) {
+			total += Math.pow(info.getTime() - avg, 2);
+		}
+		return (int) (total / list.size());
+	}
+
+}

--
Gitblit v1.8.0