【发布时间】:2017-08-18 12:12:50
【问题描述】:
您好,当我使用 Grok 调试器 https://grokdebug.herokuapp.com/ 进行测试时,我在使用从 Github 下载的代码和 grok api 解析日志文件时遇到问题(我没有使用 logstash) 它工作正常,但在我的代码中它不会
这是我要解析的文件中的日志行:
DEBUG 2015-06-17 14:44:57,475 (com.test.logging.exceptionmanager.ExceptionTreeModel:findNodeByIdRecursively:651) - 找不到 ID 为 1913 的节点
这是我的代码:
public class LogParse {
public static void main(String[] args) throws GrokException {
// Get an instance of grok
Grok grok = new Grok();
// add a pattern to grok
grok.addPattern("LOGLEVEL", "\\w+");
grok.addPattern("YEAR", "\\w+");
grok.addPattern("MONTHNUM", "\\w+");
grok.addPattern("MONTHDAY", "\\w+");
grok.addPattern("HOUR", "\\w+");
grok.addPattern("MINUTE", "\\w+");
grok.addPattern("SECOND", "\\w+");
grok.addPattern("GREEDYDATA", "\\w+");
grok.compile("%{LOGLEVEL:loglevel} %{YEAR:year}-%{MONTHNUM:month}-%{MONTHDAY:day} %{HOUR:hour}:%{MINUTE:minute}:%{SECOND:second} %{GREEDYDATA:data}");
try{
FileInputStream fstream = new FileInputStream("C://file.log");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String log;
//output
BufferedWriter output = new BufferedWriter(new FileWriter("out.json"));
/* read log line by line */
while ((log = br.readLine()) != null) {
System.out.println (log);
Match gm = grok.match(log);
gm.captures();
//output
System.out.println(gm.toJson());
output.write(gm.toJson());
output.newLine();
}
output.close();
fstream.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
} }}
感谢您的帮助。
【问题讨论】:
-
我更新了我的代码,现在它工作正常,这是正则表达式中的问题
-
我已回滚您的更改:请找到 your solution in the revision history 并将其作为自己的答案发布,谢谢。
标签: java json parsing grok logparser