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

Friday, January 22, 2016

GWT Client Side logging using Console.log

Another possible way to log client side in GWT is to use console.log method of java script.

As in GWT all client side code is converted into Java script we can add native java script method it self on the client side to print log.

Follow belwo given step to do the same.
Step 1: Add follwoing lines in your *.gwt.xml.





Step 2:- Create a class Named as DebugClientSide inside client folder.

package com.test.client;
public class DebugClientSide {
    private static boolean isEnabledFlag_ = false;
    public static void enable() { isEnabledFlag_ = true; }
    public static void setEnabled( final boolean isEnabledFlag ) 
    { isEnabledFlag_ = isEnabledFlag; }

    public static void log( final String s ) 
    { if( isEnabledFlag_ ) nativeConsoleLogMehod( s ); }

    private static native void nativeConsoleLogMehod( String s ) 
    /*-{ console.log( s ); }-*/;
}

Step 3:- Put following line on the client side class to see the console log

Debug.enable();
Debug.log("This is client side console log");

After execution you can see your client side log in console as shown below.


GWT Client Side logging

GWT is special kind of web development technogies where we are broadly classifying development in two part
1- Client side :- Getting compiled in Java Script
2- Server side :- Getting compiled in class files

Server side logging can be done by using log4j

http://siddharathadhumale.blogspot.in/2016/01/adding-log4j-to-server-side-in-gwt.html

But in terms of GWT their are many different ways to debug client side. One of them is to use the com.google.gwt.logging.Logging class of GWT.

To do client side logging using this class :-com.google.gwt.logging.Logging following changes need to be done

Step 1- Add following line in you *.gwt.xml





Step -2 Add following line in your Client side code

private static Logger rootLogger = Logger.getLogger(YourClientClass.class.toString());
rootLogger.log(Level.SEVERE, "Shwing client side loggin on the user Browser screen");

When you execute the same on browser you will be able to see above log in pop as shown below.


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=org.apache.log4j.RollingFileAppender


log4j.appender.file.File=C:/java-workspace/log/logigng.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
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/Log4j.properties");
logger.info("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