Log4j Overview And Installation In Java Project

Log4j is a java logging framework that is fast, reliable, and flexible. It is open-source and free. You can click here to go to it’s official home page.

You can configure it with a configuration file. In that file, you can configure logging levels, log information layout. You can also configure where the log data saved to, such as console, file, database, etc.

1. log4j Components.

  1. There are three components in Log4j.
  2. logger: In charge of catching logging data.
  3. layouts: In charge of format logging data in various styles.
  4. appenders: In charge of writing logging data to numerous chosen locations.

2. log4j Features.

  1. Use named logger hierarchy.
  2. Thread-safe.
  3. Faster than other logger libraries.
  4. Can assign multiple appenders to one logger.
  5. Supports internationalization.
  6. Can use a configuration file to change logging behavior at runtime.
  7. Can process Java Exceptions.
  8. There is a total of 7 log levels, they are ALL, DEBUG, TRACE, INFO, WARN, FATAL and ERROR.
  9. The log data format can be adjusted by customizing the Layout class.
  10. You can easily change the log output destination and log writing strategy by implement the Appender interface.

3. log4j – Download.

  1. Go to the log4j download page,  get the latest version.
  2. Unzip the zip file to a local directory such as C:\WorkSpace\dev2qa.com\Lib\apache-log4j-2.8.2-bin.

4. Add log4j To System Environment Variable Path and ClassPath.

  1. For Windows, you can follow the below steps.
  2. Right-click your PC icon in windows explorer, click Properties in the popup menu.
  3. In the popup window, click “Advanced system settings —> Environment Variables —> Path“, add your log4j directory path to the value. The Path value after changed for my environment is %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\WorkSpace\dev2qa.com\Lib\apache-log4j-2.8.2-bin
  4. Add log4j-api-2.8.2.jar to the ClassPath system environment variable like above.
  5. For Linux, you can run the below command in a terminal.
    $ export CLASSPATH=$CLASSPATH:/usr/local/apache-log4j-2.8.2-bin/log4j-api-2.8.2.jar
    $ export PATH=$PATH:/usr/local/apache-log4j-2.8.2-bin/

5. Add log4j Jar In Java Project.

  1. Right-click your java project name, click the Properties menu item to open the java project Properties dialog window.
  2. Click the ” Java Build Path ” on the left and then click the Libraries tab on the right side.
  3. Click the ” Add External JARs ” button to choose log4j-api-2.8.2.jar from your unzipped folder. Click OK to close the dialog.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x