Sun Java System Application Server 

Samples Index

The Logging Sample Application

The Logging application demonstrates using Logger objects to log messages for a specific system or application component. Log messages can help developers to debug their applications.

The Java Standard Edition (J2SE) a package java.util.logging contains all the classes and interfaces for the core logging facilities.

Typically, a developer uses a Logger object with a name that corresponds to the class or package name of the application to generate log messages at any of seven severity levels (defined by the Level class). These log messages might report errors, warnings, or informational messages. They can include debugging information, or even trace the execution of important methods within the program.

The messages are generated and logged according to a default logging configuration file which specifies where the messages are directed (to the console, a file, a network socket, or a combination of these outputs), how they are formatted (as ASCII text or XML documents), and at what severity level they are logged (messages with a severity below the specified level are discarded and should not significantly affect performance). The system administrator or end user of the application can modify this default configuration as needed.

Applications can use the java.util.logging package in various ways. The general procedure is:

This sample demonstrates using java.util.logging.Logger to print the INFO log and the FINE log.

The sample code includes a range of logging APIs. Based on user selection, the application starts logging messages to the server.log file. However, messages at the FINE selection require additional steps. See Run the Application for details.

The following steps describe the execution of this sample:

  1. Open a web page that contains a form with a field for user input labeled "Enter Name:" and instructions to select one of two radio button options: "Log messages at INFO log level" or "Log messages at FINE log level." The page should also include a button labeled "Process."
  2. Enter your name, select a logging option, and click the Process button.
  3. A servlet is invoked, which in turn calls a stateless session bean to determine a greeting based on the current system time.
  4. Both the servlet and the bean start writing log messages which are based on your logging selection.
  5. The servlet uses JavaServer Pages (JSP) technology to deliver the greeting back to the browser.
  6. The browser displays a page that includes the title "Hello World!" along with a personal greeting.

Building, Deploying, and Running the Application

The general instructions for building, deploying, and running sample applications are provided in Building, Deploying, and Running Sample Applications. In reading this document, use the following application-specific variables for this application:

Follow this general procedure:

  1. Build the Application. The build file, build.xml, is in the app_dir directory, specifically, install_dir/samples/logging/apps/simple.
  2. Deploy the Application. The EAR file is install_dir/samples/logging/apps/simple/logging-helloworld.ear.
  3. Run the Application. The application name is logging-helloworld.

Run the Application

Run the application by entering this URL in your browser:

http://hostname:port/logging-helloworld

As described above, the application includes options for two log levels, INFO and FINE. While most Log messages are stored in the server.log file, FINE messages are filtered out.

To view messages that are logged at the FINE level, you'll need to set the log level for samples.logging.simple.servlet to FINE from the Admin Console. Follow the steps below:

  1. Start the Admin GUI from this URL: http://localhost:admin.port/asadmin
  2. Type the user name and password for your application server and log in.
  3. Click Application Server in the left frame.
  4. Click the Logging tab in the right frame.
  5. Click the Log Levels tab in the right frame.
  6. Click the Add Property button at the bottom of the right frame.
  7. Type samples.logging.simple.servlet in the Name field, and type FINE in the Value field.
  8. Click Save at the top of the right frame.
  9. Restart the server so that the changes take effect.
  10. Log back into the Admin GUI as described in Steps 1 and 2 above.
  11. Click the Search Log Files link in the right frame under the Common Tasks section.
    The Log Viewer opens.
  12. Search for the name of the application's custom logger by typing samples.logging.simple.servlet in the Custom Logger text field, then cllicking the Search button at the bottom of the Log Viewer window.
    Log viewer results display at the top of the Log Viewer window.

Copyright © 2006 Sun Microsystems, Inc. All rights reserved.