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

Create @LogIgnore as Complement to @JsonIgnore #3

Open
vjkoskela opened this issue Jun 4, 2015 · 0 comments
Open

Create @LogIgnore as Complement to @JsonIgnore #3

vjkoskela opened this issue Jun 4, 2015 · 0 comments

Comments

@vjkoskela
Copy link
Member

Create an additional annotation @LogIgnore and associated filter to augment @JsonIgnore for logging. Additionally, the configuration for @LogIgnore should allow disabling of @JsonIgnore (e.g. to ignore @JsonIgnore).

This is similar to the relationship between @LogValue and @JsonValue. The pattern allows developers to create a distinct serialization profile of a class for logging and another one for standard serialization/deserialization. It is particularly important when the existing serialization profile already exists and logging is being migrated to Logback-Steno.

Disabling @JsonIgnore and using @LogIgnore instead would accomplish at least part of this. We will still need to investigate how to support customized serialization and the annotations that come with it.

See: FasterXML/jackson-databind#133

Reference implementation of annotation:

package com.arpnetworking.logback.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation that marks a field to be ignored by the RedactionFilter in StenoEncoder.
*
* @author Ville Koskela (vkoskela at groupon dot com)
* @see com.arpnetworking.logback.jackson.RedactionFilter
* @since 1.9.0
*/
@Target( { ElementType.METHOD, ElementType.FIELD } )
@Retention(RetentionPolicy.RUNTIME)
public @interface LogIgnore {
}

Reference implementation of filter:

package com.arpnetworking.logback.jackson;
import java.util.Collections;
import java.util.Set;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.arpnetworking.logback.annotations.LogIgnore;

/**
 * Jackson property filter that suppresses fields annotated with @LogIgnore.
 *
 * @author Ville Koskela (vkoskela at groupon dot com)
 * @since 1.9.0
 */
public class IgnoreFilter extends SimpleBeanPropertyFilter.SerializeExceptFilter {
    private static final long serialVersionUID = -8732095428853471218L;
    public static final String IGNORE_FILTER_ID = "com.arpnetworking.logback.jackson.IgnoreFilterId";

    public IgnoreFilter() { 
        this(Collections.<String>emptySet()); 
    }

    public IgnoreFilter(final Set<String> properties) { 
        super(properties); 
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @Deprecated
    public void serializeAsField(
            final Object pojo,
            final JsonGenerator jgen,
            final SerializerProvider prov,
            final BeanPropertyWriter writer) throws Exception {
        if (writer.getAnnotation(LogIgnore.class) == null) { 
            super.serializeAsField(pojo, jgen, prov, writer); 
        }
        // else: ignore field entirely
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void serializeAsField(
            final Object pojo,
            final JsonGenerator jgen,
            final SerializerProvider prov,
            final PropertyWriter writer) throws Exception {
        if (writer instanceof BeanPropertyWriter) { 
                final BeanPropertyWriter beanPropertyWriter = (BeanPropertyWriter) writer; 
                if (beanPropertyWriter.getAnnotation(LogIgnore.class) == null) { 
                    super.serializeAsField(pojo, jgen, prov, writer); 
                } 
                // else
        } else { 
            super.serializeAsField(pojo, jgen, prov, writer); 
        }
    }
}
@BrandonArp BrandonArp assigned matthayter and unassigned matthayter Jun 15, 2015
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

2 participants