简体   繁体   中英

logging.config configuration for spring boot

I wanted to configure location of log4j.xml file in my spring boot application. For that I have added logging.config property to my application.properties configuration, indicating log4j.xml file path. But seems this property is ignored. But it should work accorindg to spring boot docs:

logging.config= # location of config file (default classpath:logback.xml for logback)

Have I did something wrong?

Spring Boot includes some starters that can be used if you want to exclude or swap specific technical facets. It's using logback by default, if you're gonna use log4j add spring-boot-starter-log4j in your classpath. For example, with maven it would be something like this:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>1.2.4.RELEASE</version>
</dependency>

and for log4j 1.x:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.2.4.RELEASE</version>
</dependency>

Then add logging.config to your application.properties :

logging.config = classpath:path/to/log4j.xml

I find out that in some cases external logging config(logback.xml)is not ignored: when application is started from application folder, it works properly. Some clarification on this point: application is run through script, which can be called from any place. I have not yet gone deep and found out why it works in that way, but if I provide config file path as an argument during the start up, it will work. So we just add this argument to running script: --spring.config.location=/configPath/application.properties Probably this problem is caused by Spring loading stages. If you have any idea what is the root cause of this problem , please share:)

According to spring boot docs :

If you are using the starter poms for assembling dependencies that means you have to exclude Logback and then include your chosen version of Log4j instead.

like this :

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

I spend few days to understand whether this should even work and I have doubts regarding this. Despite it is clearly mentioned in the documentation how to use Custom Log Configuration , some treat it differently . There many issues regarding this property is not working here and there on spring github issue tracker, like this and this . And another valid point is that logging configuration must be done as earlier as possible to correctly log application initialization. Thus system property looks like most savvy option here. And you can keep it within your application code. The only requirements would be to set it before spring context initialization.

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        // to start from command line
        System.setProperty("logging.config", "classpath:portal-log4j2.yaml");
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // to start within container
        System.setProperty("logging.config", "classpath:portal-log4j2.yaml");
        // this has SpringApplication::run() inside
        super.onStartup(servletContext);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM