Monday, January 25, 2016

Logging GWT Client Side log to server log files

Lets try to log our client side log on server side using *.log files. There are many ways to do it i.e. using gwt-log jar etc but we will use simple java.util.logging.Logger at the client side and org.apache.log4j.Logger on the server side.

Following are the important point hat need to be taken in to consideration


Step 1:- Download log4j.jar and keep the same in your /lib folder and add the same in your prject classpath.
Step 2:- Create log4j.properties or log4j.xml. We are using log4j.xml







Step 3:- Create a servlet as shown below on server side

package com.test.server;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import org.apache.log4j.Logger;

import com.google.gwt.logging.shared.RemoteLoggingService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;




public class MyRemoteLoggingServlet extends RemoteServiceServlet implements RemoteLoggingService {

/**

*/
private static final long serialVersionUID = 1L;
private static Logger rootLogger = Logger.getLogger("remote_logging");

    @Override
    public String logOnServer(LogRecord record) {

        Level level = record.getLevel();
        String message = record.getMessage();

        if (Level.INFO.equals(level)) {
        rootLogger.info(message);
        } else if (Level.SEVERE.equals(level)) {
        rootLogger.error(message);
        } else if (Level.WARNING.equals(level)) {
        rootLogger.warn(message);
        } else if (Level.FINE.equals(level)) {
        rootLogger.debug(message);
        }

        return null;
    }


}

Step 4:- Modify web.xml as given below






Step 5:-  Modify following line in project.gwt.xml






Step 6:- On client side add following line for logging

import java.util.logging.Level;
import java.util.logging.Logger;
 
private static Logger rootLogger = Logger.getLogger(TestSidGWTLog.class.toString());
rootLogger.info("some msg");


Note:- if you are gettin warning as below

log4j:WARN No appenders could be found for logger
- Make sure to add your log4j.xml in src folder



You can also refer to the same on

https://shdhumale.wordpress.com/2016/01/25/logging-gwt-client-side-log-to-server-log-files/

When you open the log files C:/java-workspace/log/logigng.log
you will see 
--- 2016-01-25 15:06:34,802 [4-32] INFO  remote_logging - some msg

No comments: