Skip to content

Instantly share code, notes, and snippets.

@marciol
Created March 24, 2023 14:25
Show Gist options
  • Save marciol/98753d222f5ece8aa353f529046dcabd to your computer and use it in GitHub Desktop.
Save marciol/98753d222f5ece8aa353f529046dcabd to your computer and use it in GitHub Desktop.
Zalando Logbook Request-Response in one line configuration.
package com.acme
import org.zalando.logbook.DefaultHttpLogWriter;
@EnableFeignClients
@SpringBootApplication
public class Application {
@Generated
public static void main( String[] args ) {
SpringApplication.run(Application.class, args);
}
@Bean
public Sink logRequestResponse() {
return new LogRequestAndResponseTogetherSink(
new RequestAndReponseJsonHttpLogFormatter(),
new DefaultHttpLogWriter());
}
}
package com.acme.configuration;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.AllArgsConstructor;
import org.zalando.logbook.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@AllArgsConstructor
public class LogRequestAndResponseTogetherSink implements Sink {
private final RequestAndReponseJsonHttpLogFormatter formatter;
private final HttpLogWriter writer;
@Override
public boolean isActive() {
return writer.isActive();
}
@Override
public void write(final Precorrelation precorrelation, final HttpRequest request) throws IOException {
}
@Override
public void write(final Correlation correlation, final HttpRequest request, final HttpResponse response)
throws IOException {
try {
writer.write(correlation, formatter.format(correlation, request, response));
} catch (IOException e) {
log.error("LogRequestAndResponseTogetherSink Error - {}", e);
}
}
}
package com.acme.configuration;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.zalando.logbook.*;
import org.zalando.logbook.json.JsonHttpLogFormatter;
import java.io.IOException;
import java.util.*;
public class RequestAndReponseJsonHttpLogFormatter implements StructuredHttpLogFormatter {
private final ObjectMapper mapper;
private final JsonHttpLogFormatter jsonHttpLogFormatter;
public RequestAndReponseJsonHttpLogFormatter() {
this(new ObjectMapper(), new JsonHttpLogFormatter());
}
public RequestAndReponseJsonHttpLogFormatter(final ObjectMapper mapper, final JsonHttpLogFormatter jsonHttpLogFormatter) {
this.mapper = mapper;
this.jsonHttpLogFormatter = jsonHttpLogFormatter;
}
public Optional<Object> prepareBody(final HttpMessage message) throws IOException {
return this.jsonHttpLogFormatter.prepareBody(message);
}
public String format(final Correlation correlation, final HttpRequest request, final HttpResponse response) throws IOException {
Map<String, Object> requestMap = prepare(correlation, request);
Map<String, Object> responseMap = prepare(correlation, response);
Map<String, Object> content = new LinkedHashMap();
content.put("requestMessage", requestMap);
content.put("responseMessage", responseMap);
return this.format(content);
}
public String format(final Map<String, Object> content) throws IOException {
return this.mapper.writeValueAsString(content);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment