6

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!

Trong Tran
  • 676
  • 1
  • 8
  • 22

3 Answers3

13

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>
awsome
  • 2,143
  • 2
  • 23
  • 41
8

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!

Kevyn Meganck
  • 609
  • 6
  • 15
1

You can configure a logger like this:

    String log4jfile = topDir + "log4j2.xml";
    PropertyConfigurator.configure(log4jfile);        

where log4jfile is the path to the configuration file you want to use. Without doing this, it will just have the default behaviour.

randombee
  • 699
  • 1
  • 5
  • 26
  • No, I mean, I just have one log4j2.xml file. And I want to configure all things in that file. For now, it received configure from log4j2.xml file. – Trong Tran Feb 23 '16 at 10:25
  • But if you don't run the PropertyConfigurator it won't be getting the configuration from there...(?) Sorry maybe I'm misunderstanding this – randombee Feb 23 '16 at 10:27
  • It ran, but log data was saved to both files. – Trong Tran Feb 23 '16 at 10:31