当 Spring Cloud Config 使用 Git 或基于文件的后端以外的后端时,无法使用 log4j-spring-cloud-config-client

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

显然,要将 log4j-spring-cloud-config-client 与 Spring Cloud Config 一起使用,您需要利用 SearchPathLocator 功能根据特定的网址。来自 Spring-cloud-config 代码似乎只有 JGitEnvironmentRepositoryNativeEnvironmentRepository 实现了该接口并提供了该功能。

运行 在本地,如果我点击以下端点,我会得到一个原始的 log4j2 配置文件:http://localhost:8088/config-server-properties-poc/default/master/log4j2.xml.

当我尝试使用 S3 后端时,我收到 404,并且它不会尝试搜索该特定文件。我可以通过将我的文件命名为 log4j2-default.json(不支持 XML)来解决这个问题。当我点击以下 URL 时,我可以取回我的属性,但格式不正确 http://localhost:8088/log4j2/default

格式

{
    "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"
            }
        }
    ]
}

如您所见,属性被包装到 Spring Environment 对象中,并且属性被推送到 Map 中,因此将其剥离并让 log4j2 解析它会很棘手。

有人让 log4j 客户端与非 git 后端一起工作吗?

你是对的。 Log4j 对 Spring Cloud Config 的支持依赖于 SCC 对提供纯文本文件的支持。

最新的 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 添加了对保管库后端的支持。

Log4j 对 SCC 的支持是在 2019 年 6 月的 2.12.0 版本中添加的,当时 SCC 还不支持 AWS S3。我只用本机测试过 unit/functional 测试和 Git 因为那是我雇主使用的后端。但是,根据文档,如果您可以让 SCC 通过 AWS 后端提供纯文本,那么 Log4j 应该可以正常工作,因为它所做的只是通过 URL 查询 SCC。