springspring-bootspring-cloudlog4j2spring-cloud-config

how to centralize log4j2 configuration with Spring Cloud Config Server


I'm using Spring Cloud Config Server File System Backend : docs.spring.io

I have a multi maven project

project:
├── pom.xml
│
├── project-config-provider
│   ├── pom.xml
│   ├── src
│   │   └── main
│   │       ├── java
│   │       │   └── com
│   │       │       └── company
│   │       │           └── project
│   │       │               └── config_provider
│   │       │                   └── Application.java
│   │       └── resources
│   │           └── log4j2-spring.yaml
│   │
│   │
│   ├── config
│   │   ├── application_config-provider--native.yaml
│   │   └── application_config-provider--aws.yaml
│   │
│   └── cloud_config
│       ├── application-config_main.yaml
│       ├── application-config_storage.yaml
│       ├── application-config_swagger.yaml
│       └── application-env_local.yaml
│
│
└── project-abc
    ├── pom.xml
    ├── src
    │   └── main
    │       ├── java
    │       │   └── com
    │       │       └── company
    │       │           └── project
    │       │               └── abc
    │       │                   └── AbcApplication.java
    │       └── resources
    │           └── log4j2-spring.yaml
    │ 
    └── config
        └── application_abc.yaml

The module project-config-provider is the spring cloud config server, and the module project-abc will use some of the configs from project-config-provider.

each module has a config folder:

application_config-provider--native.yaml :

spring:
   application:
      name: config-provider
   profiles:
      active: native
      include: config_main
   cloud:
      config:
         server:
            native:
               search-location: cloud_config
            prefix: /config
            bootstrap: true

server.port: 8888

application_abc.yaml :

spring:
   application:
      name: maps
      version: 1.0.0
   profiles:
      #declare the configurations to include from cloud_config folder of config-provider 
      include: config_main, config_storage, config_swagger
   config:
      import: configserver:http://${CONFIG_PROVIDER_HOST:localhost}:${CONFIG_PROVIDER_PORT:8888}/config

I want to move the log4j2-spring.yaml under cloud_config folder, so it will be centralized, and I don't have to duplicate it for each maven module of the project.

The issue is that it seems to be required to be in the src/main/resources.


Solution

  • If you are using Spring Cloud Config for your application configuration then it makes perfect sense to use it for the log4j configuration as well. To do this you would usually configure your application's bootstrap.yml with something like:

    spring:
      application:
        name: abc
      cloud:
        config:
        uri: https://spring-configuration-server.company.com/
    

    To add the logging configuration you need to add log4j-spring-boot-n.n.n.jar as a dependency of your application and then add

      logging
        config: "https://spring-configuration-server.company.com/log4j2.xml"
    

    The path of to the file depends on how your SCC service is configured to search for files.

    By default, the only remote protocol allowed for loading configuration files is HTTPS. If you need to enable HTTP, you should also add

    log4j2.Configuration.allowedProtocols: "http,https"
    

    If you want the logging configuration to automatically update you can either configure your SCC to generate events to Spring Cloud Bus and then configure your app to receive them by adding the log4j-spring-cloud-client jar and configuring Spring Cloud Bus in your app to receive the events sent by the SCC service or you can configure Log4j to poll for changes by specifying