SLF4J adapter (in 'common') + call graph demo
[logging-analytics.git] / reference / slf4j-reference / src / test / java / org / onap / logging / ref / slf4j / analysis / LogEntry.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.logging
4  * ================================================================================
5  * Copyright © 2018 Amdocs
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *    http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.logging.ref.slf4j.analysis;
23
24 import java.util.Calendar;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import javax.xml.bind.DatatypeConverter;
31
32 import org.apache.commons.lang3.StringUtils;
33 import org.apache.commons.lang3.builder.ToStringBuilder;
34 import org.slf4j.event.Level;
35
36 /**
37  * Test class for reading a logentry during analysis.
38  */
39 public class LogEntry {
40
41     /** Property. */
42     private final Date mTimestamp;
43
44     /** Property. */
45     private final String mThread;
46
47     /** Property. */
48     private final Level mLevel;
49
50     /** Property. */
51     private final String mLogger;
52
53     /** Property. */
54     private final String mMessage;
55
56     /** Property. */
57     private final String mException;
58
59     /** Property. */
60     private final Map<String, String> mMDCs;
61
62     /** Property. */
63     private final String mMarkers;
64
65     /**
66      * Construct from log line.
67      * @param line to be parsed.
68      */
69     public LogEntry(final String line) {
70
71         final String [] tokens = line.split("\t", -1);
72         if (tokens.length < 8) {
73             throw new IllegalArgumentException("Unsupported line (expected 8+ tokens, got "
74                     + tokens.length + "): " + line);
75         }
76
77         int index = 0;
78
79         this.mTimestamp = DatatypeConverter.parseDateTime(tokens[index++]).getTime();
80         this.mThread = tokens[index++];
81         this.mLevel = Level.valueOf(tokens[index++].trim());
82         this.mLogger = tokens[index++];
83
84         this.mMDCs = parseMDCs(tokens[index++]);
85         this.mMessage = tokens[index++];
86         this.mException = tokens[index++];
87         this.mMarkers = tokens[index++];
88     }
89
90     /**
91      * Parse serialized MDCs.
92      * @param mdc serialized DMC map.
93      * @return parsed.
94      */
95     static Map<String, String> parseMDCs(final String mdc) {
96
97         final Map<String, String> mdcs = new HashMap<>();
98         for (final String token : mdc.split(",")) {
99             final String[] mdcTokens = token.split("=");
100             if (mdcTokens.length == 2) {
101                 mdcs.put(StringUtils.trim(mdcTokens[0]), StringUtils.trim(mdcTokens[1]));
102             }
103         }
104         return Collections.unmodifiableMap(mdcs);
105     }
106
107     /**
108      * Getter.
109      * @return property.
110      */
111     public Date getTimestamp() {
112         return this.mTimestamp;
113     }
114
115     /**
116      * Getter.
117      * @return property.
118      */
119     public String getThread() {
120         return this.mThread;
121     }
122
123     /**
124      * Getter.
125      * @return property.
126      */
127     public Level getLevel() {
128         return this.mLevel;
129     }
130
131     /**
132      * Getter.
133      * @return property.
134      */
135     public String getLogger() {
136         return this.mLogger;
137     }
138
139     /**
140      * Getter.
141      * @return property.
142      */
143     public String getMessage() {
144         return this.mMessage;
145     }
146
147     /**
148      * Getter.
149      * @return property.
150      */
151     public String getException() {
152         return this.mException;
153     }
154
155     /**
156      * Getter.
157      * @return property.
158      */
159     public Map<String, String> getMDCs() {
160         return this.mMDCs;
161     }
162
163     /**
164      * Getter.
165      * @return property.
166      */
167     public String getMarkers() {
168         return this.mMarkers;
169     }
170
171     /**
172      * Getter.
173      * @return property.
174      */
175     public String getRequestID() {
176         return this.getMDCs().get("RequestID");
177     }
178
179     /**
180      * Getter.
181      * @return property.
182      */
183     public String getInvocationID() {
184         return this.getMDCs().get("InvocationID");
185     }
186
187     /**
188      * Getter.
189      * @return property.
190      */
191     public String getPartnerName() {
192         return this.getMDCs().get("PartnerName");
193     }
194
195     /**
196      * Getter.
197      * @return property.
198      */
199     public String getInvokingID() {
200         if (StringUtils.defaultString(this.getMarkers()).startsWith("INVOKE")) {
201             return this.getMessage();
202         }
203         return null;
204     }
205
206     /**
207      * Getter.
208      * @return property.
209      */
210     public String toShortString() {
211         final StringBuilder buf = new StringBuilder();
212         buf.append("LogEntry(markers=").append(StringUtils.defaultString(this.getMarkers()));
213         buf.append(", logger=").append(this.getLogger().substring(1 + this.getLogger().lastIndexOf(".")));
214         if (StringUtils.isNotBlank(this.getRequestID())) {
215             buf.append(", requestID=[...]").append(StringUtils.right(this.getRequestID(), 8));
216         }
217         if (StringUtils.isNotBlank(this.getInvocationID())) {
218             buf.append(", invocationID=[...]").append(StringUtils.right(this.getInvocationID(), 8));
219         }
220         if (StringUtils.isNotBlank(this.getInvokingID())) {
221             buf.append(", invokingID=[...]").append(StringUtils.right(this.getInvokingID(), 8));
222         }
223
224         final Calendar c = Calendar.getInstance();
225         c.setTime(this.getTimestamp());
226
227         buf.append(", timestamp=").append(DatatypeConverter.printDateTime(c));
228         return buf.append(")").toString();
229     }
230
231     /**
232      * {@inheritDoc}
233      */
234     @Override
235     public String toString() {
236         return ToStringBuilder.reflectionToString(this);
237     }
238 }