package com.chenyang.nse.bussiness.tools.net;

import cn.hutool.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import org.graylog2.syslog4j.Syslog;
import org.graylog2.syslog4j.SyslogIF;

public class MySyslogClient {
   private static final String HOST = "192.168.2.99";
   private static final int PORT = 32376;

   public void generate(String msg) {
      SyslogIF syslog = Syslog.getInstance("udp");
      syslog.getConfig().setHost("192.168.2.99");
      syslog.getConfig().setPort(32376);
      StringBuffer buffer = new StringBuffer();
      buffer.append((new Date()).toString().substring(4, 20) + msg + "  ;");
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("message", buffer.toString());

      try {
         syslog.log(0, URLDecoder.decode(jsonObject.toString(), "utf-8"));
      } catch (UnsupportedEncodingException e) {
         System.out.println("generate log get exception " + e);
      }

      System.out.println("发送成功!");
   }

   public static void main(String[] args) {
      String msg = "aaa";
      (new MySyslogClient()).generate(msg);
   }
}
