Applications can die, but logs must live.


Both large and small systems require logging.

The system has different requirements on log configuration in different environments

Such as

Develop local: output directly to the console

Production environment: Output to a file or an additional log collection system, such as Graylog.

(This article does not discuss specific logging system configuration.)

Spring Boot official solution

Profile-specific Configuration

  • Advantages: Almost no changes, just the system configuration file specifying spring.profiles.action=
  • Disadvantages: Multiple environment configurations are mixed together, and the application runtime should be unaware of the running configuration.

To give up.

The configuration logging. Config

You need to specify logging.config in the configuration file

Multiple log configuration files are required

e.g


logging.config=classpath:logback-console.xml
logging.config=classpath:logback-file.xml
logging.config=classpath:logback-graylog.xml
Copy the code

Is feasible.

Fall to the ground

The configuration logging.config scheme is adopted

Background: The company configuration file is published to the specified directory on the application machine through the distribution system based on the file-based configuration center. The application machine always reads the configuration file in the specified directory.

A few questions

  1. How to read the application configuration of a specified directory?
  2. How do I ensure that the specified config is read during log initialization?

Official external configuration file loading sequence

As can be seen from the document, the default Spring Boot application configuration file classpath: application. The propreties is loaded on the sequence of the 15th.

Application properties packaged inside your jar (application.properties and YAML variants).

@PropertySource ?

The 16th overall pick.

However, things are not so simple.

The log system needs to be initialized as early as possible due to its particularity.

Since logging is initialized before the ApplicationContext is created, it is not possible to control logging from @PropertySources in Spring @Configuration files. The only way to change the logging system or disable it entirely is via System properties. The logging system is initialized before the ApplicationContext is created, so the @propertysource configuration does not affect the logging system initialization.

That the road is blocked

Command line arguments ?

Moving up the configuration order, command line parameters can be applied at relatively low cost.

e.g

  public static void main(String[] args) throws UnknownHostException {
    System.out.println(new Date() + " loaded...");
    args = new String[1];
    args[0] =
      "--spring.config.location=/etc/conf/app/xxx/application.properties";

    ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
    ConfigurableEnvironment env = context.getEnvironment();

    log.info("\n----------------------------------------------------------\n\t" +
        "Application '{}' is running! Access URLs:\n\t" +
        "Local: \t\thttp://localhost:{}\n\t" +
        "External: \thttp://{}:{}\n\t" +
        "Profile(s): \t{}\n----------------------------------------------------------",
      env.getProperty("spring.application.name"),
      env.getProperty("server.port"),
      InetAddress.getLocalHost().getHostAddress(),
      env.getProperty("server.port"),
      env.getActiveProfiles());

    System.out.println(new Date() + " started...");
  }
Copy the code

As expected, the logging system uses the correct specified configuration.

Come to an end

The problem is basically solved, but it is not elegant, hack command line arguments, and has some limitations. Let’s shelve it here.

Afterword.

  • Is there a more elegant way to solve this problem?
  • Why is the Spring Boot configuration file loading order defined like this?
  • How is the Spring Boot load configuration implemented?
  • How is the configuration of Spring Cloud Config and other configuration centers integrated into specific applications?
  • Does configuration center configuration like Spring Cloud Config support configuration of log system configuration file location? .

Wait for the subsequent disassembly and analysis.

REF

  • Externalized Configuration
  • Custom Log Configuration
  • Application Property Files