Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j 2 - configuration issue

Tags:

java

log4j2

I am trying to configure log4j 2.0 to report logs.

My config is saved as log4j2.xml and this is its content:

  <?xml version="1.0" encoding="UTF-8"?>
  <configuration name="PRODUCTION" status="OFF">

    <appenders>
        <RollingFile name="MyFileAppender" 
            fileName="../Logs/app.log" 
            filePattern="../Logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>%d %p %C{1.} [%t] %m%n</pattern>
            </PatternLayout>
            <Policies>
                <OnStartupTriggeringPolicy />
                <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
                <SizeBasedTriggeringPolicy size="250 MB"/>
            </Policies>
        </RollingFile>
    </appenders>

    <loggers>
        <root level="trace">
            <appender-ref ref="MyFileAppender"/>
        </root>
    </loggers>

 </configuration>

It exists in the classpath of the project and I tried putting it in many other directories..

I created a logger in the code like so:

    Logger          logger = LogManager.getLogger(MyClass.class.getName());
    logger.info("test");

And nothing is written and no file is created. When I debug the code I see that the logger is the default logger(console).

like image 811
Lee Avatar asked Feb 06 '13 16:02

Lee


People also ask

What is configuration status in LOG4J2?

Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.

What is the difference between log4j and LOG4J2?

Community support: Log4j 1. x is not actively maintained, whereas Log4j 2 has an active community where questions are answered, features are added and bugs are fixed. Automatically reload its configuration upon modification without losing log events while reconfiguring.

Where can I find the log4j configuration file?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.


5 Answers

place log4j2.xml file under src/main/resources. It works

like image 79
Ramachandraiah Nikadi Avatar answered Sep 19 '22 16:09

Ramachandraiah Nikadi


Actually This is a straight forward process. Two main classes of Log4j 2 are as follows that you need to import like this:

    import org.apache.logging.log4j.LogManager
    import org.apache.logging.log4j.Logger

Now get a Logger instance by using this code.

    private static final Logger logger = LogManager.getLogger();

Note I didn't specified the class name to getLogger() method as a parameter. Log4j 2 automatically figures it out.

Now you can use any of the info(), trace(), debug(), warn(), error(), fatal() method from the Logger class. But to get the output from all of these methods you'll need a XML configuration file. By default Log4j 2 only produces output from the error() and fatal() methods.

Configuration file is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="WARN">
        <Appenders>
            <File name="MyCustomLogFile" fileName="/home/me/mylogfile.log">
                <PatternLayout>
                    <Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
                </PatternLayout>
            </File>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            </Console>
        </Appenders>
        <Loggers>
            <Root level="all">
                <AppenderRef ref="MyCustomLogFile"/>
                <!--<AppenderRef ref="Console"/>-->
            </Root>
        </Loggers>
    </Configuration>

Save this file with any name any where. I use Log4j2.xml as name. Now you'll need this file to be put in classpath, which you can do by passing a system property when running the program as follows:

    java -Dlog4j.configurationFile=/path/to/xml/configuration/file/Log4j2.xml MyMainClass

And you've done it. Logging will be right away on your console.

Special Notes:

  • In XML file I've provided 2 appenders: A file and a console. You can see that you just need to uncomment the commented AppenderRef tag to get output in a file instead of console.

  • You can also provide an environment variable as a system property. Log4j 2 will read the configuration file from the environment variable first and then in -D argument if not found an environment variable.

Have fun with logging. :-)

like image 24
Manvendra SK Avatar answered Sep 22 '22 16:09

Manvendra SK


you should put your log4j2.xml into the classpath.

or set "log4j.configurationFile" system property to force to use your log4j2.xml

Please refer to: http://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguration

like image 29
ldelucio Avatar answered Sep 18 '22 16:09

ldelucio


It exists in the classpath of the project and I tried putting it in many other directories.

Where exactly? There is often confusion about where "in the classpath" means. It can't just be anywhere. It has to be at the 'top' or the 'default package'.

src
├── main
│   └── java
│       ├── com
│       │   └── example
│       └── log4j2.xml
like image 30
wolfcastle Avatar answered Sep 19 '22 16:09

wolfcastle


A tip for eclipse users:

Right click on the project and click "refresh". Make sure you could see the log4j2.xml file in eclipse. (This solved my problem.)

To be verbose:

  • You shouldn't add the file to build path.(If you do, eclipse will warn you about this)

  • The name of this file doesn't appear in '.classpath' file.

  • I put my log4j2.xml under src/ directory. It works.
like image 32
fstang Avatar answered Sep 18 '22 16:09

fstang