Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log separate log levels to separate files in log4j2 properties file

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.

like image 887
rishi Avatar asked Dec 06 '25 10:12

rishi


1 Answers

Try LevelRangeFilter

Try LevelRangeFilter: If you are using log4j2 for logging, include this line in your Appender configuration

<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>

the above line seperates the error logs from other logs

If the same is mentioned with minLevel and maxLevel as INFO it considers only INFO logs but not the above level logs.

<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>

The below log4j.xml file worked for me:

<?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">
            <LevelRangeFilter minLevel="TRACE" maxLevel="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>
            <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="info-log"
                     fileName="${log-path}/mycuteblog-info.log"
                     filePattern="${log-path}/mycuteblog-info-%d{yyyy-MM-dd}.log">
            <LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            <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">
            <LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <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>
        <Root level="info" additivity="false">
            <AppenderRef ref="console-log"/>
            <AppenderRef ref="trace-log"/>
            <AppenderRef ref="error-log"/>
            <AppenderRef ref="info-log"/>
        </Root>
    </Loggers>
</Configuration>

for more reference follow this link: https://howtodoinjava.com/log4j2/multiple-appenders/

like image 68
Maggi Maggi Avatar answered Dec 08 '25 06:12

Maggi Maggi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!