Why Does the Log4J Environment Variable Need Both Core and Api Jar's?

When installing Log4J, we need to have two variables in the system classpath :

log4j-api
log4j-core

The log4j-api variable looks like this :

log4j-api : C:\apache_log4j_beta5\apache-log4j-2.0-beta5-bin\log4j-api-2.0-beta5.jar

And the log4j-core variable looks like this :

log4j-core: C:\apache_log4j_beta5\apache-log4j-2.0-beta5-bin\log4j-core-2.0-beta5.jar

Both of these env. variables go into the overlapping LOG4J_HOME variable :

LOG4J_HOME - %log4j-api%;%log4j-core%

Why does it need both the api and core libraries(JAR files) though? When I import a class, I say :

import org.apache.logging.log4j.Logger;

So which of the above JAR's get searched for here? Is it core or api ? any tips appreciated, thanks

1 Answer

log4j-api, as it's name says is an API. So basically it just contains interfaces that you can use to interface your code with the log4j framework. log4j-core is the implementation of this interface, so it contains actual code. It implements every interface in the API.

So basically when you import the Logger class, you import it from the API library. And when you call LogManager.getLogger() it first initializes itself in a static block, and it will try to search for valid implementations. And that's why you need the core library too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest