Logging

It is a process to tracking the different states of system while it is running. We record all the messages according to our observations in executions. It is a way to debugging your application in real time and provides required information to resolve problems. Because, you can set your logging statement at any executable place in you coding, so you can use this feature to test, debug, and profiling.

The entire messages generated by logging are recorded at specified destination of logger and can be retrieved later. Mostly, all messages are redirected to standard output of system and you can view all logging information as you application starts to executes.

Log4j is mostly used Logging tool in java that allows you to log 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.

One of the distinctive features of log4j is the notion of inheritance in loggers. Using a logger hierarchy it is possible to control which log statements are output at arbitrarily fine granularity but also great ease. This helps to reduce the volume of logged output and the cost of logging.

Here is an example of Log4j

import org.apache.log4j.*;
public class Log4JDemo1{
    public static void main(String ar[]){
  
        BasicConfigurator.configure();
        Logger  logger1 = Logger.getLogger("logger1");
        logger1.setLevel(Level.INFO);
        Logger logger2 = Logger.getLogger("logger1.logger2");
        logger1.warn("Missing user name");
        logger1.debug("getting user name from database");
        logger2.info("User name found in database");
        logger2.debug("validating user");

    }
}

No comments:

Popular Posts