JavaLite switched to Log4j2 since v 2.4 and 3.0 respectively (as well as preceding snapshots).

Generally, developers want to see the results of logging in the console during development, and in files on test, staging and production environments. The configuration below is a suggestion on how to achieve that and also integrate Log4j2 into your apps.

Maven profiles

Here is an example of a development profile that configures an appender-name property to the value CONSOLE. Since this profile is active by default, this will be the value of the appender-name property when standard Maven commands issued: mvn clean install, mvn test, etc.

The second profile file_log sets the appender-name to a value FILE, and is designed to indicate that the log needs to go to a file.

Configuring Log4j2

This is done by adding a file log4j2.xml to directory src/main/resources.

Log4j2 supports other formats too, please refer to its configuration documentation.

Here is an example:

Let's decompose this configuration:

  • Line 2 contains this attribute: packages="org.javalite.logging", which is necessary to use if you want the output in JSON format, which is achieved by JsonLog4j2Layout configured on line 20th
  • Line 5 contains a valiable appender-name which is replaced with either FILE or CONSOLE by Maven while filtering the resources. See Filtering resources below.

As a result, your log4j2.xml file will have either CONSOLE or FILE on line 5, depending what profile you used in your Maven command.

As you can see, the configuration above contains two appenders, CONSOLE and FILE, and just the FILE appender has a layout configured. This means that the output in a file will be in a JSON format, while the CONSOLE format will be in plain text.

Having logs on the console in development and in a log file in production achieves goals of fast development and automated log processing.

Filtering resources

In order to tell Maven to replace the appender-name, you need to filter resources, and this is how you do this in Maven, considering that the file log4j2.xml is located in src/main/resources, as it should.

How to comment

The comment section below is to discuss documentation on this page.

If you have an issue, or discover bug, please follow instructions on the Support page