rupweb
rupweb

Reputation: 3328

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...

Upvotes: 0

Views: 262

Answers (2)

rupweb
rupweb

Reputation: 3328

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!

Upvotes: 0

Remko Popma
Remko Popma

Reputation: 36754

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".

Upvotes: 1

Related Questions