[英]Log4j2 log file not created
For a spring-boot application I'm trying to set up a log4j2 logger that will write the logs into a few sperate files, but the logs are only written to the console, I've checked my user_dir to make sure I'm not looking in the wrong location as well.对于 spring-boot 应用程序,我正在尝试设置一个 log4j2 记录器,它将日志写入几个单独的文件,但日志只写入控制台,我检查了我的 user_dir 以确保我没有查看错误的位置也是如此。
This is the code that should write the test logs:这是应该编写测试日志的代码:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class UsersController {
@Autowired
UsersService userService;
private static final Logger logger = LogManager.getLogger(UsersController.class);
@PostMapping("CreateUserWithEmailAndPassword")
public void createUserWithEmailAndPassword(@RequestParam String userName, @RequestParam String password, @RequestParam String email, HttpServletResponse response) throws ExecutionException, InterruptedException {
logger.info("creating a new user");
if(userService.createUserWithEmailAndPassword(userName, password, email))
response.setStatus(HttpServletResponse.SC_ACCEPTED);
else{
logger.error("failed on creating a new user");
response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
}
}
}
this is my log4j2.xml file:这是我的 log4j2.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="FileAppender" ref="infoAppender" fileName="info-${date:yyyyMMdd}.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="FileAppender" ref="errorAppender" fileName="error_warn-${date:yyyyMMdd}.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="warn">
<AppenderRef ref="errorAppender"/>
</Root>
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="infoAppender" level="info"/>
</Loggers>
<Properties>
<!--LINUX LOG DIRECTORY-->
<Property name="logDirLinux">/storage1/user/logs/folderName/</Property>
<!--WINDOWS LOG DIRECTORY-->
<Property name="logDirWindows">C:\\logs\\</Property>
</Properties>
</Configuration>
this is my build.gradle file:这是我的 build.gradle 文件:
plugins {
id 'org.springframework.boot' version '2.7.2'
id 'io.spring.dependency-management' version '1.0.12.RELEASE'
id 'java'
}
group = 'com.Itamarled'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation ('org.springframework.boot:spring-boot-starter-web'){
exclude group : 'spring-boot-starter-logging'
}
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation ('org.springframework.boot:spring-boot-starter-test'){
exclude group : 'spring-boot-starter-logging'
}
implementation 'com.google.firebase:firebase-admin:9.0.0'
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
}
tasks.named('test') {
useJUnitPlatform()
}
add a properties tag in to your XML file and then define your path to save your log file:在 XML 文件中添加属性标记,然后定义保存日志文件的路径:
<Properties>
...
<!--LINUX LOG DIRECTORY-->
<Property name="logDirLinux">/storage1/user/logs/folderName/</Property>
<!--WINDOWS LOG DIRECTORY-->
<Property name="logDirWindows">D:\\logs\\folderName\\</Property>
</Properties>
The issue was that spring-boot couldn't recognize the log4j2.xml file because of the files structure.问题是由于文件结构,spring-boot 无法识别 log4j2.xml 文件。
The file has to be located directly in src/main/resources该文件必须直接位于 src/main/resources
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.