Search code examples
javaspring-bootloggingwar

spring boot .war tomcat application logs not there


I am trying to write to my webapp logs to a .log file in the /logs directory in tomcat but the file is not generated nor is any logging output going to the console other than the spring logs and tomcat logs. When I run spring boot as a jar file with the embedded tomcat it writes to the log file just fine, but as soon as I deploy to tomcat via the webapps folder the application logs are no where to be found.

SpringBoot 2.1.2 Java 1.8 Tomcat 8.5

I have tried:

  • configuring LOGGING_CONFIG in setenv.sh
  • Multiple loggers.. logback, java utils etc..

application.properties:

logging.file=../logs/my-app.log
logging.level.org.springframework=INFO
logging.level.com.bose=DEBUG

log4j.properties for log4j:

log4j.rootLogger=${marge.log.level}, stdout, file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=marge.log

#when stdout is also specified it will not write to the file

log4j.appender.file.MaxFileSize=1MB
# Keep one backup file
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} [%c] [%-5p] %n%m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# stdout uses PatternLayout
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%c] [%-5p] %n%m%n

# Print only messages of level DEBUG or above in the package com.bose
log4j.logger.com.app=${log.level}

Expected: when I deploy my webapp in /webapps the application logs (generated by log4j) should be in my-app.log in the /logs directory

Actual: No file is generated and no logs are even in stdout/console


Solution

  • By default spring boot uses logback as a logging binder, so the key concept here to exclude logback first then include log4j

    Example:

    <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>
    

    After that add log4j 2 dependency:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    
    • Add your log4j2.properties file to src/main/resources to be on classpath
    • Finally pay attention to what logging interface you are using this is important, with the above configuration you should use apache logging like:

       package com.example;
      
       import org.apache.logging.log4j.LogManager;
       import org.apache.logging.log4j.Logger;
       import org.springframework.boot.SpringApplication;
       import org.springframework.boot.autoconfigure.SpringBootApplication;
       import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
       import org.springframework.context.ApplicationContext;
      
        @SpringBootApplication
        public class Application extends SpringBootServletInitializer {
      
        private static final Logger LOGGER = LogManager.getLogger(Application.class);
      
        public static void main(String[] args){
          ApplicationContext ctx = SpringApplication.run(Application.class, args);
      
          LOGGER.info("Info level log message");
          LOGGER.debug("Debug level log message");
          LOGGER.error("Error level log message");
          }
        }