Is there any way we can create separate log files for different log levels?
All I want is to log error
logs to one file and info
logs to another file.
I did not find any solution to do this in log4j2.properties
. Here is the log4j2.xml
which I got and it works fine. Can anyone help me writing the same in properties file?
This XML file uses the method from the Log4j2 FAQ and sets level
on the AppenderRef(s):
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<RollingFile name="trace-log"
fileName="${log-path}/mycuteblog-trace.log"
filePattern="${log-path}/mycuteblog-trace-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<RollingFile name="error-log"
fileName="${log-path}/mycuteblog-error.log"
filePattern="${log-path}/mycuteblog-error-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.mycuteblog.log4j2" level="debug" additivity="false">
<appender-ref ref="trace-log" level="debug"/>
<appender-ref ref="error-log" level="error"/>
<appender-ref ref="console-log" level="debug"/>
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="console-log"/>
</Root>
</Loggers>
</Configuration>
P.S. - I do not want to make any code change for this. I am looking for specifically log4j2.properties
.
Thanks in advance.