在JavaWeb服务上记录事件

在JavaWeb服务上记录事件,java,mysql,log4j,tomcat7,Java,Mysql,Log4j,Tomcat7,我目前正在开发一个在ApacheTomcat7上运行的Java Web应用程序。由于我想将一些信息记录到数据库中,因此我使用以下配置文件信息来启动记录器: log4j.rootLogger = DEBUG, DB log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender log4j.appender.DB.URL=jdbc:mysql://localhost:3306/cap_recommender_log log4j.appender.DB.dr

我目前正在开发一个在ApacheTomcat7上运行的Java Web应用程序。由于我想将一些信息记录到数据库中,因此我使用以下配置文件信息来启动记录器:

log4j.rootLogger = DEBUG, DB
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DB.URL=jdbc:mysql://localhost:3306/cap_recommender_log
log4j.appender.DB.driver=com.mysql.jdbc.Driver
log4j.appender.DB.user=log_user
log4j.appender.DB.password=some_password
log4j.appender.DB.sql=INSERT INTO notify_service_log(date, logger, level, message) VALUES('%d{YYYY-MM-dd}','%C','%p','%m')
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
此外,notify_service_log表如下所示:

CREATE TABLE `notify_service_log` ( 
    `date` date NOT NULL,
    `logger` varchar(256) NOT NULL,
    `level` varchar(10) NOT NULL,
    `message` text NOT NULL,
    KEY `index_level` (`level`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
package com.imis.cap.service.notify;

import com.imis.cap.module.etl.EtlModuleClient;
import gr.aia.cap.eventbroker.v1.ArrayOfServiceMessage;
import gr.aia.cap.eventbroker.v1.BooleanResponse;
import gr.aia.cap.eventbroker.v1.ServiceMessage;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jws.WebService;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

@WebService(serviceName = "NotifyService", portName = "HttpBinding_NotifyService", endpointInterface = "gr.aia.cap.eventbroker.v1.NotifyService", targetNamespace = "http://www.aia.gr/cap/eventbroker/v1/", wsdlLocation = "WEB-INF/wsdl/NotifyService/NotifyService.wsdl")
public class NotifyService {

    private String username;

    private String password;

    private String log_properties_file;

    private DataSource registration_db;

    private static org.apache.log4j.Logger notifyServiceLogger = 
        Logger.getLogger(NotifyService.class);

    public NotifyService() {
        Context context;
        try {
            context = (Context) new InitialContext().lookup("java:comp/env");
            this.username = (String) context.lookup("CAP_RECOMMENDER_UNAME");
            this.password = (String) context.lookup("CAP_RECOMMENDER_PASS");
            this.registration_db = (DataSource) context.lookup("jdbc/cap_registration_db");
            this.log_properties_file = (String) context.lookup("NOTIFY_SERVICE_LOG_PROPERTIES_FILE");
        }catch(NamingException e) {
            System.err.println(e.getMessage());
            notifyServiceLogger.error("NotifyService: NamingException occured during construction. Message: " +
                e.getMessage());
        }
        PropertyConfigurator.configure(this.log_properties_file);
        notifyServiceLogger.info("NotifyService: Object constructor completed successfully.");
   }

   public gr.aia.cap.eventbroker.v1.BooleanResponse notify(gr.aia.cap.eventbroker.v1.NotifyRequest request) throws ParseException {
    ArrayOfServiceMessage arrayOfServiceMsg = new ArrayOfServiceMessage();
    ServiceMessage msg = new ServiceMessage();
    BooleanResponse response = new BooleanResponse();
    EventTypeReader eventType = new EventTypeReader(request);
    String regCode = request.getRegistrationCode();
    String dbRegCode = "";
        **notifyServiceLogger.info("NotifyService.notify(): Called with reg-code: " + request.getRegistrationCode() + ".");**

    /**Some more code**/
    return new BooleanResponse(); //Sample response
    }
}
package notifyclient;

import gr.aia.cap.eventbroker.v1.ArrayOfAttribute;
import gr.aia.cap.eventbroker.v1.BooleanResponse;
import gr.aia.cap.eventbroker.v1.EventType;
import gr.aia.cap.eventbroker.v1.NotifyRequest;

public class NotifyClient {

    public static void main(String[] args) {
        NotifyRequest request = new NotifyRequest();
        request.setRegistrationCode("blasdadasd");
        request.setAttributes(new ArrayOfAttribute());
        request.setEventType(EventType.USER);
        BooleanResponse response = notify(request);
        System.out.println("Output: " + response.getErrors().getServiceMessage().toString());
    }

    public static gr.aia.cap.eventbroker.v1.BooleanResponse notify(gr.aia.cap.eventbroker.v1.NotifyRequest request) {
        gr.aia.cap.eventbroker.v1.NotifyService_Service service = new gr.aia.cap.eventbroker.v1.NotifyService_Service();
        gr.aia.cap.eventbroker.v1.NotifyService port = service.getHttpBindingNotifyService();
        return port.notify(request);
    }
}
最后,web服务代码如下所示:

CREATE TABLE `notify_service_log` ( 
    `date` date NOT NULL,
    `logger` varchar(256) NOT NULL,
    `level` varchar(10) NOT NULL,
    `message` text NOT NULL,
    KEY `index_level` (`level`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
package com.imis.cap.service.notify;

import com.imis.cap.module.etl.EtlModuleClient;
import gr.aia.cap.eventbroker.v1.ArrayOfServiceMessage;
import gr.aia.cap.eventbroker.v1.BooleanResponse;
import gr.aia.cap.eventbroker.v1.ServiceMessage;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jws.WebService;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

@WebService(serviceName = "NotifyService", portName = "HttpBinding_NotifyService", endpointInterface = "gr.aia.cap.eventbroker.v1.NotifyService", targetNamespace = "http://www.aia.gr/cap/eventbroker/v1/", wsdlLocation = "WEB-INF/wsdl/NotifyService/NotifyService.wsdl")
public class NotifyService {

    private String username;

    private String password;

    private String log_properties_file;

    private DataSource registration_db;

    private static org.apache.log4j.Logger notifyServiceLogger = 
        Logger.getLogger(NotifyService.class);

    public NotifyService() {
        Context context;
        try {
            context = (Context) new InitialContext().lookup("java:comp/env");
            this.username = (String) context.lookup("CAP_RECOMMENDER_UNAME");
            this.password = (String) context.lookup("CAP_RECOMMENDER_PASS");
            this.registration_db = (DataSource) context.lookup("jdbc/cap_registration_db");
            this.log_properties_file = (String) context.lookup("NOTIFY_SERVICE_LOG_PROPERTIES_FILE");
        }catch(NamingException e) {
            System.err.println(e.getMessage());
            notifyServiceLogger.error("NotifyService: NamingException occured during construction. Message: " +
                e.getMessage());
        }
        PropertyConfigurator.configure(this.log_properties_file);
        notifyServiceLogger.info("NotifyService: Object constructor completed successfully.");
   }

   public gr.aia.cap.eventbroker.v1.BooleanResponse notify(gr.aia.cap.eventbroker.v1.NotifyRequest request) throws ParseException {
    ArrayOfServiceMessage arrayOfServiceMsg = new ArrayOfServiceMessage();
    ServiceMessage msg = new ServiceMessage();
    BooleanResponse response = new BooleanResponse();
    EventTypeReader eventType = new EventTypeReader(request);
    String regCode = request.getRegistrationCode();
    String dbRegCode = "";
        **notifyServiceLogger.info("NotifyService.notify(): Called with reg-code: " + request.getRegistrationCode() + ".");**

    /**Some more code**/
    return new BooleanResponse(); //Sample response
    }
}
package notifyclient;

import gr.aia.cap.eventbroker.v1.ArrayOfAttribute;
import gr.aia.cap.eventbroker.v1.BooleanResponse;
import gr.aia.cap.eventbroker.v1.EventType;
import gr.aia.cap.eventbroker.v1.NotifyRequest;

public class NotifyClient {

    public static void main(String[] args) {
        NotifyRequest request = new NotifyRequest();
        request.setRegistrationCode("blasdadasd");
        request.setAttributes(new ArrayOfAttribute());
        request.setEventType(EventType.USER);
        BooleanResponse response = notify(request);
        System.out.println("Output: " + response.getErrors().getServiceMessage().toString());
    }

    public static gr.aia.cap.eventbroker.v1.BooleanResponse notify(gr.aia.cap.eventbroker.v1.NotifyRequest request) {
        gr.aia.cap.eventbroker.v1.NotifyService_Service service = new gr.aia.cap.eventbroker.v1.NotifyService_Service();
        gr.aia.cap.eventbroker.v1.NotifyService port = service.getHttpBindingNotifyService();
        return port.notify(request);
    }
}
执行notify过程调用的客户端代码如下所示:

CREATE TABLE `notify_service_log` ( 
    `date` date NOT NULL,
    `logger` varchar(256) NOT NULL,
    `level` varchar(10) NOT NULL,
    `message` text NOT NULL,
    KEY `index_level` (`level`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
package com.imis.cap.service.notify;

import com.imis.cap.module.etl.EtlModuleClient;
import gr.aia.cap.eventbroker.v1.ArrayOfServiceMessage;
import gr.aia.cap.eventbroker.v1.BooleanResponse;
import gr.aia.cap.eventbroker.v1.ServiceMessage;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jws.WebService;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

@WebService(serviceName = "NotifyService", portName = "HttpBinding_NotifyService", endpointInterface = "gr.aia.cap.eventbroker.v1.NotifyService", targetNamespace = "http://www.aia.gr/cap/eventbroker/v1/", wsdlLocation = "WEB-INF/wsdl/NotifyService/NotifyService.wsdl")
public class NotifyService {

    private String username;

    private String password;

    private String log_properties_file;

    private DataSource registration_db;

    private static org.apache.log4j.Logger notifyServiceLogger = 
        Logger.getLogger(NotifyService.class);

    public NotifyService() {
        Context context;
        try {
            context = (Context) new InitialContext().lookup("java:comp/env");
            this.username = (String) context.lookup("CAP_RECOMMENDER_UNAME");
            this.password = (String) context.lookup("CAP_RECOMMENDER_PASS");
            this.registration_db = (DataSource) context.lookup("jdbc/cap_registration_db");
            this.log_properties_file = (String) context.lookup("NOTIFY_SERVICE_LOG_PROPERTIES_FILE");
        }catch(NamingException e) {
            System.err.println(e.getMessage());
            notifyServiceLogger.error("NotifyService: NamingException occured during construction. Message: " +
                e.getMessage());
        }
        PropertyConfigurator.configure(this.log_properties_file);
        notifyServiceLogger.info("NotifyService: Object constructor completed successfully.");
   }

   public gr.aia.cap.eventbroker.v1.BooleanResponse notify(gr.aia.cap.eventbroker.v1.NotifyRequest request) throws ParseException {
    ArrayOfServiceMessage arrayOfServiceMsg = new ArrayOfServiceMessage();
    ServiceMessage msg = new ServiceMessage();
    BooleanResponse response = new BooleanResponse();
    EventTypeReader eventType = new EventTypeReader(request);
    String regCode = request.getRegistrationCode();
    String dbRegCode = "";
        **notifyServiceLogger.info("NotifyService.notify(): Called with reg-code: " + request.getRegistrationCode() + ".");**

    /**Some more code**/
    return new BooleanResponse(); //Sample response
    }
}
package notifyclient;

import gr.aia.cap.eventbroker.v1.ArrayOfAttribute;
import gr.aia.cap.eventbroker.v1.BooleanResponse;
import gr.aia.cap.eventbroker.v1.EventType;
import gr.aia.cap.eventbroker.v1.NotifyRequest;

public class NotifyClient {

    public static void main(String[] args) {
        NotifyRequest request = new NotifyRequest();
        request.setRegistrationCode("blasdadasd");
        request.setAttributes(new ArrayOfAttribute());
        request.setEventType(EventType.USER);
        BooleanResponse response = notify(request);
        System.out.println("Output: " + response.getErrors().getServiceMessage().toString());
    }

    public static gr.aia.cap.eventbroker.v1.BooleanResponse notify(gr.aia.cap.eventbroker.v1.NotifyRequest request) {
        gr.aia.cap.eventbroker.v1.NotifyService_Service service = new gr.aia.cap.eventbroker.v1.NotifyService_Service();
        gr.aia.cap.eventbroker.v1.NotifyService port = service.getHttpBindingNotifyService();
        return port.notify(request);
    }
}
此时我必须通知您,执行Web服务调用所需的类是由Netbeans 7.2自动生成的


问题在于构造函数的消息被记录到数据库中,但notify函数中的信息消息从未被记录。为什么会这样?有什么想法吗?

作为注释状态,添加
PropertyConfigurator.configure(这个.log\u properties\u文件)notify()
方法中记录器调用上方的行上的code>解决了问题。

是否调用过notify?或者这个类是刚刚构造的?是的。为了执行对web服务的请求,我开发了一个测试客户机。构造函数日志消息已插入到数据库中,但notify调用未插入。您是否可以发布客户端代码,以便在客户端出现问题时我们可以看到调用它,而不是此代码?我已使用请求的信息更新了第一篇文章。感谢您的关注。请尝试添加
PropertyConfigurator.configure(此.log\u properties\u文件)在您的信息登录到该方法之前。它可以很简单,因为在使用之前需要对其进行配置,并且其配置不在构造函数的范围之外。