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
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
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
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
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();
private static long userid = 10000;
private static ObjectMapper om = new ObjectMapper();
static {
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();
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);
cal.add(Calendar.MINUTE, 1);
date = cal.getTime();
userid = 10000 + rand.nextInt(90000);
String res = null;
try {
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 {
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();
}
}
}