Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use log4j2 with different log files? [duplicate]

Tags:

java

log4j2

I have a log4j2.xml file that I configured as following:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
    <Property name="log-path">${sys:catalina.base}/logs</Property>
</Properties>
<Appenders>

    <!-- console appender -->
    <Console name="console-log" target="SYSTEM_OUT">
        <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss} [%t] %c{1} - %msg%n"/>
    </Console>

    <!-- file appender -->
    <RollingFile name="commons-log" fileName="${log-path}/commons.log"
                 filePattern="${log-path}/commons-%d{yyyy-MM-dd}.log">
        <!-- log pattern -->
        <PatternLayout>
            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c{1} - %msg%n</pattern>
        </PatternLayout>

        <!-- set file size policy -->
        <Policies>
            <TimeBasedTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="5 MB" />
        </Policies>
    </RollingFile>
    <RollingFile name="analytics-log" fileName="${log-path}/analytics.log"
                 filePattern="${log-path}/analytics-%d{yyyy-MM-dd}.log">
        <PatternLayout>
            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c{1} - %msg%n</pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
        </Policies>
    </RollingFile>
</Appenders>
<Loggers>
    <Logger name="com.test.app" level="debug" additivity="false">
        <appender-ref ref="commons-log" level="debug"/>
        <appender-ref ref="analytics-log" level="warn"/>
        <appender-ref ref="console-log" level="debug"/>
    </Logger>
    <Root level="info" additivity="false">
        <AppenderRef ref="console-log"/>
    </Root>
</Loggers>

Now, I want to custom log data into commons.log and analytics.log files.

Example in java:

private static final Logger logCommon = LogManager.getLogger("commons-log");
private static final Logger logAnalytics = LogManager.getLogger("analytics-log");

And I want to custom write log to each file when I need, example:

logCommons.info ("Need it save into commons.log file");
logAnalytics.info ("Only save into analytics.log file");

Question: How can I do that? Please help me fix all my issues from log4j2.xml file and in Java code examples.

Thanks!

like image 997
Trong Tran Avatar asked Feb 23 '16 09:02

Trong Tran


2 Answers

Try it as below, You are writing debug logs to two files in the same logger

 <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
    <Property name="log-path">${sys:catalina.base}/logs</Property>
</Properties>
<Appenders>

    <!-- console appender -->
    <Console name="console-log" target="SYSTEM_OUT">
        <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss} [%t] %c{1} - %msg%n"/>
    </Console>

    <!-- file appender -->
    <RollingFile name="commons-log" fileName="${log-path}/commons.log"
                 filePattern="${log-path}/commons-%d{yyyy-MM-dd}.log">
        <!-- log pattern -->
        <PatternLayout>
            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c{1} - %msg%n</pattern>
        </PatternLayout>

        <!-- set file size policy -->
        <Policies>
            <TimeBasedTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="5 MB" />
        </Policies>
    </RollingFile>
    <RollingFile name="analytics-log" fileName="${log-path}/analytics.log"
                 filePattern="${log-path}/analytics-%d{yyyy-MM-dd}.log">
        <PatternLayout>
            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c{1} - %msg%n</pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
        </Policies>
    </RollingFile>
</Appenders>
<Loggers>
    <Logger name="com.test.app.commons" level="debug" additivity="false">
        <appender-ref ref="commons-log" level="debug"/>
        <appender-ref ref="analytics-log" level="warn"/>
    </Logger>
    <Logger name="com.test.app.console" level="debug" additivity="false">       
        <appender-ref ref="console-log" level="debug"/>
        <appender-ref ref="analytics-log" level="warn"/>
    </Logger>
    <Root level="info" additivity="false">
        <AppenderRef ref="console-log"/>
    </Root>
</Loggers>
like image 66
awsome Avatar answered Oct 13 '22 10:10

awsome


I think the problem comes from the fact that you have all your appenders ref in one logger.

Also, the logger name filters the log entries by the name you gave to the Logger in your java code.

You used in your example:

private static final Logger logCommon = LogManager.getLogger("commons-log");
private static final Logger logAnalytics = LogManager.getLogger("analytics-log");

So, your loggers will need to have the same names. What you can do is:

<Logger name="com.test.app" level="debug" additivity="false">
    <appender-ref ref="console-log" level="debug"/>
    <!-- you can put other appender references here if you want to log you 
         app logs in a specific file -->
</Logger>
<Logger name="commons-log" level="debug" additivity="false">
    <appender-ref ref="commons-log" level="debug"/>>
</Logger>
<Logger name="analytics-log" level="debug" additivity="false">
    <appender-ref ref="analytics-log" level="warn"/>
</Logger>

This way, it'll split your logs into different files based on the loggers names.

Hope it helps!

like image 28
Kevyn Meganck Avatar answered Oct 13 '22 11:10

Kevyn Meganck