Log4j Spring Cloud 配置

当 Spring Cloud Configuration 中有新版本可用时,此模块允许动态更新日志记录配置文件。

Overview

Spring Boot 应用程序初始化日志记录 3 次。 1. SpringApplication 声明一个 Logger。将使用 Log4j 的“常规”机制初始化此 Logger。因此,将检查名为 log4j2.configurationFile 的系统属性,以查看是否提供了特定的配置文件,否则它将在 Classpath 上搜索配置文件。该属性也可以在 log4j2.component.properties 中声明。

Usage

指定监视间隔大于零的 Log4j 配置文件将使用轮询来确定配置是否已更新。如果监视间隔为零,则 Log4j 将侦听来自 Spring Cloud Config 的通知,并在每次生成事件时检查配置更改。如果监视间隔小于零,则 Log4j 将不检查日志配置的更改。

引用位于 Spring Cloud Config 中的配置时,应引用类似于以下内容的配置

log4j.configurationFile=http://host.docker.internal:8888/ConfigService/sampleapp/default/master/log4j2.xml

Log4j 还支持复合配置。执行此操作的标准方法是用逗号分隔的字符串合并文件的路径。不幸的是,Spring 验证了提供的 URL,并且不允许使用逗号。因此,必须提供其他配置作为“替代”查询参数。

log4j.configurationFile=http://host.docker.internal:8888/ConfigService/sampleapp/default/master/log4j2.xml
?override=http://host.docker.internal:8888/ConfigService/sampleapp/default/master/log4j2-sampleapp.xml

请注意,目录结构中的位置以及配置文件的位置完全取决于 Spring Cloud Config 服务器中的 searchPaths 设置。

在 docker 容器中运行时,host.docker.internal 可以用作访问在 docker 容器外部相同软管上运行的应用程序的域名。请注意,按照 Spring Cloud Config 的惯例,但是应在 url 中指定应用程序,配置文件和标签。

Spring Cloud Config 支持还允许使用 TLS 和/或基本身份验证进行连接。使用基本身份验证时,可以将用户标识和密码指定为系统属性,log4j2.component.properties 或 Spring Boot 的 bootstrap.yml。下表显示了可用于指定属性的备用名称。任何替代方案都可以在任何配置位置中使用。

PropertyAliasSpring-like aliasPurpose
log4j2.configurationUserNamelog4j2.config.usernamelogging.auth.username基本认证的用户名
log4j2.configurationPasswordlog4j2.config.passwordlogging.auth.password基本认证密码
log4j2.authorizationProviderlog4j2.config.authorizationProviderlogging.auth.authorizationProvider用于创建 HTTP 授权 Headers 的类
log4j2.configurationUserName=guest
log4j2.configurationPassword=guest

如上所述,Log4j 支持从 bootstrap.yml 访问日志记录配置。例如,要使用基本授权配置从 Spring Cloud Configuration 服务读取的内容,您可以执行以下操作:

spring:
  application:
    name: myApp
  cloud:
    config:
      uri: https://spring-configuration-server.mycorp.com
      username: appuser
      password: changeme

logging:
  config: classpath:log4j2.xml
  label: ${spring.cloud.config.label}

---
spring:
  profiles: dev

logging:
  config: https://spring-configuration-server.mycorp.com/myApp/default/${logging.label}/log4j2-dev.xml
  auth:
    username: appuser
    password: changeme

请注意,Log4j 当前不直接支持加密密码。但是,Log4j 确实使用 Spring 的标准 API 来访问 Spring 配置中的属性,因此对 Spring 的属性处理所做的任何自定义也将应用于 Log4j 所使用的属性。

如果需要更广泛的身份验证,则可以使用 log4j2.authorizationProvider 密钥或键日志记录来实现 AuthorizationProvider,并在 log4j2.authorizationProvider 系统属性,log4j2.component.properties 或 Spring 的 bootstrap.yml 中实现标准类名。 auth.authorizationProvider。

可以通过添加以下系统属性或在 log4j2.component.properties 中定义它们来启用 TLS

Property可选或默认值Description
log4j2.trustStoreLocationOptional信任库的位置。如果未提供,将使用默认信任库。
log4j2.trustStorePasswordOptional访问信任库所需的密码。
log4j2.trustStorePasswordFileOptional包含信任存储区密码的文件的位置。
log4j2.trustStorePasswordEnvironmentVariableOptional包含信任库密码的环境变量的名称。
log4j2.trustStoreKeyStoreType如果提供了密钥库位置,则为必需密钥库的类型。
log4j2.trustStoreKeyManagerFactoryAlgorithmOptionalJava 加密算法。
log4j2.keyStoreLocationOptional密钥库的位置。如果未提供,将使用默认密钥库。
log4j2.keyStorePasswordOptional访问密钥库所需的密码。
log4j2.keyStorePasswordFileOptional包含密钥存储区密码的文件的位置。
log4j2.keyStorePasswordEnvironmentVariableOptional包含密钥库密码的环境变量的名称。
log4j2.keyStoreType如果提供了信任库位置,则为必需。密钥库的类型。
log4j2.keyStoreKeyManagerFactoryAlgorithmOptionalJava 加密算法。
log4j2.sslVerifyHostNamefalse对或错

Requirements

Log4j 2 Spring Cloud Configuration 集成对 Log4j 2 API,Log4j 2 Core 和 Spring Cloud Configuration 版本 2.0.3.RELEASE 或 2.1.1.RELEASE 或更高版本的依赖关系。有关更多信息,请参见Runtime Dependencies