Configuring Log4j 2 and Maven

The details of how to configure the Log4j 2 functionality is covered in some detail on the primary Log4j 2 homepage.

Integrating it within a maven project can be a useful thing to do. There are a couple of ways to do it, and one stackoverflow recommendation is as follows:

Put the file log4j2.xml into the src/main/resources directory.

This means that this file will end up being used when you run the program.

It makes is specific to your application – which is quite beneficial.

For test, you could use the appropriately named:

log4j2-test.xml

and place it into the src/test/resources directory.

What is in the file.

<configuration status="OFF">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</appenders>
<loggers>
<logger name="com.foo.Bar" level="trace">
<appender-ref ref="Console"/>
</logger>
<root level="error"><appender-ref ref="Console"/>
</root></loggers>
</configuration>

This is where it should be.

There are two logger types – the specified class, and the root.
For the root class – choose an appropriate level – such as “warn” so that you can see all of the warnings from the different classes.
For the particular example above – the is what is used.

We have the package that we are using specified as “com.foo.Bar” above – so typically we are setting the pattern at that level.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Configuring Log4j 2 and Maven

  1. Tax says:

    Erh sorry but where is the Maven? 😉

    • admin says:

      I was using the post to point out that you can place different log4j2.xml files in the src/main/resources or indeed the src/test/resources folder if you want to have different logging behaviours. Let me know if I can clarify this in the post and I will do so!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s