1 package com.github.triceo.splitlog.splitters;
2
3 import java.util.Calendar;
4 import java.util.Date;
5 import java.util.List;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
8
9 import com.github.triceo.splitlog.api.MessageSeverity;
10 import com.github.triceo.splitlog.api.MessageType;
11
12
13
14
15
16 final public class JBossServerLogTailSplitter extends AbstractTailSplitter {
17
18 private static final int BODY = 11;
19
20 private static final String DATE_SUBPATTERN = "(([01]?[0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9]),([0-9][0-9][0-9])";
21 private static final int HOURS = 2;
22 private static final int MILLIS = 7;
23 private static final int MINUTES = 5;
24 private static final int SECONDS = 6;
25 private static final int SEVERITY = 8;
26
27 private static final String SEVERITY_SUBPATTERN = "[A-Z]+";
28 private static final int TYPE = 9;
29
30 private static final String TYPE_SUBPATTERN = "([a-zA-Z_$][a-zA-Z\\d_$]*\\.)*[a-zA-Z_$][a-zA-Z\\d_$]+";
31 private final Pattern pattern = Pattern.compile("^\\s*(" + JBossServerLogTailSplitter.DATE_SUBPATTERN + ")\\s+("
32 + JBossServerLogTailSplitter.SEVERITY_SUBPATTERN + ")\\s+\\[(" + JBossServerLogTailSplitter.TYPE_SUBPATTERN
33 + ")\\]\\s+(.+)\\s*");
34
35 @Override
36 public Date determineDate(final List<String> raw) {
37 final Matcher m = this.pattern.matcher(raw.get(0));
38 m.matches();
39 final String hours = m.group(JBossServerLogTailSplitter.HOURS);
40 final String minutes = m.group(JBossServerLogTailSplitter.MINUTES);
41 final String seconds = m.group(JBossServerLogTailSplitter.SECONDS);
42 final String millis = m.group(JBossServerLogTailSplitter.MILLIS);
43 final Calendar c = Calendar.getInstance();
44 c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hours));
45 c.set(Calendar.MINUTE, Integer.valueOf(minutes));
46 c.set(Calendar.SECOND, Integer.valueOf(seconds));
47 c.set(Calendar.MILLISECOND, Integer.valueOf(millis));
48 return c.getTime();
49 }
50
51 @Override
52 public String determineLogger(final List<String> raw) {
53 return this.determineLogger(raw.get(0));
54 }
55
56 private String determineLogger(final String line) {
57 final Matcher m = this.pattern.matcher(line);
58 m.matches();
59 final String type = m.group(JBossServerLogTailSplitter.TYPE);
60 if (type.equals("stderr") || type.equals("stdout")) {
61 return null;
62 } else {
63 return type;
64 }
65 }
66
67 @Override
68 public MessageSeverity determineSeverity(final List<String> raw) {
69 final Matcher m = this.pattern.matcher(raw.get(0));
70 m.matches();
71 final String severity = m.group(JBossServerLogTailSplitter.SEVERITY);
72 if (severity.equals("INFO")) {
73 return MessageSeverity.INFO;
74 } else if (severity.equals("DEBUG")) {
75 return MessageSeverity.DEBUG;
76 } else if (severity.equals("WARN")) {
77 return MessageSeverity.WARNING;
78 } else if (severity.equals("ERROR")) {
79 return MessageSeverity.ERROR;
80 } else if (severity.equals("TRACE")) {
81 return MessageSeverity.TRACE;
82 } else {
83 return MessageSeverity.UNKNOWN;
84 }
85 }
86
87 @Override
88 public MessageType determineType(final List<String> raw) {
89 return this.determineType(raw.get(0));
90 }
91
92 private MessageType determineType(final String line) {
93 final Matcher m = this.pattern.matcher(line);
94 m.matches();
95 final String type = m.group(JBossServerLogTailSplitter.TYPE);
96 if (type.equals("stderr")) {
97 return MessageType.STDERR;
98 } else if (type.equals("stdout")) {
99 return MessageType.STDOUT;
100 } else {
101 return MessageType.LOG;
102 }
103 }
104
105 @Override
106 public boolean isStartingLine(final String line) {
107 return this.pattern.matcher(line).matches();
108 }
109
110 @Override
111 public String stripOfMetadata(final String line) {
112 if (this.isStartingLine(line)) {
113 final Matcher m = this.pattern.matcher(line);
114 m.matches();
115 if (this.determineType(line) == MessageType.LOG) {
116 return "[" + m.group(JBossServerLogTailSplitter.TYPE) + "] "
117 + m.group(JBossServerLogTailSplitter.BODY).trim();
118 } else {
119 return m.group(JBossServerLogTailSplitter.BODY).trim();
120 }
121 } else {
122 return line.trim();
123 }
124 }
125
126 }