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
- In the
Project Explorer , choose
.
- Right-click and choose
.
- In the
Name field, enter a non-specific name, for example
LogText and choose
Finish .
- 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 "
- 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);
}
- Save the editor.
- 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 + ".";
}
}
- Save the current project.
Results
You have successfully entered the logging functionality.