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

How can i disbale to log default params & can i rename "correlation" field #1029

Closed
stephenreg opened this issue May 11, 2021 · 5 comments
Closed

Comments

@stephenreg
Copy link

stephenreg commented May 11, 2021

Hi,

currently i just getting started with the lib in a spring-boot project. I just added the starter to pom and found a way to replace the value of my correlation via using a bean. nice... but, can i also overwrite the field name? i would like to call it tracking_id instead of correlation?

Secondly, is there a way to turn of default log fields like "origin" "type" etc?

This is how my log statment looks like currently:

2021 - 05 - 11 13: 17: 09.954 TRACE 12532-- - [nio - 8082 - exec - 2]org.zalando.logbook.Logbook: { "origin": "remote", "type": "request", "correlation": "de1d86d9-f59e-4e11-b1bf-e5f4b569900c", "protocol": "HTTP/1.1", "remote": "0:0:0:0:0:0:0:1", "method": "POST", "uri": "http://localhost:8082/api/v1/dummy", "host": "localhost", "path": "/api/v1/dummy", "scheme": "http", "port": "8082", "headers": { "accept": ["*/*"], "accept-encoding": ["gzip, deflate, br"], "authorization": ["XXX"], "connection": ["keep-alive"], "content-length": ["27"], "content-type": ["application/json"], "host": ["localhost:8082"], "postman-token": ["750b3646-839c-4593-a50b-83d428942458"], "user-agent": ["PostmanRuntime/7.28.0"] }, "body": { "name": "Dummy" } }

edit: found this in you docs:

Logbook logbook = Logbook.builder() .condition(exclude( requestTo("/health"), requestTo("/admin/**"), contentType("application/octet-stream"), header("X-Secret", newHashSet("1", "true")::contains))) .build();

seems like you can exclude a header "x-secret" via code.. since i am currently just using the spring properties file, is there a way as well or am i mistaken?

@PascalSchumacher
Copy link
Contributor

You can obfuscate headers by setting a spring-boot property:

logbook.obfuscate.headers=[X-Secret]

see: https://github.com/zalando/logbook#configuration

I think customization of fields logged and field names can be done by implementing and registering a custom https://github.com/zalando/logbook/blob/main/logbook-api/src/main/java/org/zalando/logbook/Strategy.java

@whiskeysierra
Copy link
Collaborator

I think customization of fields logged and field names can be done by implementing and registering a custom https://github.com/zalando/logbook/blob/main/logbook-api/src/main/java/org/zalando/logbook/Strategy.java

Not quite. If you want to re-use most of the json mechanics, then a custom HttpLogFormatter would be better.

Take a look at the javadoc for JsonHttpLogFormatter to see how:

/**
* A custom {@link HttpLogFormatter} that produces JSON objects. It can be augmented with composition:
*
* <pre>
* {@code
*
* public class CustomsFormatter implements HttpLogFormatter {
*
* private final JsonHttpLogFormatter delegate;
*
* public CustomsFormatter(ObjectMapper mapper) {
* this.delegate = new JsonHttpLogFormatter(mapper);
* }
*
* public String format(Precorrelation precorrelation, HttpRequest request) throws IOException {
* Map<String, Object> content = delegate.prepare(precorrelation, request);
* // modify request here
* return delegate.format(content);
* }
*
* public String format(Correlation correlation, HttpResponse response) throws IOException {
* Map<String, Object> content = delegate.prepare(correlation, response);
* // modify response here
* return delegate.format(content);
* }
*
* }
* }
* </pre>
*/
@API(status = STABLE)
public final class JsonHttpLogFormatter implements StructuredHttpLogFormatter {

@whiskeysierra
Copy link
Collaborator

Within those format methods you get a map that contains e.g. origin, type, etc. You can modify that map to your liking, e.g. drop keys or rename them.

@whiskeysierra
Copy link
Collaborator

Feel free to re-open, if this doesn't suite your needs.

@stephenreg
Copy link
Author

Thanks i will take a look next week !

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