Untitled
unknown
plain_text
3 years ago
10 kB
3
Indexable
package pl.inpost.parcellogisticstatusprovider.application.log;
import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.toMap;
import static pl.inpost.parcellogisticstatusprovider.application.log.LogType.PARCEL_SCAN_EVENT;
import brave.Span;
import brave.Tracer;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.stereotype.Component;
import pl.inittec.logging.logstash.LogstashHelper;
import pl.inittec.logging.logstash.soap.HttpMethodDirection;
import pl.inittec.logging.logstash.soap.HttpMethodMode;
import pl.inittec.logging.logstash.text.TextLog;
@Component
@Slf4j
@AllArgsConstructor
public class Logger {
private static final String SPAN_ID = "spanID";
private static final String REQUEST = "request";
private final Tracer tracer;
public void scanParcelEvent(String parcelNumber, ScanParcelEvent event, String scanPlace, String userLogin, String methodName) {
log.info("Scan parcel event (parcelNumber: {}, event: {}, scanPlace: {}, userLogin: {}, methodName: {})", parcelNumber, event, scanPlace,
userLogin, methodName);
LogstashHelper.getLogstashTextLog()
.withCustomField(SPAN_ID, getSpanId())
.withCustomField("parcelNumber", parcelNumber)
.withCustomField("event", event.toString())
.withCustomField("scanPlace", scanPlace)
.withCustomField("userLogin", userLogin)
.withCustomField("methodName", methodName)
.withCustomField("logType", PARCEL_SCAN_EVENT.toString())
.send();
}
public void request(String method, String path, String query, String body, Map<String, String> headers) {
log.info(header("Received REST request")
+ "URI : {}\n"
+ "Method : {}\n"
+ "Headers : {}\n"
+ "Request body : {}\n"
+ footer(), path, method, headers, body);
LogstashHelper.getLogstashRestLog()
.withDirection(HttpMethodDirection.request)
.withMode(HttpMethodMode.server)
.withIID(getSpanId())
.withRequestMethod(method)
.withRequestPath(path)
.withRequestQueryParams(query)
.withRequestEntity(body)
.withHttpHeaders(headers)
.withCustomLogField(SPAN_ID, getSpanId())
.send();
}
public void response(long executionTime, String body, int statusCode, Map<String, String> headers, String request) {
log.info(header("Returning REST response")
+ "Status code : {}\n"
+ "Headers : {}\n"
+ "Execution time : {}ms\n"
+ "Response body : {}\n"
+ footer(), statusCode, headers, executionTime, body);
LogstashHelper.getLogstashRestLog()
.withDirection(HttpMethodDirection.response)
.withMode(HttpMethodMode.server)
.withIID(getSpanId())
.withExecutionTime(executionTime)
.withResponseEntity(body)
.withResponseStatus(statusCode)
.withHttpHeaders(headers)
.withCustomLogField(SPAN_ID, getSpanId())
.withCustomLogField(REQUEST, request)
.send();
}
public void feignRequest(String method, String path, String body, Map<String, Collection<String>> headers) {
Map<String, String> flatHeaders = headers.entrySet().stream()
.collect(toMap(Map.Entry::getKey, stringCollectionEntry -> String.join(",", stringCollectionEntry.getValue())));
log.info(header("Sending REST request")
+ "URI : {}\n"
+ "Method : {}\n"
+ "Headers : {}\n"
+ "Request body : {}\n"
+ footer(), path, method, flatHeaders, body);
LogstashHelper.getLogstashRestLog()
.withDirection(HttpMethodDirection.request)
.withMode(HttpMethodMode.client)
.withIID(getSpanId())
.withRequestMethod(method)
.withRequestPath(path)
.withRequestEntity(body)
.withHttpHeaders(flatHeaders)
.withCustomLogField(SPAN_ID, getSpanId())
.send();
}
public void feignResponse(long executionTime, String body, int statusCode, Map<String, Collection<String>> headers) {
Map<String, String> flatHeaders = headers.entrySet().stream()
.collect(toMap(Map.Entry::getKey, stringCollectionEntry -> String.join(",", stringCollectionEntry.getValue())));
log.info(header("Received REST response")
+ "Status code : {}\n"
+ "Headers : {}\n"
+ "Execution time : {}ms\n"
+ "Response body : {}\n"
+ footer(), statusCode, flatHeaders, executionTime, body);
LogstashHelper.getLogstashRestLog()
.withDirection(HttpMethodDirection.response)
.withMode(HttpMethodMode.client)
.withIID(getSpanId())
.withExecutionTime(executionTime)
.withResponseEntity(body)
.withResponseStatus(statusCode)
.withHttpHeaders(flatHeaders)
.withCustomLogField(SPAN_ID, getSpanId())
.send();
}
public void soapRequest(String uri, String body) {
log.info(header("Sending SOAP request")
+ "URI : {}\n"
+ "Request body : {}\n"
+ footer(), uri, body);
LogstashHelper.getLogstashSoapLog()
.client()
.outgoingMessage()
.withUri(uri)
.withBody(body)
.send();
}
public void soapResponse(String uri, String body, long executionTime) {
log.info(header("Received SOAP response")
+ "URI : {}\n"
+ "Response body : {}\n"
+ "Execution time : {}ms\n"
+ footer(), uri, body, executionTime);
LogstashHelper.getLogstashSoapLog()
.client()
.incomingMessage()
.withUri(uri)
.withBody(body)
.withExecutionTime(executionTime)
.send();
}
public void soapError(String uri, String body) {
log.error("Error: {uri: {}, body: {}}", uri, body);
LogstashHelper.getLogstashSoapLog()
.client()
.incomingMessage()
.fault()
.withUri(uri)
.withBody(body)
.send();
}
public void info(String messagePattern, Object... argArray) {
log.info(messagePattern, argArray);
FormattingTuple formattingTuple = MessageFormatter.arrayFormat(messagePattern, argArray);
String message = formattingTuple.getMessage();
String spanId = getSpanId();
TextLog textLog = LogstashHelper.getLogstashTextLog()
.withCustomField(SPAN_ID, spanId);
if (nonNull(message)) {
textLog.withMessage(message);
}
textLog.send();
}
public void info(LogType logType, String messagePattern, Object... argArray) {
log.info(messagePattern, argArray);
FormattingTuple formattingTuple = MessageFormatter.arrayFormat(messagePattern, argArray);
String message = formattingTuple.getMessage();
String spanId = getSpanId();
TextLog textLog = LogstashHelper.getLogstashTextLog()
.withCustomField(SPAN_ID, spanId)
.withCustomField("logType", logType.toString());
if (nonNull(message)) {
textLog.withMessage(message);
}
textLog.send();
}
public void error(String messagePattern, Object arg) {
error(messagePattern, new Object[] {arg});
}
public void error(String messagePattern, Object arg1, Object arg2) {
error(messagePattern, new Object[] {arg1, arg2});
}
public void error(String messagePattern, Object... argArray) {
log.error(messagePattern, argArray);
FormattingTuple formattingTuple = MessageFormatter.arrayFormat(messagePattern, argArray);
String message = formattingTuple.getMessage();
Exception exception = getException(formattingTuple);
String spanId = getSpanId();
TextLog textLog = LogstashHelper.getLogstashTextLog()
.withCustomField(SPAN_ID, spanId);
if (nonNull(message)) {
textLog.withMessage(message);
}
if (nonNull(exception)) {
textLog.withException(exception);
}
textLog.send();
}
public void error(LogType logType, String messagePattern, Object... argArray) {
log.error(messagePattern, argArray);
FormattingTuple formattingTuple = MessageFormatter.arrayFormat(messagePattern, argArray);
String message = formattingTuple.getMessage();
Exception exception = getException(formattingTuple);
String spanId = getSpanId();
TextLog textLog = LogstashHelper.getLogstashTextLog()
.withCustomField(SPAN_ID, spanId)
.withCustomField("logType", logType.toString());
if (nonNull(message)) {
textLog.withMessage(message);
}
if (nonNull(exception)) {
textLog.withException(exception);
}
textLog.send();
}
private String getSpanId() {
Span currentSpan = tracer.currentSpan();
if (currentSpan == null) {
currentSpan = tracer.nextSpan().name(UUID.randomUUID().toString());
}
return currentSpan.context().spanIdString();
}
private Exception getException(FormattingTuple formattingTuple) {
try {
return (Exception) formattingTuple.getThrowable();
} catch (Exception ex) {
log.error("Couldn't cast {} to Exception", formattingTuple.getThrowable(), formattingTuple.getThrowable());
return null;
}
}
private String header(String title) {
return "\n================================== " + title + " ==================================\n";
}
private String footer() {
return "===========================================================================================";
}
}
Editor is loading...