Skip to content

Commit 0bf7fc3

Browse files
committed
2022.5.3-Client
1 parent 20ec241 commit 0bf7fc3

File tree

23 files changed

+110
-34
lines changed

23 files changed

+110
-34
lines changed
Binary file not shown.
Binary file not shown.

.gradle/7.4/fileHashes/fileHashes.bin

350 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
476 Bytes
Binary file not shown.
Binary file not shown.

.gradle/file-system.probe

0 Bytes
Binary file not shown.

.gradle/workspace-id.txt.lock

0 Bytes
Binary file not shown.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ _截止目前,Optilog代码总量2247行!_
88

99
**Optilog的优势**
1010

11-
1.性能高:初始化日志+输出一条日志(输出到屏幕+输出到文件+通过socket输出到服务端) 最多只需要194毫秒(后续还会再优化)。
11+
1.性能高:初始化日志+输出一条日志(输出到屏幕+输出到文件+通过socket输出到服务端) 最多只需要162毫秒(后续还会再优化),但在你的你不用这个log实例之后要调用log.shutdown()
1212

1313
2.占位符方便:Optilog支持无限个占位符,并且可以重复使用(#1 #1输出两次第一个占位符)
1414

41 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/main/java/com/optilog/log/Log.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,8 @@ static Optilog reInitLog(String pathOfSettingFile) {
170170
void setServerFatal(boolean serverFatal);
171171

172172
void getAllField(Object instance);
173+
174+
void shutdown();
175+
176+
void shutdownNow();
173177
}

src/main/java/com/optilog/log/LogInit.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import com.optilog.util.exception.OptilogException;
1010

1111
import java.io.IOException;
12+
import java.util.concurrent.Executors;
1213

1314
public class LogInit {
1415
@OnlyInInit
1516
public static void initLog(String settingFilePath, Optilog instance) {
17+
instance.logThread = Executors.newCachedThreadPool();
1618
if (settingFilePath.isBlank()) {
1719
instance.consoleFileMasterCaution = false;
1820
instance.allSetting = new SettingFiles();

src/main/java/com/optilog/log/Optilog.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,36 @@
55

66
import java.lang.reflect.Field;
77
import java.net.DatagramSocket;
8+
import java.util.concurrent.ExecutorService;
89
import java.util.regex.Matcher;
910
import java.util.regex.Pattern;
1011

1112
public class Optilog implements Log {
12-
public DatagramSocket socket;
13-
public boolean consoleFileMasterCaution = true;
14-
public boolean alreadyInit;
13+
public volatile ExecutorService logThread;
1514

16-
public SettingFiles allSetting;
15+
public volatile DatagramSocket socket;
16+
public volatile boolean consoleFileMasterCaution = true;
17+
public volatile boolean alreadyInit;
1718

18-
public String settingFilePath = "";
19+
public volatile SettingFiles allSetting;
1920

20-
public String info = "";
21-
public String error = "";
22-
public String warn = "";
23-
public String debug = "";
24-
public String fatal = "";
21+
public volatile String settingFilePath = "";
22+
23+
public volatile String info = "";
24+
public volatile String error = "";
25+
public volatile String warn = "";
26+
public volatile String debug = "";
27+
public volatile String fatal = "";
28+
29+
@Override
30+
public void shutdown() {
31+
this.logThread.shutdown();
32+
}
33+
34+
@Override
35+
public void shutdownNow() {
36+
this.logThread.shutdownNow();
37+
}
2538

2639
@Override
2740
public void info() {

src/main/java/com/optilog/log/Send.java

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,84 @@ void loggerPrint(String level, String message, Optilog instance) {
2121

2222
@OnlyInLog
2323
void loggerConsole(String level, String message, Optilog instance) {
24-
synchronized (Send.INSTANCE) {
25-
try {
26-
if (instance.consoleFileMasterCaution && Level.INFO.getName().equals(level) && !instance.info.isBlank()) {
27-
Files.writeString(Path.of(instance.info), Files.readString(Path.of(instance.info), StandardCharsets.UTF_8) + Packing.packMessage(message, level, instance), StandardCharsets.UTF_8);
28-
}
29-
if (instance.consoleFileMasterCaution && Level.ERROR.getName().equals(level) && !instance.error.isBlank()) {
30-
Files.writeString(Path.of(instance.error), Files.readString(Path.of(instance.error), StandardCharsets.UTF_8) + Packing.packMessage(message, level, instance), StandardCharsets.UTF_8);
31-
}
32-
if (instance.consoleFileMasterCaution && Level.DEBUG.getName().equals(level) && !instance.debug.isBlank()) {
33-
Files.writeString(Path.of(instance.debug), Files.readString(Path.of(instance.debug), StandardCharsets.UTF_8) + Packing.packMessage(message, level, instance), StandardCharsets.UTF_8);
34-
}
35-
if (instance.consoleFileMasterCaution && Level.WARN.getName().equals(level) && !instance.warn.isBlank()) {
36-
Files.writeString(Path.of(instance.warn), Files.readString(Path.of(instance.warn), StandardCharsets.UTF_8) + Packing.packMessage(message, level, instance), StandardCharsets.UTF_8);
37-
}
38-
if (instance.consoleFileMasterCaution && Level.FATAL.getName().equals(level) && !instance.fatal.isBlank()) {
39-
Files.writeString(Path.of(instance.fatal), Files.readString(Path.of(instance.fatal), StandardCharsets.UTF_8) + Packing.packMessage(message, level, instance), StandardCharsets.UTF_8);
40-
}
41-
} catch (RuntimeException | IOException e) {
42-
instance.consoleFileMasterCaution = false;
43-
instance.error("Optilog Note:Java throws Exception when log is output", e);
24+
try {
25+
if (instance.consoleFileMasterCaution && Level.INFO.getName().equals(level) && !instance.info.isBlank()) {
26+
String s = Packing.packMessage(message, level, instance);
27+
instance.logThread.submit(() -> {
28+
try {
29+
synchronized (Send.INSTANCE) {
30+
Files.writeString(Path.of(instance.info), Files.readString(Path.of(instance.info), StandardCharsets.UTF_8) + s, StandardCharsets.UTF_8);
31+
}
32+
} catch (IOException e) {
33+
instance.consoleFileMasterCaution = false;
34+
instance.error("Optilog Note:Java throws Exception when log is output", e);
35+
}
36+
});
37+
}
38+
if (instance.consoleFileMasterCaution && Level.ERROR.getName().equals(level) && !instance.error.isBlank()) {
39+
String s = Packing.packMessage(message, level, instance);
40+
instance.logThread.submit(() -> {
41+
try {
42+
synchronized (Send.INSTANCE) {
43+
Files.writeString(Path.of(instance.error), Files.readString(Path.of(instance.error), StandardCharsets.UTF_8) + s, StandardCharsets.UTF_8);
44+
}
45+
} catch (IOException e) {
46+
instance.consoleFileMasterCaution = false;
47+
instance.error("Optilog Note:Java throws Exception when log is output", e);
48+
}
49+
});
50+
}
51+
if (instance.consoleFileMasterCaution && Level.DEBUG.getName().equals(level) && !instance.debug.isBlank()) {
52+
String s = Packing.packMessage(message, level, instance);
53+
instance.logThread.submit(() -> {
54+
try {
55+
synchronized (Send.INSTANCE) {
56+
Files.writeString(Path.of(instance.debug), Files.readString(Path.of(instance.debug), StandardCharsets.UTF_8) + s, StandardCharsets.UTF_8);
57+
}
58+
} catch (IOException e) {
59+
instance.consoleFileMasterCaution = false;
60+
instance.error("Optilog Note:Java throws Exception when log is output", e);
61+
}
62+
});
63+
}
64+
if (instance.consoleFileMasterCaution && Level.WARN.getName().equals(level) && !instance.warn.isBlank()) {
65+
String s = Packing.packMessage(message, level, instance);
66+
instance.logThread.submit(() -> {
67+
try {
68+
synchronized (Send.INSTANCE) {
69+
Files.writeString(Path.of(instance.warn), Files.readString(Path.of(instance.warn), StandardCharsets.UTF_8) + s, StandardCharsets.UTF_8);
70+
}
71+
} catch (IOException e) {
72+
instance.consoleFileMasterCaution = false;
73+
instance.error("Optilog Note:Java throws Exception when log is output", e);
74+
}
75+
});
76+
}
77+
if (instance.consoleFileMasterCaution && Level.FATAL.getName().equals(level) && !instance.fatal.isBlank()) {
78+
String s = Packing.packMessage(message, level, instance);
79+
instance.logThread.submit(() -> {
80+
try {
81+
synchronized (Send.INSTANCE) {
82+
Files.writeString(Path.of(instance.fatal), Files.readString(Path.of(instance.fatal), StandardCharsets.UTF_8) + s, StandardCharsets.UTF_8);
83+
}
84+
} catch (IOException e) {
85+
instance.consoleFileMasterCaution = false;
86+
instance.error("Optilog Note:Java throws Exception when log is output", e);
87+
}
88+
});
4489
}
90+
} catch (RuntimeException e) {
91+
instance.consoleFileMasterCaution = false;
92+
instance.error("Optilog Note:Java throws Exception when log is output", e);
4593
}
4694
}
4795

4896
@OnlyInLog
49-
void loggerToServer(String level, String message, Optilog instance) {
97+
void loggerToServer(final String level, String message, final Optilog instance) {
5098
synchronized (Send.INSTANCE) {
5199
message = Packing.packMessage(message, level, instance);
52-
Client.send(message + level, instance);
100+
String finalMessage = message;
101+
instance.logThread.submit(() -> Client.send(finalMessage + level, instance));
53102
}
54103
}
55104
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.optilog;
22

3+
import com.optilog.log.Log;
4+
35
public class PerformanceTester {
46
public static void main(String[] args) {
5-
7+
long l = System.currentTimeMillis();
8+
Log log = Log.initLog("%prop -cp /Settings.properties");
9+
log.info("Hello World!");
10+
log.shutdown();
11+
System.out.println(System.currentTimeMillis() - l);
612
}
713
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[2022-05-27|12:40:49(83))][com.optilog.PerformanceTester main(PerformanceTester.java:9)/main] info:Hello World!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[2022-05-27|12:45:22(87))][com.optilog.PerformanceTester main(PerformanceTester.java:9)/main] info:Hello World!

0 commit comments

Comments
 (0)