我是Web服务的新手,并在我的localhost(Websphere Application Server 8)上发布了一个Web服务。
我能够成功使用Web服务,但无法找到生成日志的位置,甚至无法生成日志。
我在 WebContent / WEB-INF / classes 中放置了 log4j.properties 文件,但是当我尝试从中生成WSDL时,此文件是自动删除的文件Java (步骤 - 右键单击项目名称 - >新建 - > gt; Web服务 - >选择类)。
生成的文件:
并在 WebContent / WEB-INF / lib 中添加了一些JAR。
删除文件后,我再次将 log4j.properties 复制到 WebContent / WEB-INF / classes ,并导出部署在服务器上的EAR。
我已经检查了文件夹中的日志:
但没有生成日志文件。
主要网络服务 - >
package com.gateway.request.demo;
import java.util.Date;
import javax.jws.WebMethod;
import javax.jws.WebService;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
@WebService
public class GetDemoTicketv2 {
//Logger log = Logger.getLogger(GetDemoTicket.class);
Logger log = LogManager.getLogger(GetDemoTicketv2.class);
public GetDemoTicketv2() {
super();
// TODO Auto-generated constructor stub
}
@WebMethod
public String getTicket(String serviceName, String actionName, String bodyStr, String userId){
Date curDate = new Date();
log.debug("Starting Execution");
log.info("Service Name is: "+serviceName);
log.info("Action is: "+actionName);
log.info("Body: "+bodyStr);
log.info("User ID is: "+userId);
log.debug("Execution Done");
serviceName = "Service name received: "+serviceName+" "+curDate.toString()+"\n";
actionName = "Action name received: "+actionName+" "+curDate.toString()+"\n";
bodyStr = "Body string received: "+bodyStr+" "+curDate.toString()+"\n";
userId = "User ID received: "+userId+" "+curDate.toString()+"\n";
log.info("Return Values is \n"+serviceName+actionName+bodyStr+userId);
return serviceName+actionName+bodyStr+userId;
}
/*public static void main(String[] args) {
System.out.println("CHECKING...");
GetDemoTicketv2 obj = new GetDemoTicketv2();
String retVal = obj.getTicket("My Service T", "My Action T", "My Body String T", "My User ID T");
System.out.println(retVal);
}*/}
客户端 - >
package com.gateway.request.demo;
import java.rmi.RemoteException;
public class TestService {
public static void main(String[] args) {
// TODO Auto-generated method stub
GetDemoTicketv2Proxy pxy = new GetDemoTicketv2Proxy("http://hostname:9081/Gateway_Webservice_For_ISTM_v3.0/GetDemoTicketv2Service");
try {
String retVal = pxy.getTicket("SERVICE", "ACTION", "BODY", "USER ID");
System.out.println("Value returned:\n"+retVal);
} catch (RemoteException e) {
// TODO Auto-generated catch block
System.out.println("RemoteException while consuming web service");
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
System.out.println("Exception while consuming web service");
e.printStackTrace();
}
}}
log4j.properties
log4j.rootLogger=DEBUG, main
log4j.appender.main=org.apache.log4j.RollingFileAppender
log4j.appender.main.MaxFileSize=15MB
log4j.appender.main.MaxBackupIndex=10
log4j.appender.main.File=logs/DemoRequest.log
log4j.appender.main.layout=org.apache.log4j.PatternLayout
log4j.appender.main.layout.ConversionPattern=%d [%t] %-5p %c{1} - %m%n
答案 0 :(得分:1)
从上面的代码片段中看来,它似乎缺少加载log4j.properties文件的逻辑,你应该在程序的开头加载它。所以请尝试添加以下内容:
Properties applicationProp = new Properties();
String applicationPropFilePath = "C:\\log4j.properties";
load( applicationPropFilePath, applicationProp );
/* This method is a cookie-cutter and loads the given properties file to the given handle */
public static void load( String propFilepath, Properties propFileHandle ){
try {
InputStream is = new FileInputStream( propFilepath );
propFileHandle.load( is );
} catch (FileNotFoundException e) {
System.out.println(" Unable to locate properties file: " + propFilepath + e );
} catch (IOException e) {
System.out.println(" IO Error while loading properties file: " + propFilepath + e );
} catch (Exception e) {
System.out.println(" Some error occurred while loading properties file: " + propFilepath + e );
}