Thursday 11 July 2013

What is Log4j and what is the use of log4j in java

With log4j it is possible to enable logging at runtime without modifying the application binary. The log4j package is designed so that these statements can remain in shipped code without incurring a heavy performance cost. Logging behavior can be controlled by editing a configuration file, without touching the application binary.

Logging equips the developer with detailed context for application failures. On the other hand, testing provides quality assurance and confidence in the application. Logging and testing should not be confused. They are complementary. When logging is wisely used, it can prove to be an essential tool.

To add to this, with Log4J you can dynamically switch logging on/off. You can change the format dynamically (do you want timestamps ? datestamps ?) and you can change where the logging goes (to the console ? to a file ? to a database ?), all without changing your code.

You can also log to multiple sources at once. In an application I'm working on now, when I'm testing the application, I log to both the console (so I can see debug information as well as any errors that pop up) along with the standard log file that's always logged to. Quite handy

The beauty of log4j is in its architecture of appenders and layouts. As mentioned by previous poster, you change the aspect of logging of your application without much hassle, most of the time it's just a matter of simple configuration. One of the usages I would add on my part is centralized logging which can be added to your application without touching its code base

1 comment:

  1. http://www.tutorialspoint.com/log4j/log4j_sample_program.htm

    ReplyDelete