Log separate log levels to separate files in log4j2 properties file
Asked Answered
S

3

3

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.

Skirting answered 31/5, 2017 at 14:7 Comment(0)
A
2

Try ThresholdFilter

The ThresholdFilter serves for filtering messages according to the log level. To get the different log files each appender should have the appropriate threshold filter. It should be something like this (with xml):

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Properties>
        <Property name="log-path">logs</Property>
        <Property name="log-pattern">[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1}- %msg%n"/</Property>
    </Properties>
    <Appenders>
        <Console name="console-log" target="SYSTEM_OUT">
            <PatternLayout>
                <pattern>${log-pattern}</pattern>
            </PatternLayout>
        </Console>

        <RollingFile name="trace-log"
                     fileName="${log-path}/mycuteblog-trace.log"
                     filePattern="${log-path}/mycuteblog-trace-%d{yyyy-MM-dd}.log">
            <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT"/>
            <PatternLayout>
                <pattern>${log-pattern}</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">
            <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout>
                <pattern>${log-pattern}</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>
    </Appenders>
...

Pay attention, that the log pattern is defined as a property, since the same pattern is used for all three appenders.

I cannot help with configuration as a properties file, I never used it.

You can find more about filters here and on the ThresholdFilter documentation

Angiology answered 1/6, 2017 at 6:35 Comment(0)
R
2

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/

Rossini answered 13/5, 2021 at 6:38 Comment(0)
T
0

Presuming the log42j config is the same as log4j w/apache log4j extras. Try this in your properties file:

log4j.appender.mycuteblog.filter.1=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.mycuteblog.filter.1.levelMin=INFO
log4j.appender.mycuteblog.filter.1.levelMax=INFO
Telegu answered 6/3, 2023 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.