Search code examples
spring-cloudlog4j2spring-cloud-config

Unable to utilize log4j-spring-cloud-config-client when Spring Cloud Config uses a backend other than Git or File Based


Apparently, to use the log4j-spring-cloud-config-client with Spring Cloud Config, you need to take advantage of the SearchPathLocator functionality to pull the raw file based on a specific URI. From the Spring-cloud-config code it appears only the JGitEnvironmentRepository and NativeEnvironmentRepository implement that interface and offer that functionality.

Running locally, if I hit the following endpoint, I get back a raw log4j2 config file: http://localhost:8088/config-server-properties-poc/default/master/log4j2.xml.

When I try that with an S3 backend, I get a 404, and it doesn't try to search for that specific file. I was able to work around this by naming my file to log4j2-default.json (XML is not supported). When I hit the following URL, I can get my properties back but not in the correct format http://localhost:8088/log4j2/default

Format

{
    "name": "log4j2",
    "profiles": ["default"],
    "label": null,
    "version": null,
    "state": null,
    "propertySources": [{
            "name": "log4j2",
            "source": {
                "configuration.appenders.appender[0].PatternLayout.Pattern": "${logging_pattern}",
                "configuration.appenders.appender[0].name": "Console",
                "configuration.appenders.appender[0].target": "SYSTEM_OUT",
                "configuration.appenders.appender[0].type": "Console",
                "configuration.loggers.Root.AppenderRef.ref": "Console",
                "configuration.loggers.Root.level": "info",
                "configuration.loggers.logger[0].AppenderRef.ref": "Console",
                "configuration.loggers.logger[0].additivity": "false",
                "configuration.loggers.logger[0].level": "info",
                "configuration.loggers.logger[0].name": "com.paychex",
                "configuration.loggers.logger[1].AppenderRef.ref": "Console",
                "configuration.loggers.logger[1].additivity": "false",
                "configuration.loggers.logger[1].level": "info",
                "configuration.loggers.logger[1].name": "com.paychex.traceability",
                "configuration.loggers.logger[2].AppenderRef.ref": "Console",
                "configuration.loggers.logger[2].level": "WARN",
                "configuration.loggers.logger[2].name": "org.apache.catalina.startup.VersionLoggerListener",
                "configuration.properties.property[0].name": "logging_pattern",
                "configuration.properties.property[0].value": "%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX},severity=%p,thread=%t,logger=%c,%X,%m%n",
                "configuration.properties.property[1].name": "traceability_logging_pattern",
                "configuration.properties.property[1].value": "%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ},severity=%p,thread=%t,logger=%c,%X,%m%n"
            }
        }
    ]
}

As you can see, the properties are wrapped into the Spring Environment object, and the properties are pushed into a Map, so peeling this apart and getting log4j2 to parse it would be tricky.

Has anyone gotten the log4j client to work with a non-git backend?


Solution

  • You are correct. Log4j's support for Spring Cloud Config relies on SCC's support for serving plain text files.

    The latest Spring Cloud Config documentation indicates that plain text support via urls onlys work for Git, SVN, native and AWS S3 but that for S3 to work Spring Cloud AWS must be included in the Config Server. This issue indicates support for serving plain text files from S3 appears to have been added in Spring Cloud Config 2.2.1.Release which was published in Dec 2019. There is still an open issue to add support for a vault backend.

    Log4j's support for SCC was added in the 2.12.0 release in June 2019 when SCC did not yet support AWS S3. I have only tested it with native for unit/functional testing and Git since that is the backend my employer uses. However, according to the documentation if you can get SCC to serve plain text with an AWS backend then Log4j should work as well as all it does is query SCC via URLs.