spring-attic / spring-cloud-gcp

Integration for Google Cloud Platform APIs with Spring
Apache License 2.0
704 stars 694 forks source link

<customJson> in logback-spring.xml not working with StackDriverJsonLayout #2575

Closed Tritpi closed 3 years ago

Tritpi commented 3 years ago

@meltsufin Im trying to add a custom json to get some custom data added in the console appender with StackDriverJsonLayout. Below is the code from logback-spring.xml:

<if condition='property("LOG_APPENDER").contains("CONSOLE_IN_SD_FORMAT")'>
                <then>
                    <encoder
                        class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
                    <layout
                        class="org.springframework.cloud.gcp.logging.StackdriverJsonLayout">                
                        <customJson>{"product_name":"${PRODUCT_NAME}"</customJson>
                    </layout>
                    </encoder>
                </then> 
                <else>  
                    <encoder>
                            <pattern>${LOG_PATTERN}</pattern>
                            <charset>utf8</charset>
                    </encoder>
                </else>
            </if>

This works in local. But when the application is deployed to GKE and the console logs are checked the custom json field does not appear.

I tried to override the addCustomDataToJsonMap method in a custom class which extends the JsonLayout. But how should this custom class be referred in logback-spring.xml. Also please confirm if this step of overriding the addCustomDataToJsonMap method is really required or not.

Please share an example if possible

meltsufin commented 3 years ago

You shouldn't have to override addCustomDataToJsonMap(). Note that product_name is not supposed to appear as a top level attribute of the LogEntry in the Cloud Logging console. It should appear under jsonPayload. See: https://cloud.google.com/logging/docs/agent/configuration#process-payload. Can you share the LogEntry as it appears in the Logging console?

meltsufin commented 3 years ago

Closing due to inactivity from OP.