4

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.

8
  • Can you try to configure using log4j2-spring.xml as Spring Doc's recommend? docs.spring.io/spring-boot/docs/current/reference/html/… Aug 2, 2018 at 11:24
  • Our Requirement is to develop logging using properties file so trying.as following based on this https://springframework.guru/log4j-2-configuration-using-properties-file/ . i couldn't find exactly what is the issue here. Thanks @ Jose A. Matarán Aug 2, 2018 at 12:07
  • Did you try to change name of log to a name without path? Aug 2, 2018 at 12:13
  • is property.filename = C:/Logs this? @Jose A. Matarán Aug 2, 2018 at 12:17
  • put property.filename = logs Aug 2, 2018 at 12:19

6 Answers 6

11

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.

1
  • 1
    Weard: Without the logging.config property, the file seems to not be loaded correctly (I say correctly, because the log file is created but the logs are not written in it)
    – Mohicane
    Oct 26, 2018 at 14:51
2

Using Spring Boot you can specify log4j2.properties inside the application.properties

logging.config=src/main/resources/log4j2.properties
2
  • No, It's not working even added above in application.properties and i have seen some blogs that if we are using 2.4 greater version of log4j2 then mapping also not required. if the filename is log4j2.properties then it will automatically picks up and execute. Thanks @Ashraf Sarhan Aug 2, 2018 at 10:07
  • 1
    Better logging.config=classpath:log4j2.xml Jul 4, 2020 at 16:25
2

Change the POM.xml file as below and it should be working.

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
             <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

<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>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Please note that, Recommendation is to use log4j2.xml instead of log4j2.properties.

0

it also seems to be necessary to set log4j as standard apache logging. In general it uses logback. So I had to add

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

to my pom.xml

0

Spring Boot supports Log4j 2 for logging configuration, you can get how to configure from Configure Log4j for Logging

First you need to exclude spring boot 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>

Then have log4j2.json or log4j2.xml or log4j2.yaml in your src/main/resources

0

To use the external configurations, you can simply place log4j.properties or logback-spring.xml or logback.xml and add the following property to your application.properties file

logging.config=classpath:logback-spring.xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.