Log4J2 – How to prevent it from appending?
Here’s my log4j2 configuration:
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="INFO"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> <File name="MyFile" fileName="logs\application.log" append="false"> <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </File> </Appenders> <Loggers> <Root level="debug"> <AppenderRef ref="Console" level="error" /> <AppenderRef ref="MyFile" level="info"/> </Root> </Loggers> </Configuration>
and here’s how Log4J2 outputs to my file:
2020-07-15 12:09:40.449 [main] WARN [MYAPP] - Values don't match: First Map Key: [MYKEY] - First Map Value: [MYVALUE] Second Map Key: [MYKEY] - Second Map Value: [MYVALUE] 2020-07-15 12:09:40.449 [main] WARN [MYAPP] - Values don't match: First Map Key: [MYKEY] - First Map Value: [MYVALUE] Second Map Key: [MYKEY] - Second Map Value: [MYVALUE] Values don't match: First Map Key: [MYKEY] - First Map Value: [MYVALUE] Second Map Key: [MYKEY] - Second Map Value: [MYVALUE] ...
In my code; I’m informing Logger and Formatter as such:
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; private static final Logger logger = LogManager.getLogger([CURRENT_JAVA_CLASS].class); private static final Formatter formatter = new Formatter(); ... formatter.format("Values don't match: \n First Map Value: %s \n Second Map Value: %s \n", firstMapEntry.getValue(),secondMapEntry.getValue()); logger.warn(formatter);
I don’t want it to do this, I just want it to output the latest error, not concatenate it with the previous lines. Can anyone tell me how to do this?