嗨,我有以下模式的日志文件-
2014-03-06 03:21:45,432 ERROR [mfs:pool-3-thread-19] dispatcher.StatusNotification - Error processing notification. Operation aborted.
java.sql.SQLException: Network error IOException: Connection timed out: connect
2014-03-06 03:22:06,454 ERROR [mfs:pool-3-thread-19] dispatcher.ClientStatusNotification - Error processing notification. Operation aborted.
java.sql.SQLException: Network error IOException: Connection timed out: connect
2014-03-06 03:22:27,462 ERROR [pool-1-thread-1] cluster.ClusterServiceImpl - unexpected error when trying to update LastCheckinTime
java.sql.SQLException: Network error IOException: Connection timed out: connect
...
我正在尝试将字符串拆分为子字符串,以便-
parsedString[0]=2014-03-06 03:21:45
parsedString[1]=,432 ERROR [mfs:pool-3-thread-19] dispatcher.StatusNotification - Error processing notification. Operation aborted.
java.sql.SQLException: Network error IOException: Connection timed out: connect
parsedString[2]=2014-03-06 03:22:06
....
我尝试使用,string.split(datepattern)
但是它只给我字符串数组中的内容,而不是日期。我也尝试使用模式匹配器,但它只给我一个匹配日期列表,而不是内容列表。
如何将两个值都放入同一个字符串数组中。任何帮助将非常感激。谢谢
编辑-字符串模式=“([[0-9] {4}-[0-1] [0-9]-[0-3] [0-9] \ s(?:[0-1] [0- 9] | [2] [0-3]):[0-5] [0-9]:[0-5] [0-9],)“;字符串parsedLogMessage [] = GetLogString()。split(pattern); this.MessageContent = Arrays.asList(parsedLogMessage);
这仅提供由正则表达式分割的字符串,而不是正则表达式字符串本身
每次\n
发现逗号或换行符时,都会拆分字符串:
String[] parsedString = logString.split("(,|\n)");
它应该产生所需的输出,但是我预见到这里几乎没有潜在的问题:
首先,我感觉到您正在尝试首先将整个日志文件加载到字符串中。如果按行处理它们,这将浪费大量内存(如果日志文件为10GB,会发生什么情况?)。更好的方法是使用BufferedReader并按行执行。
其次,请记住,日志输出本身可以包含逗号,因此上面的代码有问题。由于前缀部分似乎是固定长度的,因此您可能需要使用子字符串来将它们切碎。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句