分析项目日志的小工具

当项目日志太过庞大时,就需要对大量日志内容进行筛选由用的日志来分析排查问题,针对这个需求做了一个日志分析小工具

配置文件

application.properties

#选择开关 flag=true读取分析过车日志,flag=false读取分析违法日志
flag=true

#服务器ip
linux.ip=
#服务器用户名
linux.username=root
#服务器密码
linux.password=

#过车日志命令 #筛选日志
#cmd=tail -10000 ../iflytek/server/logs/jtcn-upload-vio.log | grep -E '上传失败|<message>'

command=tail -10000 ../iflytek/server/logs/jtcn-upload-wxs.log | grep -i 'error'

command1=tail -10000 ../iflytek/server/logs/jtcn-upload-wxs.log | grep  '方向类型'

#违法日志命令 #筛选日志
#cmd=tail -10000 ../iflytek/server/logs/jtcn-upload-vio.log | grep -E '上传失败|<message>'
cmd=tail -10000 ../iflytek/server/logs/jtcn-upload-vio.log | grep -i '<message>'

  

代码

package com.iflytek.jtcn.service.impl;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import com.alibaba.fastjson.JSONObject;
import com.iflytek.jtcn.conf.Config;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.client.Client;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.*;
import static javax.swing.JOptionPane.showMessageDialog;

public class RemoteExecuteCommand extends JFrame implements ActionListener {

    //字符编码默认是utf-8
    private static String DEFAULTCHART = "UTF-8";
    private static String command1;
    private Connection conn;
    private String ip;
    private String userName;
    private String password;
    private String command;
    


    JTextArea txt1 = new JTextArea(7, 35);///创建文本区对象,要确定文本区的大小

    public void configure(Config config) {
        this.ip = config.linuxIp;
        this.userName = config.linuxUsername;
        this.password = config.linuxPassword;
        this.command = config.command;
        System.out.println("command:" + command);
    }

    public RemoteExecuteCommand() {

    }

    public RemoteExecuteCommand(String ip, String userName, String password) {
        this.ip = ip;
        this.userName = userName;
        this.password = password;
    }


    /**
     * 远程登录linux的主机
     *
     * @return 登录成功返回true,否则返回false
     * @author Ickes
     * @since V0.1
     */
    public Boolean login() {
        boolean flg = false;
        try {
            conn = new Connection(ip);
            conn.connect();//连接
            flg = conn.authenticateWithPassword(userName, password);//认证
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flg;
    }

    /**
     * @param
     * @return 命令执行完后返回的结果值
     * @author Ickes
     * 远程执行shll脚本或者命令
     * @since V0.1
     */
    public String execute(String cmd) {
        String result = "";
        try {
            if (login()) {
                Session session = conn.openSession();//打开一个会话
                session.execCommand(cmd);//执行命令
                result = processStdout(session.getStdout(), DEFAULTCHART);
                System.out.println("cmd:" + cmd);
                System.out.println("result:" + result);
                System.out.println("------------------------------------------------");
                //如果为得到标准输出为空,说明脚本执行出错了
                if (StringUtils.isBlank(result)) {
                    result = processStdout(session.getStderr(), DEFAULTCHART);
                }
                conn.close();
                session.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 解析脚本执行返回的结果集
     *
     * @param in      输入流对象
     * @param charset 编码
     * @return 以纯文本的格式返回
     * @author Ickes
     * @since V0.1
     */
    private String processStdout(InputStream in, String charset) {
        InputStream stdout = new StreamGobbler(in);
        StringBuffer buffer = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
            String line = null;
            ArrayList list = new ArrayList();
            while ((line = br.readLine()) != null) {
                for (int i = 0; i < line.length(); i++) {
                    String ss = String.valueOf(line.charAt(i));
                    if (ss.equals("{")) {
                        String jsonStr = null;
                        int one = line.lastIndexOf("{");
                        System.out.println("one:" + one);
                        String Suffix = line.substring(one, line.length() - 1);
                        System.out.println("Suffix:" + Suffix);
                        jsonStr = Suffix + "}";
                        System.out.println("jsonStr:" + jsonStr);
                        //JsonObject recordsJson = new JsonParser().parse(Suffix).getAsJsonObject();
                        JSONObject recordsJson = JSONObject.parseObject(jsonStr);
                        System.out.println("recordsJson:" + recordsJson);
                        Object kkbh = recordsJson.get("kkbh");
                        String kkbh1 = kkbh.toString().replace(""", "");

                        list.add(kkbh1);
                        continue;
                    }
                }
                buffer.append(line + "
");
            }

            //卡口编号去重
            ArrayList duplicateList = new ArrayList();
            for (Object s : list) {
                if (!duplicateList.contains(s)) {
                    duplicateList.add(s);
                }
            }

            Object[][] data = new Object[duplicateList.size()][1];
            for (int p = 0; p < duplicateList.size(); p++) {
                data[p][0] = duplicateList.get(p);
                // System.out.println("data[p][0]:" + data[p][0]);
            }
            CreateJTable(data);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }


    public void CreateJTable(Object[][] data) {
        String[] columnName = {
                "卡口编号"
        };
        JTable jTable = new JTable(data, columnName);
        jTable.getColumn("卡口编号").setMinWidth(465);
        jTable.setRowHeight(50);// 设置表格行宽
        jTable.setSize(100, 50);
        jTable.setFont(new Font("Menu.font", Font.PLAIN, 20));
        jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        jTable.setBounds(100, 100, 300, 200);    //设置窗体位置和大小
        JScrollPane jScrollPane = new JScrollPane();
        jScrollPane.setViewportView(jTable);
        JFrame container = new JFrame("日志窗口");
        container.setSize(1000, 800);
        container.setLayout(new BorderLayout());
        container.add(jScrollPane, BorderLayout.WEST);
        validate();///设置窗体中的组件可显
        container.setVisible(true);
        jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);


        //给table加上一个鼠标事件监听器对象//仅当鼠标单击时响应
        Object[] value = {null};
        jTable.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                String logs = "null";
                //得到选中的行列的索引值
                int r = jTable.getSelectedRow();
                int c = jTable.getSelectedColumn();
                //得到选中的单元格的值,表格中都是字符串
                value[0] = jTable.getValueAt(r, c);
                // String info = r + "行" + c + "列值 : " + value[0].toString();
                String info = "卡口编号 : " + value[0].toString();
                System.out.println(":" + value[0].toString());
                showMessageDialog(null, info);

                String command2 = command1 + "| grep '" + value[0] + "'";//参数长度
                System.out.println("command:" + command2);
                RemoteExecuteCommand rec = new RemoteExecuteCommand(ip, userName, password);
                logs = rec.executeSuccess(command2);
                System.out.println("---------------------------------------------");
                System.out.println("logs:" + logs);

                System.out.println("---------------------------------------------");
                setLayout(new FlowLayout());///设置浮动布局
                txt1.setText(logs);///将str设置为文本区的内容
                txt1.setFont(new Font("Menu.font", Font.PLAIN, 10));
                container.add(txt1);///将文本区添加到窗体中
                validate();///设置窗体中的组件可显
                container.add(txt1, BorderLayout.CENTER);
            }
        });
    }

    /**
     * @param cmd 即将执行的命令
     * @return 命令执行成功后返回的结果值,如果命令执行失败,返回空字符串,不是null
     * @author Ickes
     * 远程执行shll脚本或者命令
     * @since V0.1
     */
    public String executeSuccess(String cmd) {
        String result = "";
        try {
            if (login()) {
                Session session = conn.openSession();//打开一个会话
                session.execCommand(cmd);//执行命令
                result = processStdoutSuccess(session.getStdout(), DEFAULTCHART);
                conn.close();
                session.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String s = txt1.getText();///获取文本框中的内容
        txt1.append("
" + s);///将文本框中的内容追加的文本区
    }

    public static void main(String[] args) throws Exception {
        Properties prop = new Properties();
        prop.load(new InputStreamReader(Client.class.getClassLoader().getResourceAsStream("application.properties"), "UTF-8"));
        String command = (String) prop.get("command");
        String cmd = (String) prop.get("cmd");
        String ip = (String) prop.get("linux.ip");
        String username = (String) prop.get("linux.username");
        String password = (String) prop.get("linux.password");
        command1 = (String) prop.get("command1");
        String flag = (String) prop.get("flag");
        System.out.println("ip:" + ip + "username:" + username + "password:" + password);
        System.out.println("-------------------------------------------------------------------------------flag" + flag);
        RemoteExecuteCommand rec = new RemoteExecuteCommand(ip, username, password);
        //执行命令
        if (flag.equals("true")) {
            System.out.println("过车日志");
            System.out.println("command:" + command);
            rec.execute(command);
        } else {
            System.out.println("违法日志");
            System.out.println("cmd:" + cmd);
            rec.executeLaw(cmd);
        }


    }


    /**
     * 解析脚本执行返回的结果集
     *
     * @param in      输入流对象
     * @param charset 编码
     * @return 以纯文本的格式返回
     * @author Ickes
     * @since V0.1
     */
    private String processStdoutSuccess(InputStream in, String charset) {
        InputStream stdout = new StreamGobbler(in);
        StringBuffer buffer = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
            String line = null;
            while ((line = br.readLine()) != null) {
                //System.out.println("processStdoutSuccess---line:" + line);
                buffer.append(line + "
");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }


    public static void setCharset(String charset) {
        DEFAULTCHART = charset;
    }

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    /*************************************************************************************************/


    /**
     * @param
     * @return 命令执行完后返回的结果值
     * @author Ickes
     * 远程执行shll脚本或者命令
     * @since V0.1
     */
    public String executeLaw(String cmd) {
        String result = "";
        try {
            if (login()) {
                Session session = conn.openSession();//打开一个会话
                session.execCommand(cmd);//执行命令
                result = processStdoutLaw(session.getStdout(), DEFAULTCHART);
                //如果为得到标准输出为空,说明脚本执行出错了
                if (StringUtils.isBlank(result)) {
                    result = processStdoutLaw(session.getStderr(), DEFAULTCHART);
                }
                conn.close();
                session.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 解析脚本执行返回的结果集
     *
     * @param in      输入流对象
     * @param charset 编码
     * @return 以纯文本的格式返回
     * @author Ickes
     * @since V0.1
     */
    private String processStdoutLaw(InputStream in, String charset) {
        InputStream stdout = new StreamGobbler(in);
        StringBuffer buffer = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
            String line = null;
            ArrayList arrayList = new ArrayList();
            boolean flag = true;
            while ((line = br.readLine()) != null) {
               /* if(arrayList.isEmpty()){
                    buffer.append(line + "
");
                    arrayList.add(line);
                }else {
                    for(Object str:arrayList){
                        if(line.trim().equals(str.toString().trim())){
                            flag = false;
                            continue;
                        }
                    }
                }
                if(flag){
                    buffer.append(line + "
");
                    arrayList.add(line);
                }*/
                buffer.append(line + "
");
            }
            CreateJTextArea(buffer.toString());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }


    public void CreateJTextArea(String buffer) {
        JFrame container = new JFrame("日志窗口");
        container.setSize(1000, 800);

        container.setLayout(new BorderLayout());
        validate();///设置窗体中的组件可显
        container.setVisible(true);
        System.out.println("---------------------------------------------");
        setLayout(new FlowLayout());///设置浮动布局
        txt1.setText(buffer);///将str设置为文本区的内容
        txt1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        txt1.setFont(new Font("Menu.font", Font.PLAIN, 20));
        container.add(txt1);///将文本区添加到窗体中
        txt1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        container.add(txt1, BorderLayout.CENTER);

        JScrollPane jScrollPane = new JScrollPane();
        jScrollPane.setViewportView(txt1);

        container.add(jScrollPane, BorderLayout.CENTER);
        validate();///设置窗体中的组件可显

    }


}

  

原文地址:https://www.cnblogs.com/shenhaha520/p/13603734.html