Java 模擬瀏覽器日誌

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();
        }
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章