一. 日志格式需求
1.日志格式:
- IP,UID,Time,URL,Referrer(访问来源),status code(状态码)
- 其中URL有多种,弹幕的日志格式为
- POST aa/pp HTTP 1.1
- GET dm/send?line=哈哈哈哈哈 HTTP 1.1
2.示例
58.58.128.128 - - UID000161 [2019-12-01 20:07:41 +0800]"GET category/send?line=666,好厉害,sb HTTP 1.1" 搜狗 500
217.58.128.53 - - UID000731 [2019-12-01 20:07:41 +0800]"GET other/send?line=好厉害,666,666 HTTP 1.1" 360 500
94.53.58.247 - - UID000395 [2019-12-01 20:07:41 +0800]"GET dm/send?line=废物,nb HTTP 1.1" 火狐 200
29.127.29.230 - - UID000127 [2019-12-01 20:07:41 +0800]"GET dm/send?line=666,666 HTTP 1.1" google 200
11.247.9.224 - - UID000501 [2019-12-01 20:07:41 +0800]"GET other/send?line=666,主播好厉害啊 HTTP 1.1" 360 200
247.53.235.127 - - UID000299 [2019-12-01 20:07:41 +0800]"GET category/send?line=666,废物 HTTP 1.1" google 200
53.58.229.58 - - UID000785 [2019-12-01 20:07:41 +0800]"POST other/666,666,666 HTTP 1.1" 360 200
127.36.11.29 - - UID000814 [2019-12-01 20:07:41 +0800]"POST category/666,666 HTTP 1.1" 腾讯 200
二. 代码
package com.wangt.create.log;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
@SuppressWarnings("all")
public class CreateLogsUtils {
private static int[] ips = new int[20];
private static Random random = new Random();
private static SimpleDateFormat simpleDateFormat = null;
private static String[] referrers = new String[]{
"google", "腾讯", "360", "搜狗", "火狐", "IE"
};
private static String[] statusCode = new String[]{
"200","200","200","200","404","500","200","200","200"
};
private static String[] requestType = new String[]{"POST", "GET"};
private static String[] requestMehtod = new String[]{"other","dm","category"};
private static String[] words = new String[]{
"666" , "nb", "好厉害", "sb", "废物", "主播好厉害啊", "666","666","666","666","666"
};
static{
for (int i = 0; i < ips.length; i++) {
ips[i] = randomCreateRangeInt(0,255);
}
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
public static String createLog(){
String log = getIp() + " - - " + getUID() + " "
+ getTime() + getURL() + " " + getReferrer() + " " + getStatusCode();
return log;
}
public static String createBarrage(){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < randomCreateRangeInt(1, 5); i++) {
sb.append(words[randomCreateRangeInt(0,words.length)]);
sb.append(",");
}
sb.deleteCharAt(sb.length() -1);
return sb.toString();
}
public static int randomCreateRangeInt(int start, int end){
return (int) (random.nextDouble() * (end - start)) + start;
}
public static String fillZero(Object obj, int length){
String str = obj.toString();
return fillElem("0", str, length);
}
public static String fillElem(String elem , String str, int length){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length - str.length(); i++) {
sb.append(elem);
}
sb.append(str);
return sb.toString();
}
public static String getIp(){
StringBuilder ip = new StringBuilder();
for (int i = 0; i < 4; i++) {
ip.append(ips[randomCreateRangeInt(0,20)]);
if(i < 3){
ip.append(".");
}
}
return ip.toString();
}
public static String getUID(){
Integer id = randomCreateRangeInt(1, 1000);
return "UID" + fillZero(id, 6);
}
public static String getTime(){
Date date = new Date();
return "[" + simpleDateFormat.format(date) + " +0800" +"]";
}
public static String getURL(){
String reqType = requestType[randomCreateRangeInt(0, requestType.length)];
String reqMethod = requestMehtod[randomCreateRangeInt(0, requestMehtod.length)];
String barrage = createBarrage();
StringBuilder url = new StringBuilder();
switch (reqType){
case "POST" :
url.append("\"POST " + reqMethod + "/" + barrage + " HTTP 1.1\"");
break;
case "GET" :
url.append("\"GET " + reqMethod + "/send?line=" + barrage + " HTTP 1.1\"");
break;
}
return url.toString();
}
public static String getReferrer(){
return referrers[randomCreateRangeInt(0,referrers.length)];
}
public static String getStatusCode(){
return statusCode[randomCreateRangeInt(0,statusCode.length)];
}
}