Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - log4j2.properties creating log files but not writing the logs in file

I have used log4j2.properties file with springboot application. Log file was creating but logs are not written into the file.

Please find the details as below:

log4j2.properties

name=PropertiesConfig
property.filename = C:/Logs
appenders = console, file

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/app.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

loggers=file
logger.file.name=com.java.app //Parent Package name for the application 
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

POM.XML

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

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

         <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.11.1</version>
    </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.11.1</version>
    </dependency>

DemoApplication.java

package com.java.app;

    @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) {

        log.info("Logger enabled: Entering main \\n\\n");
        SpringApplication.run(DemoApplication.class, args);
        log.info("**** Demo Application Started *****");

      }

    }

Logs are appearing in the console but not written into file as i am not getting the issue.

It's strange, parent package logger "Logger enabled: Entering main \n\n" is written into the file and the other parent logger "**** Demo Application Started *****" is not written into the file as the code is shown above. and also checked for the sub package i.e com.java.app.endpoint loggers even those also not written into the file.

and also identified that the console log is coming like as

2018-08-03 12:55:18.302 INFO 11440 --- [nio-8088-exec-1] c.j.c.e.Classname : logger message

If c.j.c.e. coming as prefix to the class name in logs those are not written into the file why?

I might be doing something wrong. Can anyone please help on this.

like image 918
Rajeswari Reddy Avatar asked Dec 10 '22 05:12

Rajeswari Reddy


1 Answers

I also face with this problem following the tutorial on springframework.guru. After searching on spring boot docs then I config my pom.xml with those dependencies

<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-log4j2</artifactId>
    </dependency>

and add logging.config=src/main/resources/log4j2.properties in the application.properties file. After that I can see the log appear on my log file when I run the application.

like image 198
K.tin Avatar answered Jan 08 '23 15:01

K.tin