-
Hello, 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. Note that the "omitEmptyFields" is working when used with %mdc directive. This is my configuration: trace_id <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<!-- TIMESTAMP field (UTC) -->
<timestamp>
<fieldName>Timestamp</fieldName>
<timeZone>UTC</timeZone>
</timestamp>
<!-- Include TRACE-ID field from MDC -->
<pattern>
<omitEmptyFields>true</omitEmptyFields>
<pattern>
{"TraceId":"%mdc{trace_id}"}
</pattern>
</pattern>
<!-- Include SPAN-ID field from MDC -->
<pattern>
<omitEmptyFields>true</omitEmptyFields>
<pattern>
{"SpanId":"%mdc{span_id}"}
</pattern>
</pattern>
<!-- LOG_LEVEL field -->
<logLevel>
<fieldName>SeverityText</fieldName>
</logLevel>
<!-- MESSAGE field -->
<message>
<fieldName>Body</fieldName>
</message>
<!-- RESOURCE nested object -->
<nestedField>
<fieldName>Resource</fieldName>
<providers>
<!-- NAMESPACE field -->
<pattern>
<omitEmptyFields>true</omitEmptyFields>
<pattern>
{"container-namespace":"%property{CONTAINER_NAMESPACE}"}
</pattern>
</pattern>
</providers>
</nestedField>
</providers>
</encoder>
<appended> And this is the printed log record: {
"Timestamp":"2022-02-16T16:20:14.530Z",
"SeverityText":"INFO",
"Body":"gRPC Server started, listening on address: *, port: 11001",
"Resource":{
"container-namespace":"null"
}
} Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
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 When a property does not exist, the A String property is considered empty if it is empty or null. A possible approach is to default the property to an empty string if it does not exist (see Default value for variables). Something like this:
Unfortunately this won't work as expected and the variable is resolved into An alternative when using SpringBoot is to leveraged the
Finally, if your CONTAINER_NAMESPACE property is set once and for all when the application is started (i.e. does not change at runtime), then you could guard part of thee Logback configuration elements with a Janino Hope this helps. PS: |
Beta Was this translation helpful? Give feedback.
-
As a stop gap measure, you can define a variable named EMPTY with the value "" and use it as the default value:
|
Beta Was this translation helpful? Give feedback.
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 returnsnull
and unfortunately the PatternLayout appends this value "asis" to its internal StringBuilder. So in your case your pattern is converted into the string{"container-namespace":"null"}
before it is transformed into a valid JSON structure by Jackson. As you case see, the property is not null but is ass…