Java 模拟浏览器日志

本文档介绍了如何使用Java来模拟浏览器日志,包括在`pom.xml`中配置依赖,定义`BrowserInfo`和`Event`模型,实现`LogService`服务,以及使用`StoreUtil`工具类进行日志存储。

pom.xml

 

dependencies

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.9</version>
    </dependency>
</dependencies>

 

build

<build>
    <finalName>mylog</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>com.kgc.mylog.App</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
</build>

 

一、Model

 

BrowserInfo

/**
 * @ClassName BrowserInfo
 * @Description 日志模型
 * @Author SuSu
 * @Date 2020/5/28 21:52
 */
public class LogInfo {

    // 浏览器
    private String browser;
    // 客户
    private String user;
    // 事件
    private Event event;

    public LogInfo() {
    }

    public LogInfo(String browser, String user, Event event) {
        this.browser = browser;
        this.user = user;
        this.event = event;
    }

    public String getBrowser() {
        return browser;
    }

    public void setBrowser(String browser) {
        this.browser = browser;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

}

 

Event

/**
 * @ClassName Event
 * @Description 事件模型
 * @Author SuSu
 * @Date 2020/5/28 22:02
 */
public class Event {

    // 事件类型
    private String type;
    // 事件时间
    private String time;
    // 页面地址
    private String url;
    // 点击位置
    private String pos;
    // 消息内容
    private String msg;

    public Event() {
    }

    public Event(String type, String time, String url, String pos, String msg) {
        this.type = type;
        this.time = time;
        this.url = url;
        this.pos = pos;
        this.msg = msg;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getPos() {
        return pos;
    }

    public void setPos(String pos) {
        this.pos = pos;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

 

二、Services

 

LogService

/**
 * @ClassName LogService
 * @Description 日志生成业务
 * @Author SuSu
 * @Date 2020/5/28 22:18
 */
public class LogService extends Thread {

    private StoreUtil su;

    public LogService(String name, int level, StoreUtil store) {
        // 设置线程名
        setName(name);
        // 设置线程优先级
        setPriority(level);
        // 设置存储数据工具对象
        this.su = store;
    }

    @Override
    public void run() {
        // 获取当前线程名称
        String name = Thread.currentThread().getName();
        // 定义生成日志数量
        int count = 10000;
        // 开始干活写入日志
        for (int i = 0; i < count; i++) {
            su.writeLog(name);
        }
    }

}

 

三、Utility

 

StoreUtil

/**
 * @ClassName StoreUtil
 * @Description 存储数据工具类
 * @Author SuSu
 * @Date 2020/5/28 22:22
 */
public class StoreUtil {

    // 日期
    private static Date date;
    // 格式化日期
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    // 日历
    private static Calendar cal = Calendar.getInstance();
    // 用户id
    private static long userid = 10000;
    // 对象映射
    private static ObjectMapper om = new ObjectMapper();

    // 静态块加载时,模拟时间
    static {
        // 指定日历对象的时间为:2020/5/20 13:14:00
        cal.set(2020, 4, 20, 13, 14, 0);
        // 获取日历对象的时间值
        date = cal.getTime();
    }

    // 模拟数据
    private final String[] BROWSERS = {"chrome", "firefox", "edge"};
    private final String[] EVENT_TYPES = {"btn_click", "href_click", "txt_input"};
    private final String[] PAGES = {"index.html", "login.html", "register.html"};
    private final String[] MSGS = {"hello", "world", "hello world"};
    // 随机数
    private Random rand = new Random();

    /**
     * 产生日志(模拟浏览器向服务器发送一条日志)
     *
     * @return 日志数据
     */
    private String makeLog() {
        // 生成随机事件
        Event event = new Event(EVENT_TYPES[rand.nextInt(3)],
                sdf.format(date),
                "http://localhost:8080/mylog/" + PAGES[rand.nextInt(3)],
                rand.nextInt(1920) + " " + rand.nextInt(1080),
                MSGS[rand.nextInt(3)]);
        // 模拟一条日志
        LogInfo log = new LogInfo(BROWSERS[rand.nextInt(3)], userid + "", event);

        // 该条日志模拟后,再次随机化数据,为下一条日志做准备
        // 时间自动向前加 1 分钟
        cal.add(Calendar.MINUTE, 1);
        date = cal.getTime();
        // 再随机生成一个在 10000~99999 之间的 用户id
        userid = 10000 + rand.nextInt(90000);

        // 模拟浏览器向服务器发送的日志数据,需要转换为 JSON 格式
        String res = null;
        try {
            // 将日志对象转换为 JSON 字符串
            res = om.writeValueAsString(log);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 保存日志(模拟服务器向硬盘写入一条日志)
     */
    public void writeLog(String logType) {
        String log = makeLog();
        String path = "/opt/mylog.log";
        try {
            // 创建一个随机存储文件流
            // 文件对象由 path 指定
            // 文件操作模式指定为 rw(以读、写方式打开,支持文件的读取或写入。若文件不存在,则创建之。)
            RandomAccessFile raf = new RandomAccessFile(path, "rw");
            // 返回文件当前文本长度
            long size = raf.length();
            // 定位光标至文本末尾
            raf.seek(size);
            // 写入一条日志并换行
            raf.writeBytes(log + "\r\n");
            // 关闭流
            raf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值