Skip to content

Commit 4e860d7

Browse files
committed
update logger
1 parent 3f62aa8 commit 4e860d7

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/main/java/y1rn/javaformat/LogConfig.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,13 @@
88

99
public class LogConfig {
1010

11-
private static boolean loaded;
12-
1311
static {
1412
init();
1513
}
1614

1715
private LogConfig() {}
1816

19-
public static synchronized void init() {
20-
if (loaded) {
21-
return;
22-
}
17+
public static void init() {
2318
LogManager lm = LogManager.getLogManager();
2419
String logLevel = Level.SEVERE.getName();
2520
System.setProperty("java.util.logging.SimpleFormatter.format", "[y1rn.java-format]: %5$s");
@@ -51,7 +46,6 @@ public static synchronized void init() {
5146
} catch (IOException e) {
5247
e.printStackTrace();
5348
}
54-
loaded = true;
5549
}
5650

5751
public static String getValue(String key, String def) {

src/main/java/y1rn/javaformat/RPC.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package y1rn.javaformat;
22

3+
import java.util.Collections;
34
import java.util.List;
45
import java.util.Map;
56
import java.util.concurrent.ExecutorService;
67
import java.util.concurrent.Executors;
8+
import java.util.logging.Level;
79
import lombok.extern.java.Log;
810
import org.eclipse.lsp4j.jsonrpc.StandardLauncher;
911
import org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethod;
@@ -12,13 +14,15 @@
1214
import org.eclipse.lsp4j.jsonrpc.messages.Message;
1315
import org.eclipse.lsp4j.jsonrpc.messages.MessageIssue;
1416
import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage;
17+
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError;
1518
import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage;
1619

1720
@Log
1821
public class RPC {
1922

20-
public static void main(String[] args) {
21-
LogConfig.init();
23+
public static void main(String[] args) throws ClassNotFoundException {
24+
// logger config
25+
Class.forName("y1rn.javaformat.LogConfig");
2226
ExecutorService es = Executors.newCachedThreadPool();
2327
Map<String, JsonRpcMethod> mm =
2428
Map.of(
@@ -34,26 +38,30 @@ public static void main(String[] args) {
3438
System.in,
3539
mjh,
3640
(Message message, List<MessageIssue> issues) -> {
37-
if (!issues.isEmpty()) {
38-
MessageIssue issue = issues.get(0);
39-
ResponseMessage resp = new ResponseMessage();
40-
resp.setId(Integer.parseInt(((RequestMessage) message).getId()));
41-
resp.setResult(
42-
issue.getCause().getMessage()
43-
+ "["
44-
+ issue.getCause().getStackTrace()[0].toString()
45-
+ "]");
46-
fh.writeResponse(resp);
47-
}
48-
49-
log.severe(message.toString());
50-
issues.forEach(
51-
issue -> {
52-
log.severe(issue.getCause().getMessage());
53-
for (StackTraceElement traceElement : issue.getCause().getStackTrace()) {
54-
log.severe(traceElement.toString());
41+
log.info(
42+
() -> {
43+
if (log.isLoggable(Level.INFO)) {
44+
return message.toString();
5545
}
46+
return null;
5647
});
48+
ResponseMessage resp = new ResponseMessage();
49+
resp.setId(Integer.parseInt(((RequestMessage) message).getId()));
50+
51+
if (issues != null && !issues.isEmpty()) {
52+
var issue = issues.get(0);
53+
ResponseError re =
54+
new ResponseError(issue.getIssueCode(), issue.getText(), issue.getCause());
55+
resp.setError(re);
56+
if (issue.getCause() != null) {
57+
log.log(Level.SEVERE, issue.getCause().getMessage(), issue.getCause());
58+
}
59+
log.log(Level.SEVERE, issue.getText());
60+
} else {
61+
resp.setResult(Collections.emptyList());
62+
}
63+
64+
fh.writeResponse(resp);
5765
});
5866
StandardLauncher<Void> launcher = new StandardLauncher<>(smp, fh, es, null, null);
5967
launcher.startListening();

0 commit comments

Comments
 (0)