log4j is a logging library for Java applications and is developed by Apache. Logging is a vital part of any application from debugging standpoint. When the application is in development phase, developers can use several debugging tools and even the IDE tools provides options to run the code in debug mode but those tools and options are not be available in actual production environments.
Logging is process of adding useful statements in the code which gets printed in files and can be used to debug the application in case error is caught. Also the complete error stack trace can be logged which can give the complete detail of the error, root cause, method details in which error is caught and even on which line error is occurred can be logged.
- Many developers think that we can include “ System.Out.println()” statements to print the statements then why we need to include log4j libraries. Answer is ‘Yes’ we can do that, but with log4j, we get a high level of configurability and various logging levels which is not possible with System logs.
- We can control the behavior of log4j with the help of configuration files.
- Log4j supports various logging levels. With log4j, we can add the logging statement with various logging levels like we can write any informative statements in info level and all the errors with error logging level. We can configure the level of logging we want to log using simple configuration and need not to touch the binaries even though debug statements are there in code. Log4j allows us to simply specify the level of which logs we want to capture and any statement below that severity will not be logged.
- log4j comes with several appenders which can be used to write the log statements to files, console, database, email etc.
- log4j is thread safe, supports internationalization.
- log4j supports layouts which can be used to change the layout of logs.