Spring Boot - no log file written (logging.file is not respected)

前端 未结 15 1460
一向
一向 2020-12-14 00:01

I use Spring Boot and want it to write log output to a file.

According to the docs, this is simply done by setting

logging.file=filename.log
<         


        
15条回答
  •  独厮守ぢ
    2020-12-14 00:55

    Sorry for the late reply. It seems spring's logger reads the property from its own classpath.Due to precedence, it's not respecting the properties supplied.

    Some tricks to get around:

    1. In main class set the property variable using springApplication.setDefaultProperties(properties); like this
    public static void main(String[] args) {
            SpringApplication springApplication = new SpringApplication(MainClass.class);
            Properties properties = new Properties();
            properties.put("logging.file", logDirectory);
            springApplication.setDefaultProperties(properties);
            springApplication.run(args);
        }
    
    1. Pass the property as JVM parameter -Dlogging.file=/location/output.log.

    Both of the above are not the best ones as in order to define other logging properties they also should follow the same way.

    Solution

    Define a property file and put all you logging configurations in that and specify the file in -Dspring.config.location. This is a derivation of my other problem and this is how I resolved that. check that out in order to know other solutions that I've tried and their challenges.

提交回复
热议问题