Show TOC

Implementing Logs in the HelloWorld ApplicationLocate this document in the navigation structure

Prerequisites

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

Context

Use this procedure to implement log messages in the created HelloWorld application.

Procedure

  1. In the Project Explorer , choose Start of the navigation path HelloWorldEJB  Next navigation step ejbModule Next navigation step com.sap.tutorial.helloworld End of the navigation path.
  2. Right-click and choose Start of the navigation path New Next navigation step Class End of the navigation path.
  3. In the Name field, enter a non-specific name, for example LogText and choose Finish .
  4. In the LogText.java editor, replace the content with the following code sample:
    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");
                 }
    }
                   

    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:
    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 will be traced with the exception stack trace...", e); 
    }
                   
  6. Save the editor.
  7. In the HelloBean.java window, insert the code line LogText.demonstrate(); .

    Your updated code seems as follows:

    package com.sap.tutorial.helloworld;
    import javax.ejb.Stateless;
    import javax.jws.WebService;
     @WebService(endpointInterface="com.sap.tutorial.helloworld.HelloBeanRemote", 
    portName="HelloBeanPort", serviceName="HelloBeanService", 
    targetNamespace="http://sap.com/tutorial/helloworld/")
    @Stateless(name="HelloBean")
    public class HelloBean implements HelloBeanRemote, HelloBeanLocal {
       private String message = "Hello, ";
       public String sayHello(String name) {
               LogText.demonstrate();          
       return message + name + ".";
          }
    }
                   
  8. Save the current project.

Results

You have successfully entered the logging functionality.