SAP / cf-java-logging-support

The Java Logging Support for Cloud Foundry supports the creation of structured log messages and the collection of request metrics
Apache License 2.0
76 stars 46 forks source link

Provide option to not emit fields with default values "-". #99

Closed KarstenSchnitter closed 2 years ago

KarstenSchnitter commented 3 years ago

cf-java-logging-support attaches a default set of fields to every log messages defined as CTX_FIELDS in LogContext:

private static Map<String, String> CTX_FIELDS = new HashMap<String, String>() {
    {
        put(Fields.CORRELATION_ID, Defaults.UNKNOWN);
        put(Fields.TENANT_ID, Defaults.UNKNOWN);
        put(Fields.TENANT_SUBDOMAIN, Defaults.UNKNOWN);
        put(Fields.COMPONENT_ID, Defaults.UNKNOWN);
        put(Fields.COMPONENT_NAME, Defaults.UNKNOWN);
        put(Fields.COMPONENT_TYPE, Defaults.COMPONENT_TYPE);
        put(Fields.COMPONENT_INSTANCE, Defaults.COMPONENT_INDEX);
        put(Fields.CONTAINER_ID, Defaults.UNKNOWN);
        put(Fields.ORGANIZATION_ID, Defaults.UNKNOWN);
        put(Fields.ORGANIZATION_NAME, Defaults.UNKNOWN);
        put(Fields.SPACE_ID, Defaults.UNKNOWN);
        put(Fields.SPACE_NAME, Defaults.UNKNOWN);
    }
};

These fields are populated from environment variables using VcapEnvReader except for CORRELATION_ID, TENANT_ID and TENANT_SUBDOMAIN. If variables are unknown, cf-java-logging-support will still add the fields to the generated log message with a value of "-". This behaviour enlarges the log messages without giving any benefit.

Compare the following test messages, the first containing the default values, the second with the respective fields removed:

{ "written_at":"2021-02-28T09:18:33.452Z","written_ts":1614503913455790000,"tenant_id":"-","component_type":"application","component_id":"-","space_name":"-","component_name":"-","component_instance":"0","organization_id":"-","correlation_id":"-","organization_name":"-","space_id":"-","container_id":"-","tenant_subdomain":"-","type":"log","logger":"com.sap.hcp.cf.logging.common.TestAppLog","thread":"main","level":"INFO","categories":[],"msg":"Running test()" }
{ "written_at":"2021-02-28T09:19:06.653Z","written_ts":1614503946656499000,"component_type":"application","component_instance":"0","type":"log","logger":"com.sap.hcp.cf.logging.common.TestAppLog","thread":"main","level":"INFO","categories":[],"msg":"Running test()" }

Their sizes are 465 vs 268 characters. In the example 200 bytes of unnecessary log volume was created. There should be an option to suppress the generation of context fields with default values. So that instead of the first message of the example the library would generate the second message of the example.