September 23, 2016

Logging with GWT

1. First, modify our module configuration file (X.gwt.xml) where X is the class which holds the EntryPoint of your application.

<inherits name="com.google.gwt.logging.Logging" />
<set-property name="gwt.logging.logLevel" value="INFO"/>
<set-property name="gwt.logging.enabled" value="TRUE"/>
<set-property name="gwt.logging.consoleHandler" value="ENABLED"/>
<set-property name="gwt.logging.popupHandler" value="DISABLED" /><!-- Disable PopupLogHandler -->
<set-property name="gwt.logging.simpleRemoteHandler" value="DISABLED" />


2. This configuration cause logs to be printed to standard output, that is usually Eclipse console window. You can also view this logs in Javascript console, which present in Chrome, Firefox and IE.

See here on how to view them in Javascript console : http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers


3. In your class, import the library.

import java.util.logging.Logger;
import java.util.logging.Level;


4. Then, instantiate the logger with whatever name, possibly with the class name to make it more descriptive.

public Logger logger = Logger.getLogger("MyClassLogger");


5. Then it can be used anywhere within it's scope. Examples :

logger.log(Level.SEVERE, "Connection refused: ");
logger.log(Level.INFO, "Query executed : " + executedQuery);


6. The log levels define the severity of a message. The Level class is used to define which messages should be written to the log. You can set the minimum level by the following :

logger.setLevel(Level.INFO); 


7. These are the typical usage of these levels :

ERROR: Any error/exception that is or might be critical.

WARN: Any message that might warn us of potential problems, e.g. when a user tried to log in with wrong credentials - which might indicate an attack if that happens often or in short periods of time

INFO: Anything that we want to know when looking at the log files, e.g. when a scheduled job started/ended.