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

PatternJsonProvider: omitEmptyFields doesn't work with %property(ENV_VAR) #765

Closed
brenuart opened this issue Mar 9, 2022 Discussed in #760 · 0 comments · Fixed by #772
Closed

PatternJsonProvider: omitEmptyFields doesn't work with %property(ENV_VAR) #765

brenuart opened this issue Mar 9, 2022 Discussed in #760 · 0 comments · Fixed by #772
Milestone

Comments

@brenuart
Copy link
Collaborator

brenuart commented Mar 9, 2022

Discussed in #760

Excerpt from original post by ziomill February 16, 2022
I'm trying to use the "omitEmptyFields" to print/omit an ENV variable, without success. If the ENV_VAR 'CONTAINER_NAMESPACE' is not populated, the logger prints "null" as the field's value rather than omit the field.
...
This is my configuration:

<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
    <providers>
        ...
        <pattern>
             <omitEmptyFields>true</omitEmptyFields>
             <pattern>
                 {"container-namespace":"%property{CONTAINER_NAMESPACE}"}
             </pattern>
        </pattern>
        ...
    </providers>
</encoder>

And this is the printed log record:

{
   ...
   "container-namespace":"null"
   ...
}

Root Cause

The Pattern json provider is using a standard Logback PatternLayout under the hood to resolve placeholders and converters in the pattern string. The resulting string is then converted into JSON and added to the final result. The "omitEmptyFields" property works by telling Jackson to omit fields whose value is considered empty.

When a property does not exist, the %property{} converter returns null and this value is unfortunately added "asis" to the internal StringBuilder. In this example, the original pattern is converted into the string {"container-namespace":"null"} before it is transformed into a valid JSON structure by Jackson. Since the property is not null but is assigned a String value, it is not filtered out by Jackson's "empty field" filtering mechanism.

@brenuart brenuart added this to the 7.1 milestone Mar 9, 2022
@brenuart brenuart changed the title PatternJsonProvider - omitEmptyFields doesn't work with %property(ENV_VAR) PatternJsonProvider: omitEmptyFields doesn't work with %property(ENV_VAR) Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant