监听器监听日志,实时读取日志文件,把读取到的数据入库

时间:2021-04-01 08:35:43

1:MyListener.java

import java.io.File;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.szmsd.log.business.ShopeeLogReader;
import com.szmsd.log.util.Const;

/** * @FileName MyListener.java * @Description: 监听器 * * @Date 2017年6月9日 * @author Li Chao * @version 1.0 * */
public class MyListener implements ServletContextListener {
    private static Log log = LogFactory.getLog(MyListener.class);
    private ShopeeLogReader logReader;
    private Const c = new Const();
    @SuppressWarnings("static-access")
    private String logFilePath = c.logFilePath;


    public void contextDestroyed(ServletContextEvent arg0) {
        if (logReader != null && logReader.isInterrupted()) {  
            logReader.interrupt();  
        }  
    }

    public void contextInitialized(ServletContextEvent arg0) {
        log.info("[监控的日志文件路径]:"+logFilePath+"开始====================");
        File logFile = new File(logFilePath);
        if(logReader == null){
            logReader = new ShopeeLogReader(logFile);
            logReader.start();
        }
    }

}

2:ShopeeLogReader.java

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.gson.Gson;
import com.szmsd.log.entity.MSDOrderReq;
import com.szmsd.log.util.Const;
import com.szmsd.log.util.LogReader;

/**
 * @FileName ShopeeLogReader.java
 * @Description: SHOPEE下单接口 实时读取日志文件
 *
 * @Date 201769日
 * @author Li Chao
 * @version 1.0
 * 
 */
public class ShopeeLogReader extends Thread {
    private File logFile = null;
    private long lastTimeFileSize = 0; // 上次文件大小
    private Gson gson = new Gson();
    private static Const c = new Const();
    private static Log log = LogFactory.getLog(LogReader.class);
    private static String monitoringStr = "com.szmsd.action.shopee.OrderAction - J&T SHOPEE标准下单[请求时间]==>";

    public ShopeeLogReader(File logFile) {
        this.logFile = logFile;
        lastTimeFileSize = logFile.length();
    }

    /**
     * 实时读取日志文件
     */
    @SuppressWarnings({ "resource", "static-access" })
    public void run() {
        while (true) {
            try {
                RandomAccessFile randomFile = new RandomAccessFile(logFile, "r");
                randomFile.seek(lastTimeFileSize);
                String tmp = null;
                while ((tmp = randomFile.readLine()) != null) {
                    tmp = new String(tmp.getBytes("ISO-8859-1"), c.charset);
                    shopeeOrderLog(tmp);
                }
                lastTimeFileSize = randomFile.length();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @Title: shopeeOrderLog 
     * @Description: 监控格式=com.szmsd.action.shopee.OrderAction - J&T SHOPEE标准下单[请求时间]==>2017-06-09 11:36:58433[请求参数]==>{"sendstarttime":"2017-05-28 18:00:00","txlogisticid":"SHOPEE-TEST0008","mailno":"JS0000000608","weight":0.5,"ordertype":1,"logisticproviderid":"JNT","servicetype":1,"eccompanyid":"SHOPEE","sendendtime":"2017-05-28 23:00:00","createordertime":"2017-06-01 13:06:48","sender":{"phone":"6285778005338","name":"ivat / Mastur Jamallulail","city":"JAKARTA","mobile":"6285778005338","prov":"DKI JAKARTA","area":"CILINCING","address":"kp bambu kuning jalan kutilang rt 12 rw 02 no 47 Kel marunda","postcode":"000"},"items":[{"itemname":"M","itemsvalue":210000,"number":1}],"customerid":"123456789","goodsvalue":0,"receiver":{"phone":"6282186006003","name":"Rangga Oktariza Armando","city":"PANGKAL PINANG","mobile":"6282186006003","prov":"BANGKA BELITUNG","area":"BUKIT INTAN","address":"Jl Tapak Dewa Asrama Brimob no 01","postcode":"33147"}}[响应结果]==>{"logisticproviderid":"JT","responseitems":[{"txlogisticid":"SHOPEE-TEST0008","mailno":"JS0000000608","success":"true","reason":""}]}[下单用时]==>105
     * @param tmp 
     * @author Li Chao
     * @date 201769日
     */
    public void shopeeOrderLog(String tmp){
        if(StringUtils.isNotEmpty(tmp)){
            if (tmp.indexOf(monitoringStr) != -1) {
                // 请求时间
                String reqBeginDate = tmp.substring(tmp.indexOf("[请求时间]==>") + "[请求时间]==>".length(), tmp.indexOf("[请求参数]==>"));
                // 请求内容
                String logistics_interface = tmp.substring(tmp.indexOf("[请求参数]==>") + "[请求参数]==>".length(), tmp.indexOf("[响应结果]==>"));
                // 请求结果
                String resResult = tmp.substring(tmp.indexOf("[响应结果]==>") + "[响应结果]==>".length(), tmp.indexOf("[下单用时]==>"));
                // 请求用时
                String totalDate = tmp.substring(tmp.indexOf("[下单用时]==>") + "[下单用时]==>".length());

                log.info("reqBeginDate:" + reqBeginDate + "logistics_interface:" + logistics_interface + "resResult:" + resResult + "totalDate:" + totalDate);

                MSDOrderReq orderReq = gson.fromJson(logistics_interface, MSDOrderReq.class);

                String executeSql = "insert into tab_电商日志表(id, 请求时间, 订单号, 运单编号, 请求来源, 日志内容, ip号, 响应时间, 反馈结果, 创建时间)" + "values (S_电商日志表序列.NEXTVAL, '" + reqBeginDate + "', '" + orderReq.getTxlogisticid() + "', '" + orderReq.getMailno() + "', 'SHOPEE', '" + logistics_interface + "', '', '" + totalDate + "', '" + resResult + "',sysdate)";

                log.info("执行的sql语句===>" + executeSql);

                Const.setDBExecuteSQL(executeSql);
            }
        }
    }

}

3:web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>jandt_log_monitor</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>com.szmsd.log.thread.MyListener</listener-class>
  </listener>
</web-app>