spring-boottomcatlogback

Spring boot logback to file is not working on tomcat


I am running a spring boot app packaged as a War on tomcat with logback to console and file.

As long as I run as Java application, it is fine. I can see logs in console and file. However, I don't see logs printed to file when run on the server.

I tried setting logger manager, which didn't work. I was wondering to know if some one has faced similar issue.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <property name="LOG_FILE" 
     value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}app.log}"/>    
    <property name="LOG_FILE_MAX_SIZE" value="10MB" />
    <property name="LOG_TOTAL_SIZE_CAP" value="100MB" />
    <property name="LOG_FILE_MAX_HISTORY" value="20" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

Solution

  • I could fix it by writing the logs to a different folder looks application doesn't have write access the path, however i need to make some changes to springboot main class for loading application props based profile, please find the class below. not sure if others had to the same.

    anyway i am glad it is working finally:)

    public class Application extends SpringBootServletInitializer{

    public String PROFILE = null;
    private static String CONFIG_LOCATION = null;
    
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //Grab the active profile from the servlet conext
        PROFILE = servletContext.getInitParameter("spring.profiles.active");
        CONFIG_LOCATION = servletContext.getInitParameter("spring.config.path");        
        super.onStartup(servletContext);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        //...and pass it to the boot application
        application.application().setAdditionalProfiles(PROFILE);
        return application.sources(Application.class).properties(getProperties());
    }
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    //For Loading config from server 
    static Properties getProperties() {
        Properties props = new Properties();
        props.put("spring.config.location", CONFIG_LOCATION);
        return props;
    }
    

    }

    web.xml

    <context-param>
     <param-name>spring.profiles.active</param-name>
     <param-value>dev</param-value>
    </context-param>
    
    <context-param>
     <param-name>spring.config.path</param-name>    
     <param-value>classpath:app.config/</param-value>
    </context-param>