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.
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:
<?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:
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()
}
CodePudding user response:
add a properties tag in to your XML file and then define your path to save your log file:
<Properties>
...
<!--LINUX LOG DIRECTORY-->
<Property name="logDirLinux">/storage1/user/logs/folderName/</Property>
<!--WINDOWS LOG DIRECTORY-->
<Property name="logDirWindows">D:\\logs\\folderName\\</Property>
</Properties>
CodePudding user response:
The issue was that spring-boot couldn't recognize the log4j2.xml file because of the files structure.
The file has to be located directly in src/main/resources