1. 程式人生 > 實用技巧 >Logstash(一)模擬生成log日誌,純java

Logstash(一)模擬生成log日誌,純java

一、目錄

二、

1.App啟動項

package cn.kgc.kb08.ls;

import cn.kgc.kb08.ls.make.LogService;

public class App {
  public static void main(String[] args) {
    (new LogService("/root/mylog.log")).write(args); // 可以傳入多個引數 1.進入write
  }
}

2.logservice

package cn.kgc.kb08.ls.make;

import cn.kgc.kb08.ls.dao.LogDao;
import
java.text.MessageFormat; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class LogService { private String path; private Random rand = new Random(); private ExecutorService pool = Executors.newFixedThreadPool(50);
private String[] eventName = new String[] { "btn_page", "btn_search", "btn_add_cart", "btn_pay" }; private String[] browser = new String[] { "firefox", "chrome", "ie", "360" }; private String[] timeZone = new String[] { "UTC", "GMT" }; public LogService(String path) { this.path = path; }
private String log(String type) { // 生成一條什麼type的記錄 Object[] time = { String.valueOf(2010 + this.rand.nextInt(11)), String.valueOf(1 + this.rand.nextInt(12)), String.valueOf(1 + this.rand.nextInt(30)) }; String eventTime = MessageFormat.format("{0}-{1}-{2}", time); Object[] info = { Integer.valueOf(1 + this.rand.nextInt(100)), String.valueOf(1 + this.rand.nextInt(2000)), this.eventName[this.rand.nextInt(this.eventName.length)], eventTime, this.browser[this.rand.nextInt(this.browser.length)], this.timeZone[this.rand.nextInt(this.timeZone.length)] }; return type.equals("0") ? MessageFormat.format("userId:{0}|clickCount:{1}|eventName:{2}|clickTime:{3}|browser:{4}|timeZone:{5}", info) : MessageFormat.format("{0}|{1}|{2}|{3}|{4}|{5}", info); } public void write(String[] args) { // 2. 處理type和記錄數量num:生成一條type的記錄並寫入path的檔案中,該操作執行num次數 String type = args[0]; int times = Integer.parseInt(args[1]); for (int i = 0; i < times; i++) this.pool.submit((Runnable)new LogDao(this.path, log(type))); // 先賦值,再寫入一條資料 this.pool.shutdownNow(); } }

3.logDao

package cn.kgc.kb08.ls.dao;

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

public class LogDao implements Runnable {
  private String msg;
  
  private String path;
  
  private static synchronized void info(String path, String msg) { // 將一條記錄新增到path檔案的末尾
    RandomAccessFile rand = null;
    try {
      rand = new RandomAccessFile(path, "rw");
      rand.seek(rand.length());
      rand.writeBytes(msg + "\r\n");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (null != rand)
        try {
          rand.close();
        } catch (IOException e) {
          e.printStackTrace();
        }  
    } 
  }
  
  public LogDao(String msg, String path) { // 先賦值,再執行info()的操作
    this.msg = msg;
    this.path = path;
  }
  
  public void run() {
    info(this.msg, this.path);
  }
}

啟動項是App。傳入兩個引數

結果:/root/下生成 mylog.log

四、使用虛擬日誌作為input,通過logstash這個管道

cd /root/

touch logstash.log

vi logstash.log

input{
// 從mylog.log中拿資料
file{ path => "/root/mylog.log" start_position => "beginning" sincedb_path => "/dev/null" type => "go" } } // 過濾加工log中的資料:使用正則匹配拿到的資料如下:
//userId:32|clickCount:890|eventName:btn_add_cart|clickTime:2018-12-16|browser:ie|timeZone:UTC filter{
if[type] == "go"{ grok{ match => {"message" => "%{NUMBER:userid}\|%{NUMBER:clickCount}\|%{WORD:eventName}\|%{TIMESTAMP_ISO8601:clickTime}\|%{WORD:browser}\|%{WORD:timeZone}"} remove_field =>["message"] } } } // 用go的type來輸出給ES output{ if[type] == "go"{ elasticsearch{ hosts => ["http://192.168.203.132:9200","http://192.168.203.142:9200","http://192.168.203.152:9200"] index => "logstashdb" document_type => "behaviour" } } }

五、啟動logstash

cd到 logstash的bin目錄下,命令如下:

nohup ./logstash -f ~/logstash.log (-f 表示用一個file作為輸入)