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 for JSON body pretty-printing #1924

Closed
hurr1canexd opened this issue Sep 27, 2024 · 2 comments
Closed

Support for JSON body pretty-printing #1924

hurr1canexd opened this issue Sep 27, 2024 · 2 comments

Comments

@hurr1canexd
Copy link

hurr1canexd commented Sep 27, 2024

How to pretty-print JSON with different BodyFilters?

Detailed Description

Using Logbook version 2.14.0 I configured BodyFIlter like this:

@Bean
public BodyFilter bodyFilter() {
    return merge(new PrettyPrintingJsonBodyFilter(), replaceJsonStringProperty(Set.of("number"), "XXX"));
}

After I upgraded Logbook version to 3.9.0 I noticed that there is JacksonJsonFieldBodyFilter bean in LogbookAutoConfiguration class. It works after PrettyPrintingJsonBodyFilter and compresses json. I tried various ways to create BodyFilters bean, but don't find good one.

Context

I didn't find in the documentation how to achieve pretty printing of JSON using Logbook. It would be very useful for our cozy community.

Possible Implementation

The only but bad solution I found is just copy JacksonJsonFieldBodyFilter bean from LogbookAutoConfiguration class to my Logbook @Configuration class before PrettyPrintingJsonBodyFilter bean. So PrettyPrintingJsonBodyFilter injects into the Logbook constructor at the end of bodyFIlters list and works at the end of the BodyFilter chain.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.zalando.logbook.BodyFilter;
import org.zalando.logbook.HttpLogWriter;
import org.zalando.logbook.autoconfigure.LogbookProperties;
import org.zalando.logbook.json.JacksonJsonFieldBodyFilter;
import org.zalando.logbook.json.PrettyPrintingJsonBodyFilter;

import java.util.List;

@Configuration
public class LogbookConfig {

    @Bean
    public HttpLogWriter writer(CustomLogbookLoggerProperties properties) {
        return new CustomHttpLogWriter(properties);
    }

    /**
     * It's just a copied implementation of {@link org.zalando.logbook.autoconfigure.LogbookAutoConfiguration#jsonBodyFieldsFilter()}.
     * We need it only to inject JacksonJsonFieldBodyFilter in List<BodyFilter> bodyFilters to
     * {@link org.zalando.logbook.autoconfigure.LogbookAutoConfiguration#logbook} before
     * {@link PrettyPrintingJsonBodyFilter}. So PrettyPrintingJsonBodyFilter works at the end of the filter chain
     */
    @Bean
    public JacksonJsonFieldBodyFilter jsonBodyFieldsFilter(LogbookProperties properties) {
        final LogbookProperties.Obfuscate obfuscate = properties.getObfuscate();
        final List<String> jsonBodyFields = obfuscate.getJsonBodyFields();

        return new JacksonJsonFieldBodyFilter(jsonBodyFields, obfuscate.getReplacement());
    }

    @Bean
    public BodyFilter prettyPrintingJsonBodyFilter() {
        return new PrettyPrintingJsonBodyFilter();
    }
}

Your Environment

Thanks in advance

Copy link
Contributor

In order to prioritize the support for Logbook, we would like to check whether the old issues are still relevant.
This issue has not been updated for over six months.

  • Please check if it is still relevant in latest version of the Logbook.
  • If so, please add a descriptive comment to keep the issue open.
  • Otherwise, the issue will automatically be closed after a week.

@github-actions github-actions bot added the stale label Mar 31, 2025
Copy link
Contributor

This issue has automatically been closed due to no activities. If the issue still exists in the latest version of the Logbook, please feel free to re-open it.

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

No branches or pull requests

1 participant