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

Wednesday Dec 15, 2010

Analyze server logs in GlassFish 3.1

How to analyze logs in GlassFish 3.1?

There are two options available:
1. Fire up GlassFish and open the admin web UI inside a browser (for example http://localhost:4848) and launch Log Viewer.
2. Use the collect-log-files subcommand in remote mode to collect log files into a ZIP archive.

In above both options, it would download log files even if your instance is down. Means, you can analyze your logs even if your instance is crashed.

How to use web UI?

1. Fire up GlassFish and open the admin web UI inside a browser (for example http://localhost:4848).
2. Click on 'Server(Admin Server)' -> Click on 'View Log Files' Button.
3. It opens new window with recent log records.
4. If you have multiple instances running then select another instance from 'Server' drop down and click on 'Search' button.
5. If you have multiple log files(rotated server log files) then select another file name from 'Log File' drop down list and click on 'Search' button.

How to use collect-log-files command?

Use the collect-log-files subcommand in remote mode to collect log files into a ZIP archive. Optionally, you can target a server name/cluster name/stand alone instance name. Default value for target is Server. For given target command would download all log files from log folder and make it zip. Default location for storing zip files under GlassFish_Home/domains/domain1/collected-logs. If user needs to store it under specific folder then specify the folder path with --retrieve option as true.

1. Run following command to get log files for Server

./asadmin collect-log-files
Created Zip file under /space/gfv3/v3setup/glassfish3/glassfish/domains/domain1/collected-logs/log_2010-12-15_15-46-23.zip.
Command collect-log-files executed successfully.

2. Run following command to get log files for Cluster having two instances running.

./asadmin collect-log-files --target cluster1
Log files are downloaded for instance1.
Log files are downloaded for instance2.
Created Zip file under /space/gfv3/v3setup/glassfish3/glassfish/domains/domain1/collected-logs/log_2010-12-15_15-54-06.zip.
Command collect-log-files executed successfully.

3. Run following command to get log files for Cluster having two instances running under /space/output directory.

./asadmin collect-log-files --target cluster1 --retrieve true /space/output
Log files are downloaded for instance1.
Log files are downloaded for instance2.
Created Zip file under /space/output/log_2010-12-15_15-55-54.zip.
Command collect-log-files executed successfully.

Unzip the generated zip file and analyze your logs. When you unzip log_2010-12-15_15-55-54.zip the directory structure looks like as below:

GlassFish_Home/domains/domain1/collected-logs/logs
	- instance1
		- server.log
		- .....
	- instance2
		- server.log
		- .....

About

Naman

Search

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