A Simple Logging Sample Application

  Samples Index




A Simple Logging Sample Application


This sample application document contains the following sections:



Overview

The essence of this sample is to demonstrate the use of Loggers in writing enterprise applications. A Logger object is used to log messages for a specific system or application component. Log messages help developers debug their applications.

JavaTM 2 Platform Std. Ed. v1.4.0 introduces a new package java.util.logging, that has all the classes and interfaces for the core logging facilities. The central goal of the logging APIs is to support maintaining and servicing software at customer sites.

Typically, the application 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 messages might report errors and warnings, or provide informational messages about interesting events in the application's lifecycle. They can include debugging information, or even trace the execution of important methods within the program. It is not intended that all of these messages will actually be generated and logged all of the time; there is a default logging configuration file that specifies where log messages are directed to (the console, a file, a network socket, or a combination of these), how they are formatted (as plain text or XML documents), and at what severity threshold they are logged (log messages with a severity below the specified threshold are discarded with very little overhead and should not significantly impact the performance of the application.) The system administrator or end user of the application can modify this default configuration to suit their needs.

The java.util.logging package is very flexible, and can be used in a number of ways. For most applications, however, use of the Logging API is quite simple. Obtain a named Logger object whenever necessary by calling the static Logger.getLogger() method, passing the class or package name of the application as the logger name. Then use one of the many Logger instance methods to generate log messages. The easiest methods to use have names that correspond to severity levels, such as severe(), warning(), info(), and debug()

We will demonstrate the use of these loggers in our sample in two different ways:

The sample code comes ready with a wide range of logging APIs. Based on user's selection, the application starts logging messages to the server.log file. However, the FINE selection need some special treatment. See Running the Sample, section for details.

Following section will guide you through the execution of the sample.


  1. Bring up a web page with a form like the one seen in Figure 1.


A screen shot of the front page

Figure 1    Logging front page


  1. Enter your name, make your logging selection and press the Process button.


  2. A servlet gets invoked and in turn calls a stateless session bean to determine a greeting based upon the current system time.


  3. Both the servlet and the bean start writing log messages, based on user's logging selection.


  4. The servlet dispatches a JSP to deliver the greeting back to the browser.


  5. As a result of the JSP execution, you can see the page displayed in Figure 2 returned to your browser.


A screen shot of a result page

Figure 2    Logging results page

Compiling and Assembling the Application



In this section, a procedure describes how to assemble and compile the sample using a Command Line Interface (CLI).

To rebuild the entire application from scratch, follow these steps:


  1. Compile and Assemble Logging Sample. For example:

    Execute asant under <install_dir>/samples/logging/apps/simple/.

    The default target core will be executed to rebuild the WAR and EAR files.


  2. Deploy the application.

    Once you have re-created the sample application from scratch, you may proceed to Deploying the Sample Application.


  3. Clean the web application project area. For example:

    Execute asant clean to remove the sample application's assemble and build directories.

You are now ready to use this EAR file for deployment.



Deploying the Sample Application



You may select one of the following methods of deploying and registering the application:


Command Line Interface (CLI)-based Deployment

Since the sample application is supplied with a complete EAR file, the fastest means of setting up the application is to use the command line utilities as described in this section. If you would like to experience either deploying through a Web based tool, then follow instructions in GUI based Deployment.

The pre-built logging-helloworld.ear file is a Enterprise Archive file that contains the Web Archive (WAR) file of the application. Within the WAR file resides the XML deployment descriptor files, application class files, JSPs and other content required by the application.

Deploying the pre-built logging-helloworld.ear file is simple. Deploy either using a Using asant script or Using asadmin command.


Using asant script


  1. Go to the simple directory of the sample. For example:

    <install_dir>/samples/logging/apps/simple


  2. Execute the following command:

    asant deploy


Using asadmin command


  1. Go to the root of the sample directory. For example:

    <install_dir>/samples/logging/apps/simple/


  2. Execute asadmin to deploy the module to the local application server instance. For example:

    asadmin deploy --user <user-name> --password <password> --host <hostname> -port <admin server port> logging-helloworld.ear

The deployment process involves the following operations:

If you would like to verify the registration of the application, proceed to Verifying Registration. Otherwise, you are now ready to exercise the application by Running the Sample Application.



GUI Based Deployment

Since a pre-built Enterprise Archive (.ear) file for the sample application is included with the application server, you can use the Admin GUI to quickly read in the .ear file and deploy it to the application server.

  1. Start the admin server if it is not already started. Here are the commands for each platform:
    1. Unix - Execute the <install_dir>/bin/asadmin start-domain domain1 command.
    2. Windows - Click "Start->Programs->Sun Microsystems->J2EE 1.4 SDK->Start Default Server"  command.
  2. Open the .ear file.
    1. Start Admin GUI, by entering URL  http://localhost:<admin.port>/asadmin   in browser. See replacing "localhost" and <admin.port> note here to determine correct values to use for your environment
    2. You are prompted to supply the user name and password of the application server. Enter correct values for your installed application server for "User Name:"  and "Password:" input fields, then click on the "Login" button.
    3. In the left-hand frame, click Applications-->Enterprise Applications  icon in the tree.
    4. In the right-hand frame, click on the "Deploy..."  button.
    5. Choose the file to be deployed by clicking on the "Browse..."  button, and navigate to select component to deploy (logging-helloworld.ear).
    6. In the browse dialog, click on the "OK"  button.
    7. In the right-hand frame, click on the "Next"  button.
    8. In the right-hand frame, click on the "OK"  button. (Optionally fill in remaining values for application to be deployed)
    9. In the left hand pane, you will see the newly deployed module's icon in the tree. In the right hand pane you will also see the component listed as deployed.

Verifying Deployment

As an optional step, you can use the Application Server Administration Tool to verify that the application has been registered. If you do not want to verify that the application has been registered, proceed directly to Running the Sample Application.

To verify the registration of the application, do one of the following:


  • Execute asadmin to look at applications deployed with a server instance. For example:

    asadmin list-components --user admin --password <password>

    You will see the logging-helloworld app as one of the listed applications.

OR


  • Navigate the Admin tool GUI to

    Applications-->Enterprise Applications..

    Click on Enterprise Applications on the left frame menu to diplay list of applications deployed on server.



Running the Sample Application

Unlike any other sample, running this sample is not as simple as typing in the url http://<hostname>:<port>/logging-helloworld. There is more to it.

As we discussed earlier in the overview section, we have two options to choose from. They are:

  • Print INFO log using java.util.logging.Logger
  • Print FINE log using java.util.logging.Logger (Must follow the next steps below for this "Log messages at FINE log level" option to work properly.)

Setting the log level to FINE for "samples.logging.simple.servlet" logger in Admin GUI

Note - FINE messages will be filtered out. User can go to Admin Console and set the log level for "samples.logging.simple.servlet" to "FINE" level and then should be able to see logs if logged at FINE level.

  1. Start Admin GUI, by entering URL  http://localhost:<admin.port>/asadmin   in browser. See replacing "localhost" and <admin.port> note here to determine correct values to use for your environment
  2. You are prompted to supply the user name and password of the application server. Enter correct values for your installed application server for "User Name:"  and "Password:" input fields, then click on the "Login" button.
  3. In the left-hand frame, click on the "Application Server"  node.
  4. In the right-hand frame, click on the "Logging"  tab.
  5. In the right-hand frame, click on the "Log Levels"  tab.
  6. Click on the "Add Property"  button, (this button is located at the bottom of this right hand frame under section titled "Additional Module Log Level Properties") and enter text samples.logging.simple.servlet in the Name field and enter FINE in the Value field.
  7. At the top of the right hand frame, click on the "Save"  button.
  8. Restart the server for the changes to take effect.
  9. Log back into the Admin GUI as noted above.
  10. In the right-hand frame, click on the "Search Log Files"  link. This is located under the Common Tasks section. This step will open the Log Viewer.
  11. Search for the name of the application's custom logger by entering "samples.logging.simple.servlet",  which is the name of the custom application logger, in the Custom Logger text area, and then click on the "Search"  button at the bottom of the Log Viewer window.
  12. Then you should see Log Viewer Results (40 Items) for the application custom logger "samples.logging.simple.servlet",  at the top of the Log Viewer window.

Log messages will be generated in server.log file.

Output (Option 1-INFO)
[#|2004-08-04T17:12:26.761-0400|INFO|sun-appserver-pe8.1|samples.logging.simple. servlet|_ThreadID=29;|GreeterServlet is executing ...|#] [#|2004-08-04T17:12:26.761-0400|INFO|sun-appserver-pe8.1|samples.logging.simple. servlet|_ThreadID=29;|Looking up greeter bean home interface using JNDI name: ja va:comp/env/ejb/logger|#] [#|2004-08-04T17:12:26.871-0400|INFO|sun-appserver-pe8.1|samples.logging.simple. servlet|_ThreadID=29;|Bean found!!!|#] [#|2004-08-04T17:12:26.871-0400|INFO|sun-appserver-pe8.1|samples.logging.simple. servlet|_ThreadID=29;|Creating a remote bean object...|#] [#|2004-08-04T17:12:26.881-0400|INFO|sun-appserver-pe8.1|samples.logging.simple. servlet|_ThreadID=29;|Calling bean's getGreeting method...|#] [#|2004-08-04T17:12:26.971-0400|INFO|sun-appserver-pe8.1|samples.logging.simple. ejb|_ThreadID=29;|ejbCreate() on obj samples.logging.simple.ejb.GreeterEJB@11a72 fd|#] [#|2004-08-04T17:12:26.971-0400|INFO|sun-appserver-pe8.1|samples.logging.simple. ejb|_ThreadID=29;|inside the getGreeting method of the bean...|#] [#|2004-08-04T17:12:26.971-0400|INFO|sun-appserver-pe8.1|samples.logging.simple. ejb|_ThreadID=29;|returning message to the caller...|#] [#|2004-08-04T17:12:26.981-0400|INFO|sun-appserver-pe8.1|samples.logging.simple. servlet|_ThreadID=29;|Got message from greeter bean: afternoon|#] [#|2004-08-04T17:12:26.981-0400|INFO|sun-appserver-pe8.1|samples.logging.simple. servlet|_ThreadID=29;|Storing the message in request object|#] [#|2004-08-04T17:12:26.981-0400|INFO|sun-appserver-pe8.1|samples.logging.simple. servlet|_ThreadID=29;|Dispatching JSP for output|#]

Output (Option 2 -FINE)
Note- FINE messages will be filtered out. User can go to Admin Console and set the log level for "samples.logging.simple.servlet" to "FINE" level and then will be able to see logs, if logged at FINE level.

[#|2004-08-04T17:12:26.836-0500|FINE|sun-appserver-pe8.0|samples.logging.simple.servlet|_ThreadID=12;|GreeterServlet is executing ...|#] [#|2004-08-04T17:12:26.836-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=12;|Looking up greeter bean home interface using JNDI name: java:comp/env/ejb/logger|#] [#|2004-08-04T17:12:26.876-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=12;|Bean found!!!|#] [#|2004-08-04T17:12:26.876-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=12;|Creating a remote bean object...|#] [#|2004-08-04T17:12:26.886-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=12;|Calling bean's getGreeting method...|#] [#|2004-08-04T17:12:27.046-0500|INFO|sun-appserver-pe8.1|samples.logging.simple.ejb|_ThreadID=12;|ejbCreate() on obj samples.logging.simple.ejb.GreeterEJB@31ac05|#] [#|2004-08-04T17:12:27.046-0500|INFO|sun-appserver-pe8.1|samples.logging.simple.ejb|_ThreadID=12;|inside the getGreeting method of the bean...|#] [#|2004-08-04T17:12:27.046-0500|INFO|sun-appserver-pe8.1|samples.logging.simple.ejb|_ThreadID=12;|returning message to the caller...|#] [#|2004-08-04T17:12:27.046-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=12;|Got message from greeter bean: morning|#] [#|2004-08-04T17:12:27.046-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=12;|Storing the message in request object|#] [#|2004-08-04T17:12:27.046-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=12;|Dispatching JSP for output|#] [#|2004-08-04T17:15:45.882-0500|INFO|sun-appserver-pe8.1|javax.enterprise.system.container.web|_ThreadID=13;|WebModule[/asadmin]WARNING: No 'dtdURLBase' init parameter defined in Servlet config!|#] [#|2004-08-04T17:21:11.570-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=14;|GreeterServlet is executing ...|#] [#|2004-08-04T17:21:11.570-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=14;|Looking up greeter bean home interface using JNDI name: java:comp/env/ejb/logger|#] [#|2004-08-04T17:21:11.570-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=14;|Bean found!!!|#] [#|2004-08-04T17:21:11.570-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=14;|Creating a remote bean object...|#] [#|2004-08-04T17:21:11.580-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=14;|Calling bean's getGreeting method...|#] [#|2004-08-04T17:21:11.580-0500|INFO|sun-appserver-pe8.1|samples.logging.simple.ejb|_ThreadID=14;|inside the getGreeting method of the bean...|#] [#|2004-08-04T17:21:11.580-0500|INFO|sun-appserver-pe8.1|samples.logging.simple.ejb|_ThreadID=14;|returning message to the caller...|#] [#|2004-08-04T17:21:11.580-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=14;|Got message from greeter bean: morning|#] [#|2004-08-04T17:21:11.580-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=14;|Storing the message in request object|#] [#|2004-08-04T17:21:11.580-0500|FINE|sun-appserver-pe8.1|samples.logging.simple.servlet|_ThreadID=14;|Dispatching JSP for output|#]



Troubleshooting




  • Although the war, ear, context-root and servlet alias names in this sample are all logging-helloworld, it is not necessary that it be so. The application will work as expected if the names of each of these attributes is unique. Rebuild the app after changing the context-root (in application.xml), the servlet alias (in web.xml) and the war and ear names, redeploy and test the sample to confirm this.


  • If you encounter problems when running the application, review the log files at <install_dir>/domains/<domain_name>/logs/server.log to learn what exactly went wrong.



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

Last Updated August 23, 2004