Why Log4j is better than System.Out.Println? (What is difference between log4j and common loggings?)

   Log4j is a logging framework, it is used as standard logger nowadays compared to System.out.println.
       Log4j has certainly various advantages over System.out.println,  firstly being checking the output the system.out.println outputs to standard output which is mostly a console window whereas the output from Log4j can be to the console or email server, database table or a log file.
Additionally, in Log4j different levels of logging can be set such as TRACE, DEBUG, INFO, WARN, ERROR and FATAL. If a log level is set messages will be logged for that level and level below it.
   Log4j allows logging on class-by-class basis i.e., each class can be covered whereas System.out.println can be controlled at application level. Through Log4j we can turn on or off the logging at runtime by changing the configuration file.
The Main Reasons why Log4j is better than system.out.println() –
•  There are various levels of logging i.e all, trace, debug, info, warn, error and fatal. You can log at whatever you want in production as compared to println that can only print logs on the console. In log4j you can change the granularity of logging based on your requirement.
•  Logging can be controlled at runtime using the configuration file as compared to println that requires a compilation and code deployment.
•  You can target logs as per your convenience, you can keep them in log files. Later you can archive these log files where as println prints out on console and you may get “Out of memory” on buffers.
•  Loggers are usually optimized for speed.
•  Logging support internationalization
•  Log4j allows you to print the logs at multiple locations. Not only that it allows you to print out the logs asynchronously. log4j allows you to define your own writing strategy.
•  Using log4j you can specify the output format of logs. You can define your own conversion pattern. It allows you to include all the required information you want in your logs in the required pattern.

Hope this helps —

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories