Tuesday Feb 08, 2011

Configure my Custom Log Handler in GlassFish 3.1

How do I configure my Custom Log Handler in GlassFish 3.1?

By default GlassFish logs data under server.log using pre-defined format. You may find that you want to log messages to a database, send them to a remote server or log messages from specific loggers to your own file. This can be done by writing a custom log handler. It is pretty straight forward you can add the handler, a Java class as specified by the JDK. In the examples below I log messages to a specific file in different format but you can replace that code with something that interests you.

1. Sample Handler (NewHandler.java):

package customhandler;

/\*\*
 \* @author naman mehta
 \*/

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

/\*\*
 \* New Handler
 \*/
public class NewHandler extends Handler {

    static BufferedWriter f = null;

    public NewHandler() {
        try {
            String userDir = "/space/output";
            f = new BufferedWriter(new FileWriter(userDir + "/mylogging.log"));
        } catch (IOException e) {
            System.out.println("not able to create log file." + e);
        }
    }


    /\*\*
     \* Overridden method used to capture log entries   \*
     \*
     \* @param record The log record to be written out.
     \*/
    public void publish(LogRecord record) {

        // first see if this entry should be filtered out
        // the filter should keep anything
        if (getFilter() != null) {
            if (!getFilter().isLoggable(record))
                return;
        }

        try {
            f.write("NewHandler output - ");
            f.write("logger name: " + record.getLoggerName());
            f.write(" source classname: " + record.getSourceClassName());
            f.write(" message: " + record.getMessage());
            f.newLine();
            f.flush();
        } catch (IOException ex) {
            System.out.println("not able to write to log file." + ex);
        }

    }


    /\*\*
     \* Called to close this log handler.
     \*/
    public void close() {
        try {
            f.close();
        } catch (IOException ex) {
        }
    }


    /\*\*
     \* Called to flush any cached data
     \*/
    public void flush() {
	// not used
    }
}

In above sample I am logging data under '/space/output/mylogging.log'. I also changed the output format for logging data under mylogging.log file(Check publish(LogRecord record)function).

2. Changes needed under logging.properties file (GF_HOME/glassfish/domains/domain1/config):

#GlassFish logging.properties list
#Mon Mar 16 22:32:12 PDT 2009
javax.enterprise.system.tools.admin.level=INFO
handlers=java.util.logging.ConsoleHandler, customhandler.NewHandler
java.util.logging.ConsoleHandler.formatter=com.sun.enterprise.server.logging.UniformLogFormatter
org.apache.jasper.level=INFO

Installation of custom Handler

1. Write your own custom handler as defined above 'NewHandler.java'. Compile the same and create jar for the same (e.g. customhandler.jar).
2. Copy 'customhandler.jar' under GF_HOME/glassfish/domains/domain1/lib/ext folder.
3. Make changes in default logging.properties file as mentioned above. Add your own handler class like 'handlers=java.util.logging.ConsoleHandler, customhandler.NewHandler'.
4. Restart the Glassfish.

Now when you restart the glassfish server it logs data under '/space/output/mylogging.log' file and you can find the logging statement under mylogging.log file as below:

NewHandler output - logger name: org.glassfish.ha.store.spi.BackingStoreFactoryRegistry source classname: org.glassfish.ha.store.spi.BackingStoreFactoryRegistry message: Registered org.glassfish.ha.store.adapter.cache.ShoalBackingStoreProxy for persistence-type = replicated in BackingStoreFactoryRegistry
NewHandler output - logger name: javax.enterprise.system.core.com.sun.enterprise.v3.services.impl source classname: com.sun.enterprise.v3.services.impl.GrizzlyProxy$2$1 message: Grizzly Framework 1.9.31 started in: 693ms - bound to [0.0.0.0:8080]
NewHandler output - NewHandler output - logger name: javax.enterprise.system.core.com.sun.enterprise.v3.services.impl source classname: com.sun.enterprise.v3.services.impl.GrizzlyProxy$2$1 message: Grizzly Framework 1.9.31 started in: 350ms - bound to [0.0.0.0:7676]
NewHandler output - logger name: javax.enterprise.system.core.com.sun.enterprise.v3.services.impl source classname: com.sun.enterprise.v3.services.impl.GrizzlyProxy$2$1 message: Grizzly Framework 1.9.31 started in: 539ms - bound to [0.0.0.0:4848]
NewHandler output - logger name: javax.enterprise.system.core.com.sun.enterprise.v3.services.impl source classname: com.sun.enterprise.v3.services.impl.GrizzlyProxy$2$1 message: Grizzly Framework 1.9.31 started in: 433ms - bound to [0.0.0.0:3700]
logger name: javax.enterprise.system.core.com.sun.enterprise.v3.services.impl source classname: com.sun.enterprise.v3.services.impl.GrizzlyProxy$2$1 message: Grizzly Framework 1.9.31 started in: 604ms - bound to [0.0.0.0:8181]
NewHandler output - logger name: javax.enterprise.system.core.com.sun.enterprise.v3.admin.adapter source classname: com.sun.enterprise.v3.admin.adapter.AdminConsoleAdapter message: The Admin Console is already installed, but not yet loaded.
NewHandler output - logger name: javax.enterprise.system.core.com.sun.enterprise.v3.server source classname: com.sun.enterprise.v3.server.AppServerStartup message: GlassFish Server Open Source Edition 3.2-SNAPSHOT (naman-private) startup time : Felix (3,993ms), startup services(9,065ms), total(13,058ms)
NewHandler output - logger name: javax.enterprise.system.tools.admin.org.glassfish.server source classname: org.glassfish.admin.mbeanserver.JMXStartupService$JMXConnectorsStarterThread message: JMXStartupService: Started JMXConnector, JMXService URL = service:jmx:rmi://naman:8686/jndi/rmi://naman:8686/jmxrmi

About

Naman

Search

Categories
Archives
« February 2011
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
     
       
Today