问题
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.
回答1:
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.
回答2:
Using Spring Boot you can specify log4j2.properties
inside the application.properties
logging.config=src/main/resources/log4j2.properties
回答3:
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
回答4:
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.
回答5:
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
来源:https://stackoverflow.com/questions/51649986/spring-boot-log4j2-properties-creating-log-files-but-not-writing-the-logs-in-f