java - 在 Java Web 服务上记录事件

标签 java mysql log4j tomcat7

我目前正在开发一个在 Apache Tomcat 7 上运行的 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

最后web服务代码如下:

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 自动生成。

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

最佳答案

如评论所述,添加 PropertyConfigurator.configure(this.log_properties_file);notify() 中记录器调用上方的行方法解决了问题。

关于java - 在 Java Web 服务上记录事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13954119/

相关文章:

java - 在一个别名下将服务器信任证书链导出到 Truststore

java - 如何停止循环然后继续?

Java 对象数组,要求两个不同的输入?

MySQL 搜索 TEXT 字段以匹配文本,用于多行绝对

mysql - 如何获得给定日期的上周五和下周四?

java - 在 hibernate 中包含 log4j 属性文件以显示带有值而不是问号的查询

java - 将 System.out 和 System.err 重定向到 slf4j

java - 如何添加锁以从文件方法读取和写入

mysql - 在 CakePHP 中使用事件-> 计划-> 开始和结束日期之间的日期选择所有事件

apache-spark - 如何在 Spark Streaming 中删除执行者的日志