Show TOC

Procedure documentationImplementing Logs in the HelloWorld Application Locate this document in the navigation structure

Prerequisites

You have successfully exposed the HelloWorld EJB as a Web Service.

Procedure

  1. In the Project Explorer, choose   HelloWorldEJB   ejbModule   com.sap.tutorial.helloworld  .

  2. Right-click com.sap.tutorial.helloworld and choose   New   Class  .

  3. In the Name field, enter a non-specific name, for example LogTest.

  4. In the empty class window, enter the following code sample:

    Syntax Syntax

    1. package com.sap.tutorial.helloworld;
      import com.sap.tc.logging.Location;
      import com.sap.tc.logging.Category;
      import com.sap.tc.logging.Severity;
      import com.sap.tc.logging.SimpleLogger;
      public class LogTest {
         private static final Location loc = Location.getLocation(Location.getLocation("log.test"));
         public static void demonstrate() {
      	SimpleLogger.log(Severity.INFO, Category.SYS_SERVER, loc, "api:ab0200", 
               "Component LogRecommendations started");
                   }
      }
    End of the code.

    Using this code, the application will output the INFO message:

    Component LogRecommendations started

  5. If you want some more details to be logged (with exception), for example a timer, add the following code sample in the end:

    Syntax Syntax

    1. try { //do something to be logged
                 } catch (Exception e) {  
                     SimpleLogger.log(Severity.ERROR, Category.SYS_SERVER, loc, "api:ab0201", 
      "Problem communicating with DB during demonstrating LogExamples. 
       See traces for specific details. "); 
                     SimpleLogger.traceThrowable(Severity.ERROR, loc, "a relevant meaningful message.
      But not an ex.getMessage() because an exception message will be 
      traced with the exception stack trace...", e); 
      }
    End of the code.
  6. In the HelloBean.java window, insert the code line LogTest.demonstrate();.

    Your updated code seems as follows:

    Syntax Syntax

    1. package com.sap.tutorial.helloworld;
      import javax.ejb.Stateless;
      import javax.jws.WebService;
       @WebService(endpointInterface="com.sap.tutorial.helloworld.HelloRemote", 
      portName="HelloBeanPort", serviceName="HelloService", 
      targetNamespace="http://sap.com/tutorial/helloworld/")
      @Stateless(name="HelloBean")
      public class HelloBean implements HelloRemote, HelloLocal {
         private String message = "Hello, ";
         public String sayHello(String name) {
      	   LogTest.demonstrate();	   
         return message + name + ".";
            }
      }
    End of the code.
  7. Save the current project.

Result

You have successfully entered the logging functionality.

Next step: Deploying the HelloWorld Application