Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support customisation of logged payload #6

Closed
hdpe opened this issue May 19, 2018 · 5 comments
Closed

Support customisation of logged payload #6

hdpe opened this issue May 19, 2018 · 5 comments
Assignees
Milestone

Comments

@hdpe
Copy link
Contributor

hdpe commented May 19, 2018

As discussed, it would be good to be able to customise the formatting of the request/response body before logging.

My particular use case is the need to obfuscate passwords sent in the request before writing to the log.

As a very first stab at this, how about allowing the LoggingInterceptor to be constructed with a LoggingFormatter, something like:

public interface LoggingFormatter {
	
	String formatRequest(HttpRequest request, byte[] body);
	
	String formatResponse(ClientHttpResponse response);
}

with a default implementation:

public class DefaultLoggingFormatter implements LoggingFormatter {
	
	@Override
	public String formatRequest(HttpRequest request, byte[] body) {
		// tmpl method to generate log line from URL, method, body...
	}
	
	@Override
	public String formatResponse(ClientHttpResponse response) {
		// tmpl method to generate log line from response status, body...
	}
	
	protected String formatBody(byte[] body, Charset charset) {
		return new String(body, charset);
	}
	
	protected Charset getCharset(HttpMessage message)
	{
		// ...
	}
}

This would support resolution of e.g. #2 as well. Then perhaps #1 via introducing something like a supports(MediaType) method to the formatter?

What do you think? Happy to prepare a PR following your feedback.

@markhobson
Copy link
Owner

I like it a lot! It certainly provides a good place to address the other issues as you say. Thanks Ryan 👍

P.S. A pedant would say LogFormatter as it's not actually doing the logging.

@markhobson markhobson added this to the 2.0.0 milestone Jul 28, 2018
@markhobson markhobson self-assigned this Jul 28, 2018
@markhobson
Copy link
Owner

Thanks for the PR!

@jmart1
Copy link

jmart1 commented Sep 9, 2018

Do you know when this version will be published to Maven Central?

@markhobson
Copy link
Owner

@jmart1 I've just released 2.0.0, so it should be there shortly. Thanks for the reminder!

@jmart1
Copy link

jmart1 commented Sep 9, 2018

Yup its there. Thank you!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants