java解析log日志

自己写的一段日志解析的java程序,比较乱,先记录(小文件):

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.json.JSONObject;

public class ParseLog {

	private static String VIEWSTRING = "正在查看新闻:";
	private static String APPLYSTRING = "写过的新闻";
	private static String SEARCHSTRING = "正在搜索新闻:";
	private static String USERTAG = "用户:";

	/**
	 * 用戶名,用戶行為,以key-value方式存储
	 * 用户行为 以json方式存储
	 */
	public static void readFileByLines(String fileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			System.out.println("以行为单位读取文件内容,一次读一整行:");
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
			reader = new BufferedReader(isr);
			String tempString = null;
			Map<String, Object> map = new HashMap<String, Object>();
			// 一次读入一行,直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				if (tempString.contains(VIEWSTRING)) {
					int userindex = tempString.indexOf(USERTAG);
					String username = tempString.substring(userindex + 3, tempString.indexOf(",", userindex));
					int done = tempString.indexOf(VIEWSTRING);
					String ewId = tempString.substring(done + VIEWSTRING.length());
					if (username != null && username != "") {
						try {
							if (map.containsKey(username)) {
								JSONObject json = (JSONObject) map.get(username);
								if (json.has("view")) {
									if (!json.get("view").toString().contains(ewId)) {
										json.put("view", json.get("view") + "," + ewId);
									}
								} else {
									json.put("view", ewId);
								}
								map.put(username, json);
							} else {
								JSONObject json = new JSONObject();
								json.put("view", ewId);
								map.put(username, json);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}

				if (tempString.contains(APPLYSTRING)) {
					int userindex = tempString.indexOf(USERTAG);
					String username = tempString.substring(userindex + 3, tempString.indexOf(",", userindex));
					int done = tempString.indexOf(APPLYSTRING);
					String ewId = tempString.substring(done + APPLYSTRING.length());

					if (username != null && username != "") {
						try {
							if (map.containsKey(username)) {
								JSONObject json = (JSONObject) map.get(username);
								if (json.has("apply")) {
									if (!json.get("apply").toString().contains(ewId)) {
										json.put("apply", json.get("apply") + "," + ewId);
									}
								} else {
									json.put("apply", ewId);
								}
								map.put(username, json);
							} else {
								JSONObject json = new JSONObject();
								json.put("apply", ewId);
								map.put(username, json);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}

				if (tempString.contains(SEARCHSTRING)) {
					int userindex = tempString.indexOf(USERTAG);
					String username = tempString.substring(userindex + 3, tempString.indexOf(",", userindex));
					int done = tempString.indexOf(SEARCHSTRING);
					String keyword = URLDecoder.decode(tempString.substring(done + SEARCHSTRING.length()), "utf-8");

					if (username != null && username != "") {
						try {
							if (map.containsKey(username)) {
								JSONObject json = (JSONObject) map.get(username);
								if (json.has("search")) {
									if (!json.get("search").toString().contains(keyword.trim())) {
										json.put("search", json.get("search") + "," + keyword.trim());
									}
								} else {
									json.put("search", keyword.trim());
								}
								map.put(username, json);
							} else {
								JSONObject json = new JSONObject();
								json.put("search", keyword.trim());
								map.put(username, json);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
			reader.close();
			Iterator<Entry<String, Object>> it = map.entrySet().iterator();
			while (it.hasNext()) {
				Entry<String, Object> entry = it.next();
				System.out.println(entry.getKey() + "-----------" + entry.getValue());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	public static void main(String[] args) {
		ParseLog.readFileByLines("C:\\Users\\lyh\\Desktop\\log");
	}

}
 
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解析 log4j 格式的日志文件,你可以使用 Java 日志库和正则表达式来提取所需的信息。下面是一个简单的示例代码,演示了如何解析 log4j 格式的日志文件: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class LogParser { private static final String LOG_PATTERN = "\\[(.*)\\] \\[(.*)\\] \\[(.*)\\] - (.*)"; public static void main(String[] args) { String logFilePath = "path/to/logfile.log"; try (BufferedReader reader = new BufferedReader(new FileReader(logFilePath))) { String line; Pattern pattern = Pattern.compile(LOG_PATTERN); while ((line = reader.readLine()) != null) { Matcher matcher = pattern.matcher(line); if (matcher.matches()) { String timestamp = matcher.group(1); String level = matcher.group(2); String logger = matcher.group(3); String message = matcher.group(4); System.out.println("Timestamp: " + timestamp); System.out.println("Level: " + level); System.out.println("Logger: " + logger); System.out.println("Message: " + message); System.out.println("------------------"); } } } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的代码,我们首先定义了用于匹配 log4j 格式的正则表达式模式。然后,我们读取日志文件的每一行,并使用正则表达式模式匹配每一行。如果匹配成功,我们提取出时间戳、日志级别、日志记录器和消息,并将其打印出来。 你需要将 `logFilePath` 替换为你实际的日志文件路径。注意,这只是一个简单的示例,你可能需要根据实际情况调整正则表达式模式和提取的字段。 希望这个示例能帮助到你!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值