Skip to content

Commit 74dca6e

Browse files
committed
2022.6-Client(add err print when logging error or fatal)
1 parent a840c96 commit 74dca6e

40 files changed

+63
-105
lines changed
Binary file not shown.
Binary file not shown.

.gradle/7.4/fileHashes/fileHashes.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
1.73 KB
Binary file not shown.
Binary file not shown.

.gradle/file-system.probe

0 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.
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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

build/resources/test/logs/2022-06-03 19-36-40Log.log

Lines changed: 0 additions & 20 deletions
This file was deleted.
Binary file not shown.

build/resources/test/logs/2022-06-03 19-37-16Log.log

Lines changed: 0 additions & 19 deletions
This file was deleted.
-12 Bytes
Binary file not shown.
Binary file not shown.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.optilog.log.console.Console;
55
import com.optilog.setting.SettingFiles;
66
import com.optilog.util.OnlyInInit;
7-
import com.optilog.util.Util;
87
import com.optilog.util.exception.OptilogException;
98

109
import java.io.IOException;
@@ -27,7 +26,7 @@ public static void initLog(String settingFilePath, Optilog instance) {
2726
Client.initAppender(instance);
2827
}
2928
} catch (RuntimeException | IOException e) {
30-
Util.getOutput().println("Optilog Note:An Exception was thrown when Optilog init logger");
29+
System.err.println("Optilog Note:An Exception was thrown when Optilog init logger");
3130
throw new OptilogException("An Exception was thrown when Optilog init logger", e);
3231
}
3332
}

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

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,40 @@
88
import java.util.regex.Matcher;
99

1010
public class Packing {
11-
private Packing() {
12-
super();
13-
}
14-
15-
@OnlyInLog
16-
public static String packMessage(String msg, String level, Optilog instance) {
17-
StackTraceElement[] arr = Thread.currentThread().getStackTrace();
18-
String returnString = instance.allSetting.packingFormat;
19-
try {
20-
returnString = returnString.replaceAll("%thread", Matcher.quoteReplacement(getLocalThread()));
21-
22-
returnString = returnString.replaceAll("%yyyy", DateTimeFormatter.ofPattern("yyyy").format(LocalDateTime.now()));
23-
returnString = returnString.replaceAll("%MM", DateTimeFormatter.ofPattern("MM").format(LocalDateTime.now()));
24-
returnString = returnString.replaceAll("%dd", DateTimeFormatter.ofPattern("dd").format(LocalDateTime.now()));
25-
returnString = returnString.replaceAll("%HH", DateTimeFormatter.ofPattern("HH").format(LocalDateTime.now()));
26-
returnString = returnString.replaceAll("%mm", DateTimeFormatter.ofPattern("mm").format(LocalDateTime.now()));
27-
returnString = returnString.replaceAll("%ss", DateTimeFormatter.ofPattern("ss").format(LocalDateTime.now()));
28-
returnString = returnString.replaceAll("%SS", DateTimeFormatter.ofPattern("SS").format(LocalDateTime.now()));
29-
30-
returnString = returnString.replaceAll("%level", level);
31-
32-
returnString = returnString.replaceAll("%class", Matcher.quoteReplacement(arr[5].getClassName()));
33-
returnString = returnString.replaceAll("%line", String.valueOf(arr[5].getLineNumber()));
34-
returnString = returnString.replaceAll("%file", Matcher.quoteReplacement(Objects.requireNonNull(arr[5].getFileName())));
35-
returnString = returnString.replaceAll("%msg", Matcher.quoteReplacement(msg));
36-
returnString = returnString.replaceAll("%method", Matcher.quoteReplacement(arr[5].getMethodName()));
37-
} catch (NullPointerException i) {
38-
i.getStackTrace();
39-
}
40-
return returnString + "\n";
41-
}
42-
43-
@OnlyInLog
44-
private static String getLocalThread() {
45-
return Thread.currentThread().getName();
46-
}
11+
private Packing() {
12+
super();
13+
}
14+
15+
@OnlyInLog
16+
public static String packMessage(String msg, String level, Optilog instance) {
17+
StackTraceElement[] arr = Thread.currentThread().getStackTrace();
18+
String returnString = instance.allSetting.packingFormat;
19+
try {
20+
returnString = returnString.replaceAll("%thread", Matcher.quoteReplacement(getLocalThread()));
21+
22+
returnString = returnString.replaceAll("%yyyy", DateTimeFormatter.ofPattern("yyyy").format(LocalDateTime.now()));
23+
returnString = returnString.replaceAll("%MM", DateTimeFormatter.ofPattern("MM").format(LocalDateTime.now()));
24+
returnString = returnString.replaceAll("%dd", DateTimeFormatter.ofPattern("dd").format(LocalDateTime.now()));
25+
returnString = returnString.replaceAll("%HH", DateTimeFormatter.ofPattern("HH").format(LocalDateTime.now()));
26+
returnString = returnString.replaceAll("%mm", DateTimeFormatter.ofPattern("mm").format(LocalDateTime.now()));
27+
returnString = returnString.replaceAll("%ss", DateTimeFormatter.ofPattern("ss").format(LocalDateTime.now()));
28+
returnString = returnString.replaceAll("%SS", DateTimeFormatter.ofPattern("SS").format(LocalDateTime.now()));
29+
30+
returnString = returnString.replaceAll("%level", level);
31+
32+
returnString = returnString.replaceAll("%class", Matcher.quoteReplacement(arr[5].getClassName()));
33+
returnString = returnString.replaceAll("%line", String.valueOf(arr[5].getLineNumber()));
34+
returnString = returnString.replaceAll("%file", Matcher.quoteReplacement(Objects.requireNonNull(arr[5].getFileName())));
35+
returnString = returnString.replaceAll("%msg", Matcher.quoteReplacement(msg));
36+
returnString = returnString.replaceAll("%method", Matcher.quoteReplacement(arr[5].getMethodName()));
37+
} catch (NullPointerException i) {
38+
i.getStackTrace();
39+
}
40+
return returnString + "\n";
41+
}
42+
43+
@OnlyInLog
44+
private static String getLocalThread() {
45+
return Thread.currentThread().getName();
46+
}
4747
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ private Send() {
1818
@OnlyInLog
1919
void loggerPrint(LogEvent le, Optilog instance) {
2020
synchronized (Send.INSTANCE) {
21+
if (le.level.equals(Level.ERROR) || le.level.equals(Level.FATAL)) {
22+
System.err.print(Packing.packMessage(le.message, le.level.getName(), instance));
23+
}
2124
Util.getOutput().print(Packing.packMessage(le.message, le.level.getName(), instance));
2225
}
2326
}
@@ -28,7 +31,6 @@ void loggerConsole(LogEvent le, Optilog instance) {
2831
try {
2932
new Thread(() -> {
3033
if (instance.consoleFileMasterCaution & Level.INFO.getName().equals(le.level.getName()) & !instance.info.isBlank()) {
31-
//String s = Packing.packMessage(message, level, instance);
3234
try {
3335
synchronized (Send.INSTANCE) {
3436
Files.writeString(Path.of(instance.info), Files.readString(Path.of(instance.info), StandardCharsets.UTF_8) + s, StandardCharsets.UTF_8);

src/main/java/com/optilog/log/client/Client.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void initAppender(Optilog instance) {
1616
instance.socket.setSoTimeout(1000);
1717
instance.socket.connect(InetAddress.getByName(instance.allSetting.host), instance.allSetting.socketNumber);
1818
} catch (UnknownHostException | SocketException exception) {
19-
System.out.println("Optilog Note: Exception in init client.");
19+
System.err.println("Optilog Note: Exception in init client.");
2020
exception.printStackTrace();
2121
}
2222
}

src/main/java/com/optilog/log/console/Console.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.optilog.log.Optilog;
44
import com.optilog.util.OnlyInInit;
5-
import com.optilog.util.Util;
65

76
import java.io.File;
87
import java.io.IOException;
@@ -40,11 +39,11 @@ public static void initAppender(Optilog instance) {
4039
instance.fatal = defFile.getAbsolutePath();
4140
}
4241
} catch (IOException e) {
43-
Util.getOutput().println("Optilog Note: IOException in init default path log file!");
42+
System.err.println("Optilog Note: IOException in init default path log file!");
4443
instance.consoleFileMasterCaution = false;
4544
}
4645
} else {
47-
Util.getOutput().println("Optilog Note: File check is not correct or this console is stopped");
46+
System.err.println("Optilog Note: ConsolePath maybe not correct or not exists");
4847
instance.consoleFileMasterCaution = false;
4948
return;
5049
}
@@ -134,12 +133,12 @@ public static void initAppender(Optilog instance) {
134133
instance.info = instance.allSetting.Path5;
135134
}
136135
if (instance.info.equals("")) {
137-
Util.getOutput().println("Optilog Note: Unexpected error occur when parse infoPath");
136+
System.err.println("Optilog Note: Unexpected error occur when parse infoPath");
138137
instance.allSetting.consoleInfo = false;
139138
}
140139
} catch (NullPointerException e) {
141140
instance.consoleFileMasterCaution = false;
142-
Util.getOutput().println("Optilog Note: Unexpected error occur when parse infoPath");
141+
System.err.println("Optilog Note: Unexpected error occur when parse infoPath");
143142
e.printStackTrace();
144143
}
145144
}
@@ -162,11 +161,11 @@ public static void initAppender(Optilog instance) {
162161
}
163162
if (instance.error.equals("")) {
164163
instance.allSetting.consoleError = false;
165-
Util.getOutput().println("Optilog Note: Unexpected error occur when parse errorPath");
164+
System.err.println("Optilog Note: Unexpected error occur when parse errorPath");
166165
}
167166
} catch (NullPointerException e) {
168167
instance.consoleFileMasterCaution = false;
169-
Util.getOutput().println("Optilog Note: Unexpected error occur when parse errorPath");
168+
System.err.println("Optilog Note: Unexpected error occur when parse errorPath");
170169
e.printStackTrace();
171170
}
172171
}
@@ -189,11 +188,11 @@ public static void initAppender(Optilog instance) {
189188
}
190189
if (instance.warn.equals("")) {
191190
instance.allSetting.consoleWarn = false;
192-
Util.getOutput().println("Optilog Note: Unexpected error occur when parse warnPath");
191+
System.err.println("Optilog Note: Unexpected error occur when parse warnPath");
193192
}
194193
} catch (NullPointerException e) {
195194
instance.consoleFileMasterCaution = false;
196-
Util.getOutput().println("Optilog Note: Unexpected error occur when parse warnPath");
195+
System.err.println("Optilog Note: Unexpected error occur when parse warnPath");
197196
}
198197
}
199198
if (instance.allSetting.debugPath.startsWith("%path") & instance.consoleFileMasterCaution) {
@@ -215,11 +214,11 @@ public static void initAppender(Optilog instance) {
215214
}
216215
if (instance.debug.equals("")) {
217216
instance.allSetting.consoleDebug = false;
218-
Util.getOutput().println("Optilog Note: Unexpected error occur when parse debugPath");
217+
System.err.println("Optilog Note: Unexpected error occur when parse debugPath");
219218
}
220219
} catch (NullPointerException e) {
221220
instance.consoleFileMasterCaution = false;
222-
Util.getOutput().println("Optilog Note: Unexpected error occur when parse debugPath");
221+
System.err.println("Optilog Note: Unexpected error occur when parse debugPath");
223222
}
224223
}
225224
if (instance.allSetting.fatalPath.startsWith("%path") & instance.consoleFileMasterCaution) {
@@ -241,11 +240,11 @@ public static void initAppender(Optilog instance) {
241240
}
242241
if (instance.fatal.equals("")) {
243242
instance.allSetting.consoleFatal = false;
244-
Util.getOutput().println("Optilog Note: Unexpected error occur when parse fatalPath");
243+
System.err.println("Optilog Note: Unexpected error occur when parse fatalPath");
245244
}
246245
} catch (NullPointerException e) {
247246
instance.consoleFileMasterCaution = false;
248-
Util.getOutput().println("Optilog Note: Unexpected error occur when parse fatalPath");
247+
System.err.println("Optilog Note: Unexpected error occur when parse fatalPath");
249248
}
250249
}
251250
}

src/main/java/com/optilog/log/console/ZipLog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static void zipAllLog(boolean delete, Optilog instance, String path) {
2121
throw new IOException("Create file failed.");
2222
}
2323
} catch (IOException e) {
24-
instance.warn("Optilog Note: Failed to create log package zip.");
24+
instance.warn("Optilog Note: Failed to create log package zip.", e);
2525
return;
2626
}
2727

@@ -35,7 +35,7 @@ public static void zipAllLog(boolean delete, Optilog instance, String path) {
3535
output.closeEntry();
3636
if (delete) {
3737
if (!fff.delete()) {
38-
System.out.println("Delete file failed.");
38+
System.err.println("Delete file failed.");
3939
}
4040
}
4141
}

src/main/java/com/optilog/setting/PropSettings.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.optilog.log.Optilog;
44
import com.optilog.util.OnlyInInit;
5-
import com.optilog.util.Util;
65

76
import java.io.IOException;
87
import java.io.InputStream;
@@ -55,7 +54,7 @@ static void properties(InputStream content, Optilog instance) {
5554

5655
instance.allSetting.fileName = p.getProperty("fileName", "%time Log(Client).log");
5756
} catch (IOException e) {
58-
Util.getOutput().println("Optilog Note:Read file failed.");
57+
System.err.println("Optilog Note:Read file failed.");
5958
e.printStackTrace();
6059
}
6160
}

src/main/java/com/optilog/setting/SettingFiles.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.google.gson.Gson;
44
import com.optilog.log.Optilog;
55
import com.optilog.util.OnlyInInit;
6-
import com.optilog.util.Util;
76
import com.optilog.util.exception.ConfigureException;
87
import com.optilog.util.exception.GsonNotFoundException;
98

@@ -58,7 +57,7 @@ public static void check(String str, Optilog instance) throws IOException {
5857
Class.forName("com.google.gson.Gson");
5958
} catch (ClassNotFoundException e) {
6059
if (!str.isBlank()) {
61-
Util.getOutput().println("Can't find Gson in classpath");
60+
System.err.println("Can't find Gson in classpath");
6261
instance.consoleFileMasterCaution = false;
6362
throw new GsonNotFoundException("Can't found Gson in classpath", new ClassNotFoundException("Class:com.google.gson.Gson not found"));
6463
}
@@ -67,7 +66,7 @@ public static void check(String str, Optilog instance) throws IOException {
6766
Class.forName("com.fasterxml.jackson.dataformat.xml.JacksonXmlModule");
6867
} catch (ClassNotFoundException e) {
6968
if (!str.isBlank()) {
70-
Util.getOutput().println("Can't find jackson in classpath");
69+
System.err.println("Can't find jackson in classpath");
7170
instance.consoleFileMasterCaution = false;
7271
throw new GsonNotFoundException("Can't found jackson in classpath", new ClassNotFoundException("Class:com.fasterxml.jackson.dataformat.xml.JacksonXmlModule not found"));
7372
}
@@ -86,7 +85,7 @@ public static void check(String str, Optilog instance) throws IOException {
8685
try (InputStream input = Optilog.class.getResourceAsStream(s)) {
8786
if (input == null) {
8887
instance.consoleFileMasterCaution = false;
89-
Util.getOutput().println("Optilog Note: Can't find'" + s + "'in classpath.");
88+
System.err.println("Optilog Note: Can't find'" + s + "'in classpath.");
9089
throw new ConfigureException("Can't find'" + s + "'in classpath.");
9190
}
9291
PropSettings.properties(input, instance);
@@ -112,7 +111,7 @@ private static void getSetting(String s, Optilog instance, boolean isClasspath)
112111
try (InputStream input = Optilog.class.getResourceAsStream(s.substring(4))) {
113112
if (input == null) {
114113
instance.consoleFileMasterCaution = false;
115-
Util.getOutput().println("Optilog Note: Can't find'" + s.substring(4) + "'in classpath.");
114+
System.err.println("Optilog Note: Can't find'" + s.substring(4) + "'in classpath.");
116115
throw new ConfigureException("Can't find'" + s.substring(4) + "'in classpath.");
117116
}
118117
content = readAsString(input);

src/main/java/com/optilog/setting/XmlSettings.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
55
import com.optilog.log.Optilog;
66
import com.optilog.util.OnlyInInit;
7-
import com.optilog.util.Util;
87

98
import java.io.FileInputStream;
109
import java.io.IOException;
@@ -17,7 +16,7 @@ static void xml(String path, boolean isClasspath, Optilog instance) {
1716
try (InputStream input = Optilog.class.getResourceAsStream(path)) {
1817
if (input == null) {
1918
instance.consoleFileMasterCaution = false;
20-
Util.getOutput().println("Optilog Note: Can't find " + path + "in classpath.");
19+
System.err.println("Optilog Note: Can't find " + path + "in classpath.");
2120
return;
2221
}
2322
Settings object = new XmlMapper(new JacksonXmlModule()).readValue(input, Settings.class);
@@ -90,7 +89,7 @@ static void xml(String path, boolean isClasspath, Optilog instance) {
9089
instance.allSetting.packingFormat = object.packingFormat.trim();
9190
}
9291
} catch (IOException e) {
93-
Util.getOutput().println("Optilog Note: Failed to read xml setting file!");
92+
System.err.println("Optilog Note: Failed to read xml setting file!");
9493
e.printStackTrace();
9594
}
9695
} else {
@@ -164,7 +163,7 @@ static void xml(String path, boolean isClasspath, Optilog instance) {
164163
instance.allSetting.packingFormat = object.packingFormat.trim();
165164
}
166165
} catch (IOException e) {
167-
Util.getOutput().println("Optilog Note: Failed to read xml setting file!");
166+
System.err.println("Optilog Note: Failed to read xml setting file!");
168167
e.printStackTrace();
169168
}
170169
}
Binary file not shown.

0 commit comments

Comments
 (0)