rekowski.info David Rekowski's random stuff

Log4j2 with Maven and Eclipse

2012-08-10

Notes on setting up Log4j2 with Maven in an eclipse project

I had some troubles setting up a maven project with log4j version 2 and am jotting down what turned out to work here for further reference.

maven dependencies setup

Also see logging.apache.org/log4j/2.x/manual/configuration.html for further information on how log4j2 looks for its configuration.

Note, that version 2 of log4j uses org.apache.logging.log4j as the repository group ID, instead of log4j for 1.x.

# section from /pom.xml ... <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.0-alpha1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.0-alpha1</version> </dependency> ...

log4j configuration

# /src/main/java/log4j2.xml <?xml version="1.0" encoding="UTF-8"?> <configuration status="ON"> <appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{ISO8601} [%t] %-5level %logger{36} - %msg%n"/> </Console> </appenders> <loggers> <logger name="com.example.app" level="info"> </logger> <root level="error"> <appender-ref ref="Console"/> </root> </loggers> </configuration>

App.java

# /src/main/java/com/example/app/App.java package com.example.app; import org.apache.logging.log4j.*; /** * Hello world! * */ public class App { private static Logger logger = LogManager.getLogger("com.example.app"); public static void main( String[] args ) { logger.info("Entering application."); logger.warn("Entering application."); logger.debug("Entering application."); logger.error("Entering application."); System.out.println( "Hello World!" ); } }

Output running the app

2012-08-10 19:34:48,733 [main] INFO com.example.app - Entering application. 2012-08-10 19:34:48,734 [main] WARN com.example.app - Entering application. 2012-08-10 19:34:48,734 [main] ERROR com.example.app - Entering application. Hello World!