Log File

//Add the jar file to project   log4j.jar
//Add the jar file to project   log4j-1.2.17.jar

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class log1File {

public static void main(String[] args) {

Logger logger = Logger.getLogger(“MyLog”);
FileHandler fh;

try {
// This block configure the logger with handler and formatter
fh = new FileHandler(“C:/Users/Youtility/Desktop/MyLogFile.log”);
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);

// the following statement is used to log any messages
logger.info(“My first log”);

} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
logger.info(“Hi How r u?”);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s