David Rekowski's random stuff

Log4j2 with Maven and Eclipse


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 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="" level="info"> </logger> <root level="error"> <appender-ref ref="Console"/> </root> </loggers> </configuration>

# /src/main/java/com/example/app/ package; import org.apache.logging.log4j.*; /** * Hello world! * */ public class App { private static Logger logger = LogManager.getLogger(""); public static void main( String[] args ) {"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 - Entering application. 2012-08-10 19:34:48,734 [main] WARN - Entering application. 2012-08-10 19:34:48,734 [main] ERROR - Entering application. Hello World!