Friday, January 22, 2016

Adding log4j to server side in GWT project

Logging is the important part of any application. It is mandatory to log the event that occured in the applications.

In GWT we had two part

Client and Server.

Below code can be added at the server side to do logging

Here we are using here log4j-1.2.17.jar. To do Server side logging follwing important step need to be taken.

Step 1:- Creating log4j properties or log4j xml file to read the configuration as the feed to Log4j

below is the code you can use for logging the log in file and console.

This is the propeties files

# Root logger option
log4j.rootLogger=INFO, file,stdout

# Direct log messages to a log file

log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Step 2:- Do following changes at the server side.

This is the code which use to write on the Server side

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

PropertyConfigurator.configure("WEB-INF/");"info Hello this is an debug message");
logger.warn("warn Hello this is an debug message");
logger.error(" Error Hello this is an debug message");

Note: Make sure to keep your properties files in classpath i.e. WEB-INF folder and also log4j-1.2.17.jar

No comments: