0
votes

I am using Spring Boot and log4j2 for my logging because I want my logs to be written on a file instead of console. So I have implemented log4j2.properties and kept in under resource folder in Spring Boot project.

When I start Spring Boot application, I couldn't see the log file and logs.

if added below configuration in application.properties then logs are not getting in console and in file.

logging.config=classpath:log4j2.properties

but added console and file appenders in log4j2.properties file.

below are the details:

log4j2.properties

# Root logger option
log4j.rootLogger=INFO, file, DEBUG, stdout


# configuration to print into file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:/Logs/app.log
log4j.appender.file.MaxFileSize=12MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


# configuration to print on console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

DemoApplication.java

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

  private final static Logger log = LogManager.getLogger(DemoApplication.class);

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

    return application.sources(DemoApplication.class);
  }

  public static void main(String[] args) {

    SpringApplication.run(DemoApplication.class, args);
    log.info("**** Demo Application Started *****");

  }

}

POM.XML

<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>

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

      <!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-log4j2</artifactId>
      </dependency>

      <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
    </dependency>

Can anyone please help me on this.

2
have you got the solution for thisdijo francis

2 Answers

0
votes

Try adding the following in your application.properties file:

logging.level.my.base.package=TRACE
logging.level.org.springframework=INFO
logging.level.org.hibernate=INFO 

where my.base.package represents the base package of your entire project

-1
votes

Did you add into application.properties?

logging.config=classpath:log4j2.properties

Here you can found a complete example

Best Regards.