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.
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.
<profile> <id>development</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <appender-name>CONSOLE</appender-name> </properties> </profile>
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.
<profile> <id>file_log</id> <properties> <appender-name>FILE</appender-name> </properties> </profile>
This is done by adding a file
log4j2.xml to directory
Log4j2 supports other formats too, please refer to its configuration documentation.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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
JsonLog4j2Layoutconfigured on line 20th
- Line 5 contains a valiable
appender-namewhich is replaced with either
CONSOLEby Maven while filtering the resources. See Filtering resources below.
As a result, your
log4j2.xml file will have either
FILE on line 5, depending what profile you used in your Maven command.
As you can see, the configuration above contains two appenders,
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.
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.
<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources>
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