Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Log4j2 package specific logging using log4j2.xml

Tags:

java

log4j2

I am using log4j2. But the problem that I am facing is that it logs all logs. I want to ... log from specific package to a specific file & other package to another file. I am using log4j2.xml for configuration. Please can someone help?

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>

<Loggers>
    <Root level="DEBUG" additivity="false">
        <AppenderRef level="DEBUG" ref="CONSOLE" />
        <AppenderRef level="DEBUG" ref="fileAppender" />
    </Root>
    <Logger name="com.pkg.test.logging.method" level="DEBUG"
        additivity="false">
        <Appender-ref ref="fileAppender" level="DEBUG" />
    </Logger>

</Loggers>

<Appenders>

    <Console name="CONSOLE" target="SYSTEM_OUT">
        <PatternLayout
            pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n%throwable}" />
    </Console>

    <RollingFile name="fileAppender" fileName="./log.log"
        filePattern="./log-%d{yyyy-MM-dd}.log">
        <PatternLayout
            pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n}" />
        <Policies>
            <TimeBasedTriggeringPolicy interval="1"
                modulate="true" />
        </Policies>
    </RollingFile>
</Appenders>

like image 423
Rajas Avatar asked Aug 02 '16 06:08

Rajas


People also ask

How does log4j2 xml work?

In the log4j2 architecture, an appender is basically responsible for sending log messages to a certain output destination. Here are some of the most useful types of appenders that the library provides: ConsoleAppender – logs messages to the System console. FileAppender – writes log messages to a file.

Where do I put log4j2 xml?

We should put log4j2. xml anywhere in the application's classpath. Log4j will scan all classpath locations to find out this file and then load it. We can find this file mostly placed in the 'src/main/resources' folder.


3 Answers

Just answered the question.

log4j2.xml

    <?xml version="1.0" encoding="UTF-8"?>
<Loggers>
    <Root level="DEBUG" additivity="false">
        <AppenderRef level="DEBUG" ref="CONSOLE" />
     </Root>
    <Logger name="com.pkg.test.logging.method" level="DEBUG"
        additivity="false">
        <Appender-ref ref="fileAppender" level="DEBUG" />
    </Logger>

</Loggers>

<Appenders>

    <Console name="CONSOLE" target="SYSTEM_OUT">
        <PatternLayout
            pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n%throwable}" />
    </Console>

    <RollingFile name="fileAppender" fileName="./log.log"
        filePattern="./log-%d{yyyy-MM-dd}.log">
        <PatternLayout
            pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n}" />
        <Policies>
            <TimeBasedTriggeringPolicy interval="1"
                modulate="true" />
        </Policies>
    </RollingFile>
</Appenders>

Removed the <AppenderRef level="DEBUG" ref="fileAppender" /> from root logger. Thus it started logging logs based on packages.

like image 51
Rajas Avatar answered Oct 19 '22 18:10

Rajas


From Log4J Manual:

Adding a specific logger for a class: (you can refer to packages here too)

<Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
  <AppenderRef ref="File"/>
</Logger>

Adding a specific appender:

<Appender type="File" name="File" fileName="${filename}">
  <Layout type="PatternLayout">
    <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
  </Layout>
</Appender>
like image 28
bilgec Avatar answered Oct 19 '22 19:10

bilgec


The <Loggers> section of the config looks correct. But, if all other logs not belonging to the expected package is also getting logged, it is the Root logger which is responsible for these logs, and not the package level logger. It may not be active because of the way you are creating the logger instance in your code. E.g: If one is using slf4j to create the logger instance, then this is how we would need to create the logger instance:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Logger logger = LoggerFactory.getLogger(Test.class);

If one gets the logger instance using - Logger logger = LoggerFactory.getLogger("Test") then, the config will not work as expected.

Also, another answer mentioned that the solution was to remove the fileAppender that the package logger was using from the root logger. This, in fact, is not a problem. You are free to use the same appenders in root and package level logger. My answer in context of log4j-api 2.14.1

like image 1
Binita Bharati Avatar answered Oct 19 '22 19:10

Binita Bharati