Skip to content

Instantly share code, notes, and snippets.

@yeikel
Last active April 26, 2022 19:27
Show Gist options
  • Save yeikel/a4da9f116eaeb09b1d7a4fb6c4019b31 to your computer and use it in GitHub Desktop.
Save yeikel/a4da9f116eaeb09b1d7a4fb6c4019b31 to your computer and use it in GitHub Desktop.
Add Log4j2 to Maven Project

Latest version : https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api

  1. Add dependencies
    <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.14.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.14.0</version>
        </dependency>
  1. Create log4j2.xml file in the resources folder
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="debug" additivity="false">
            <AppenderRef ref="console" />
        </Root>
    </Loggers>
</Configuration>
  1. Create logger instances :
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

private static final Logger LOGGER = LogManager.getLogger(ClasName.class.getName());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment