fork(4) download
  1. import java.util.*;
  2. import java.util.regex.*;
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6. class Ideone
  7. {
  8. final static String timestampRgx = "(?<timestamp>\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3})";
  9. final static String levelRgx = "(?<level>INFO|ERROR|WARN|TRACE|DEBUG|FATAL)";
  10. final static String classRgx = "\\[(?<class>[^\\]]+)]";
  11. final static String threadRgx = "\\[(?<thread>[^\\]]+)]";
  12. final static String textRgx = "(?<text>.*?)(?=\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3}|\\Z)";
  13. private static Pattern PatternFullLog = Pattern.compile(timestampRgx + " " + levelRgx + "\\s+" + classRgx + "-" + threadRgx + "\\s+" + textRgx, Pattern.DOTALL);
  14.  
  15. public static void main (String[] args) throws java.lang.Exception
  16. {
  17. String line = "2017-03-14 22:43:14,405 FATAL [org.springframework.web.context.support.XmlWebApplicationContext]-[localhost-startStop-1] Refreshing Root WebApplicationContext: startup date [Tue Mar 14 22:43:14 UTC 2017]; root of context hierarchy\n2017-03-14 22:43:14,476 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[localhost-startStop-1] Loading XML bean definitions from Serv\n2017-03-14 22:43:14,476 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[localhost-startStop-1] Here is a multiline\nlog entry with another entry after\n2017-03-14 22:43:14,476 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[localhost-startStop-1] Here is a multiline\nlog entry with no entries after";
  18. Matcher matcher = PatternFullLog.matcher(line);
  19. while(matcher.find()) {
  20. System.out.println(matcher.group("timestamp"));
  21. System.out.println(matcher.group("level"));
  22. System.out.println(matcher.group("class"));
  23. System.out.println(matcher.group("thread"));
  24. System.out.println(matcher.group("text"));
  25.  
  26. System.out.println("----");
  27. }
  28. }
  29. }
Success #stdin #stdout 0.06s 4386816KB
stdin
Standard input is empty
stdout
2017-03-14 22:43:14,405
FATAL
org.springframework.web.context.support.XmlWebApplicationContext
localhost-startStop-1
Refreshing Root WebApplicationContext: startup date [Tue Mar 14 22:43:14 UTC 2017]; root of context hierarchy

----
2017-03-14 22:43:14,476
INFO
org.springframework.beans.factory.xml.XmlBeanDefinitionReader
localhost-startStop-1
Loading XML bean definitions from Serv

----
2017-03-14 22:43:14,476
INFO
org.springframework.beans.factory.xml.XmlBeanDefinitionReader
localhost-startStop-1
Here is a multiline
log entry with another entry after

----
2017-03-14 22:43:14,476
INFO
org.springframework.beans.factory.xml.XmlBeanDefinitionReader
localhost-startStop-1
Here is a multiline
log entry with no entries after
----