2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2018 Amdocs
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.logging.ref.slf4j.analysis;
24 import java.util.Calendar;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.HashMap;
30 import javax.xml.bind.DatatypeConverter;
32 import org.apache.commons.lang3.StringUtils;
33 import org.apache.commons.lang3.builder.ToStringBuilder;
34 import org.slf4j.event.Level;
37 * Test class for reading a logentry during analysis.
39 public class LogEntry {
42 private final Date mTimestamp;
45 private final String mThread;
48 private final Level mLevel;
51 private final String mLogger;
54 private final String mMessage;
57 private final String mException;
60 private final Map<String, String> mMDCs;
63 private final String mMarkers;
66 * Construct from log line.
67 * @param line to be parsed.
69 public LogEntry(final String line) {
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);
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++];
84 this.mMDCs = parseMDCs(tokens[index++]);
85 this.mMessage = tokens[index++];
86 this.mException = tokens[index++];
87 this.mMarkers = tokens[index++];
91 * Parse serialized MDCs.
92 * @param mdc serialized DMC map.
95 static Map<String, String> parseMDCs(final String mdc) {
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]));
104 return Collections.unmodifiableMap(mdcs);
111 public Date getTimestamp() {
112 return this.mTimestamp;
119 public String getThread() {
127 public Level getLevel() {
135 public String getLogger() {
143 public String getMessage() {
144 return this.mMessage;
151 public String getException() {
152 return this.mException;
159 public Map<String, String> getMDCs() {
167 public String getMarkers() {
168 return this.mMarkers;
175 public String getRequestID() {
176 return this.getMDCs().get("RequestID");
183 public String getInvocationID() {
184 return this.getMDCs().get("InvocationID");
191 public String getPartnerName() {
192 return this.getMDCs().get("PartnerName");
199 public String getInvokingID() {
200 if (StringUtils.defaultString(this.getMarkers()).startsWith("INVOKE")) {
201 return this.getMessage();
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));
217 if (StringUtils.isNotBlank(this.getInvocationID())) {
218 buf.append(", invocationID=[...]").append(StringUtils.right(this.getInvocationID(), 8));
220 if (StringUtils.isNotBlank(this.getInvokingID())) {
221 buf.append(", invokingID=[...]").append(StringUtils.right(this.getInvokingID(), 8));
224 final Calendar c = Calendar.getInstance();
225 c.setTime(this.getTimestamp());
227 buf.append(", timestamp=").append(DatatypeConverter.printDateTime(c));
228 return buf.append(")").toString();
235 public String toString() {
236 return ToStringBuilder.reflectionToString(this);