Wednesday, February 29, 2012

Simple Log4j Configuration


Log4j is a simple logging framework. you can download the latest version of log4j ( Download ). After download finished add jar in class path log4j-x.x.x.jar.
Create a new HelloLogger.java file.
package com.abc.helloworld;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class HelloLogger {

 static final Logger logger = Logger.getLogger(HelloLogger.class);
 
 public static void main(String[] args) {
  BasicConfigurator.configure();
  logger.debug("Hello World!");
 }
}


Log4j available methods are info(), warn(), error() and fatal() , logger levels are
DEBUG, INFO, WARN, ERROR, FATAL .

Following example shows usage of method.

package com.abc.helloworld;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class HelloLogger {

 static final Logger logger = Logger.getLogger(HelloLogger.class);
 
 public static void main(String[] args) {
  BasicConfigurator.configure();
  logger.debug("debug message");
  logger.info("info message");
  logger.warn("warn message");
  logger.error("error message");
  logger.fatal("fatal message");
 }
}

Output of the above code.

0 [main] DEBUG com.abc.helloworld.HelloLogger  - debug message
0 [main] INFO com.abc.helloworld.HelloLogger  - info message
0 [main] WARN com.abc.helloworld.HelloLogger  - warn message
0 [main] ERROR com.abc.helloworld.HelloLogger  - error message
0 [main] FATAL com.abc.helloworld.HelloLogger  - fatal message
Enjoy log4j in you web application.

AWS EC2 - SSH locked with UFW

Need to update the instance's user data: 1. Stop the instance 2. Right click (windows) or ctrl + click (Mac) on the instance to open a c...