简体   繁体   中英

log4j2 xml configuration log to 2 files

can anyone see what's is wrong with this xml configuration? It's to setup 2 different log files then log trace to 1 file and info to another:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
    <Appenders>
        <RollingRandomAccessFile name="LogTrace" fileName="../logs/Trace.log" filePattern="../logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="25 MB"/>
            </Policies>
        </RollingRandomAccessFile>
        <RollingRandomAccessFile name="LogInfo" fileName="../logs/Info.log" filePattern="../logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="25 MB"/>
            </Policies>
        </RollingRandomAccessFile>
    </Appenders>
    <Loggers>
        <Logger name="monitor" level="info">
            <AppenderRef ref="LogInfo" level="info"/>   
        </Logger>
        <Root level="trace">
            <AppenderRef ref="LogTrace" level="trace"/>
        </Root>
    </Loggers>
</Configuration>

The LogTrace is working but not the LogInfo...

In your code, do you use the monitor logger like this:

Logger logger = LogManager.getLogger("monitor");
logger.info("test info message");

The above should work since your config declares the info level logger with name "monitor".

Using the configuration below along with the code supplied by Remko in his answer did exactly what I am looking for:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
    <Appenders>
        <RollingRandomAccessFile name="LogTrace" fileName="../logs/Trace.log" filePattern="../logs/$${date:yyyy-MM}/tracelog-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="25 MB"/>
            </Policies>
        </RollingRandomAccessFile>
        <RollingRandomAccessFile name="LogInfo" fileName="../logs/Info.log" filePattern="../logs/$${date:yyyy-MM}/infolog-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d %m %ex%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="25 MB"/>
            </Policies>
        </RollingRandomAccessFile>
    </Appenders>
    <Loggers>
        <Logger name="monitor" level="all">
            <AppenderRef ref="LogInfo" level="info"/>
            <AppenderRef ref="LogTrace" level="trace"/>
        </Logger>
    </Loggers>
</Configuration>

It's about as simple as it can be... once you have spent a few DAYS mulling over the various configuration options!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM