Java实现日志数据的采集显示

来源:互联网 发布:全职烽火知韩百度云 编辑:程序博客网 时间:2024/06/06 08:33

1.日志实体类,用户记录用户登录登出

LogRec.java

package q.dms.test;import java.util.Date;/** * 实体类 包含用户登录的id,ip,userName,Long_in,Log_out,address,type *  * @author sky_mg 2017年6月4日下午3:07:44  * TODO 记录用户登录,登出信息记录 */public class LogRec {    // ID标识    private int id;    // 时间    private Date time;    // 地址    private String address;    // 状态    private int type;    // 登录用户名    private String userName;    // 登录IP    private String ip;    /**     * 登录状态 包含LOG_IN,LOG_OUT     */    private int logType;    // LOG_IN    public static final int LOG_IN = 1;    // LOG_OUT    public static final int LOG_OUT = 0;    // 状态常量    public static final int GATHER = 1;    public static final int MATCH = 2;    public static final int RECORD = 3;    public static final int SEND = 4;    public static final int RECEIVE = 5;    public static final int WRITE = 6;    public static final int SAVE = 7;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public Date getTime() {        return time;    }    public void setTime(Date time) {        this.time = time;    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }    public int getType() {        return type;    }    public void setType(int type) {        this.type = type;    }    public String getUser() {        return userName;    }    public void setUser(String user) {        this.userName = user;    }    public String getIp() {        return ip;    }    public void setIp(String ip) {        this.ip = ip;    }    public int getLogType() {        return logType;    }    public void setLogType(int logType) {        this.logType = logType;    }    public LogRec(int id, Date time, String address, int type, String user, String ip, int logType) {        super();        this.id = id;        this.time = time;        this.address = address;        this.type = type;        this.userName = user;        this.ip = ip;        this.logType = logType;    }    public LogRec() {        // TODO Auto-generated constructor stub    }    @Override    public String toString() {        return "LogRec [id=" + id + ", time=" + time + ", address=" + address + ", type=" + type + ", user="     + userName+ ", ip=" + ip + ", logType=" + logType + "]";    }}

2.日志业务类,实现日志数据的信息采集及显示功能

LogRecServe.java

 `package q.dms.test;import java.util.Date;import java.util.Scanner;/** *  * @author sky_mg 2017年6月4日下午3:53:39 TODO 实现日志信息的采集显示功能 */public class LogRecServe {    public LogRec inputLog() {        Scanner scanner = new Scanner(System.in);        // 输入ID标识        System.out.println("请输入用户ID标识");        // 接受键盘输入的整数        int id = scanner.nextInt();        // 获取系统当前时间        Date dateNow = new Date();        // 输入地址        System.out.println("请输入地址");        // 接受键盘输入地址        String address = scanner.next();        // 设置数据状态为采集状态        int type = LogRec.GATHER;        // 用户名输入        System.out.println("请输入用户名");        // 接受用用户名输入        String userName = scanner.next();        // 主机ip输入        System.out.println("请输入主机IP");        // 接受用户输入的主机IP地址        String ip = scanner.next();        // 登录状态输入 :登录状态:1=>登录,0=>登出        System.out.println("请输入登录状态:1=>登录,0=>登出");        // 接受输入的登录状态        int logType = scanner.nextInt();        // 实例化logRec对象        LogRec logRec = new LogRec(id, dateNow, address, type, userName, ip, logType);        return logRec;    }    public void showLog(LogRec... logRecs) {        // forEach循环        for (LogRec e : logRecs) {            if (e != null) {                System.out.println(e.toString());            }        }    }}

3.创建一个日志测试类,演示日志数据的信息采集及显示

TestLogRec.java

package q.dms.test;/** * 1.1 创建日志业务类对象logRecServe 1.2 调用inputLog()进行日志数据采集 1.3 调用showLog()显示采集到的日志数据 *  * @author sky_mg 2017年6月4日下午5:16:10 TODO */public class TestLogRec {    public static void main(String[] args) {        // TODO Auto-generated method stub        // 创建一个记录日志业务对象        LogRecServe logRecServe = new LogRecServe();        // 只有一个对象时可用这种方式创建        // LogRec logRec = new LogRec();        // logRec = logRecServe.inputLog();        // logRecServe.showLog(logRec);        System.out.println("------------------------------------------------");        // 当有多个日志信息时,采用数组方式创建对象        // 创建一个日志对象数组,用于存放采集的日志信息         LogRec[] logRecs = new LogRec[2];         for (int i = 0; i < logRecs.length; i++) {         System.out.println("第" + (i + 1) + "个日志数据采集");         logRecs[i] = logRecServe.inputLog();         }         // 显示日志信息         logRecServe.showLog(logRecs);    }}

资源下载:
csdn资源下载
GitHub资源下载