Installation
Visit the following link https://logging.apache.org. You will find a download page link in the left hand side, visiting the page you will be given option to view the source to log4j jar file. Download the suitable package on to your system.
Unzip the package, you will find the list of jar files.
Importing
In order for our projects to utilize the Log4j, we need import them, open the IDE and open Project structure or Ctrl+Alt+Shit+S (In Intellij IDE).
In module tab click add and import the below 2 jar file and apply.
log4j-api-2.13.0.jar
log4j-core-2.13.0.jar
Sample Code and Execution
Code
import org.apache.logging.log4j.*;
public class Demo {
private static Logger log = LogManager.getLogger(Demo.class.getName());
public static void main(String[] args) {
int i = 10;
log.debug("Debugging");
if(i==10){
log.info("Info_log");
log.error("Error_log");
log.fatal("Fatal_log");
}
}
}
Output
Explanation
Depending upon the configuration in the Log4j.xml file the out put in printed in the console.
We will see more about the Log4j.xml on our next section.