Log4j Overview And Installation In Java Project

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

You can configure it by 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 and database etc.

1. log4j Components.

There are three components in Log4j.

  1. logger : In charge of catching logging data.
  2. layouts : In charge of format logging data in various styles.
  3. 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 library.
  4. Can assign multiple appender to one logger.
  5. Supports internationalization.
  6. Can use configuration file to change logging behavior at runtime.
  7. Can process Java Exceptions.
  8. There are totally 7 log levels, they are ALL, DEBUG, TRACE, INFO, WARN, FATAL and ERROR.
  9. The log data format can be adjusted by customize Layout class.
  10. You can easily change log output destination and log writing strategy by implement Appender interface.
3. log4j – Download.
  1. Go to log4j download page,  get the latest version.
    log4j download page
  2. Unzip the zip file to 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.
    1.1. Right click computer, click Properties in popup menu.
    right click computer properties popup menu item
    1.2. 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 should be %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\WorkSpace\dev2qa.com\Lib\apache-log4j-2.8.2-bin
    change windows PATH system environment variable
    1.3. Add log4j-api-2.8.2.jar to ClassPath system environment variable like above.
  2. For Linux.
    $ 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 ” Build Path —> Configure Build Path”.
    configure java build path in java project
  2. Click ” Add External JARs ” button to choose log4j-api-2.8.2.jar from your unzipped folder. Click OK to close the dialog.
    add log4j jar file in java project
0 0 votes
Article Rating
Notify of

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

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x