Skip to content

Commit 0efb623

Browse files
authored
add set log level handler for setting log level from vscode side. (microsoft#47)
* add set log level handler for setting log level from vscode side. * disable jdt log for unmatched level. * revert local debug changes * reset log levels for logger handler.
1 parent 8f6eda9 commit 0efb623

File tree

8 files changed

+82
-8
lines changed

8 files changed

+82
-8
lines changed

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/EventHub.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class EventHub implements IEventHub {
3434
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
3535
private PublishSubject<DebugEvent> subject = PublishSubject.<DebugEvent>create();
3636

37+
@Override
3738
public Observable<DebugEvent> events() {
3839
return subject;
3940
}
@@ -47,6 +48,7 @@ public Observable<DebugEvent> events() {
4748
* @param vm
4849
* the target virtual machine.
4950
*/
51+
@Override
5052
public void start(VirtualMachine vm) {
5153
if (isClosed) {
5254
throw new IllegalStateException("This event hub is already closed.");
@@ -66,7 +68,7 @@ public void start(VirtualMachine vm) {
6668
boolean shouldResume = true;
6769
for (Event event : set) {
6870
try {
69-
logger.info("\nJDI Event: " + event + "\n");
71+
logger.fine("\nJDI Event: " + event + "\n");
7072
} catch (VMDisconnectedException e) {
7173
// do nothing
7274
}
@@ -109,6 +111,7 @@ public void close() {
109111
* Gets the observable object for breakpoint events.
110112
* @return the observable object for breakpoint events
111113
*/
114+
@Override
112115
public Observable<DebugEvent> breakpointEvents() {
113116
return this.events().filter(debugEvent -> debugEvent.event instanceof BreakpointEvent);
114117
}
@@ -117,6 +120,7 @@ public Observable<DebugEvent> breakpointEvents() {
117120
* Gets the observable object for thread events.
118121
* @return the observable object for thread events
119122
*/
123+
@Override
120124
public Observable<DebugEvent> threadEvents() {
121125
return this.events().filter(debugEvent -> debugEvent.event instanceof ThreadStartEvent
122126
|| debugEvent.event instanceof ThreadDeathEvent);
@@ -126,6 +130,7 @@ public Observable<DebugEvent> threadEvents() {
126130
* Gets the observable object for exception events.
127131
* @return the observable object for exception events
128132
*/
133+
@Override
129134
public Observable<DebugEvent> exceptionEvents() {
130135
return this.events().filter(debugEvent -> debugEvent.event instanceof ExceptionEvent);
131136
}
@@ -134,6 +139,7 @@ public Observable<DebugEvent> exceptionEvents() {
134139
* Gets the observable object for step events.
135140
* @return the observable object for step events
136141
*/
142+
@Override
137143
public Observable<DebugEvent> stepEvents() {
138144
return this.events().filter(debugEvent -> debugEvent.event instanceof StepEvent);
139145
}
@@ -142,6 +148,7 @@ public Observable<DebugEvent> stepEvents() {
142148
* Gets the observable object for vm events.
143149
* @return the observable object for vm events
144150
*/
151+
@Override
145152
public Observable<DebugEvent> vmEvents() {
146153
return this.events().filter(debugEvent -> debugEvent.event instanceof VMStartEvent
147154
|| debugEvent.event instanceof VMDisconnectEvent

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/ProtocolServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private void sendMessage(Messages.ProtocolMessage message) {
157157
String utf8Data = new String(data, PROTOCOL_ENCODING);
158158

159159
try {
160-
logger.info("\n[[RESPONSE]]\n" + new String(data));
160+
logger.fine("\n[[RESPONSE]]\n" + new String(data));
161161
this.writer.write(utf8Data);
162162
this.writer.flush();
163163
} catch (IOException e) {
@@ -196,7 +196,7 @@ private void processData() {
196196

197197
private void dispatchRequest(String request) {
198198
try {
199-
logger.info("\n[REQUEST]\n" + request);
199+
logger.fine("\n[REQUEST]\n" + request);
200200
Messages.Request message = JsonUtils.fromJson(request, Messages.Request.class);
201201
usageDataSession.recordRequest(message);
202202
if (message.type.equals("request")) {

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/LaunchRequestHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void handle(Command command, Arguments arguments, Response response, IDeb
9393

9494
try {
9595
logger.info(String.format("Trying to launch Java Program with options \"%s -cp %s %s %s\" .",
96-
launchArguments.vmArgs, launchArguments.classPaths, launchArguments.mainClass, launchArguments.args));
96+
launchArguments.vmArgs, StringUtils.join(launchArguments.classPaths, ";"), launchArguments.mainClass, launchArguments.args));
9797
IDebugSession debugSession = DebugUtility.launch(vmProvider.getVirtualMachineManager(),
9898
launchArguments.mainClass, launchArguments.args, launchArguments.vmArgs, Arrays.asList(launchArguments.classPaths));
9999
context.setDebugSession(debugSession);

com.microsoft.java.debug.plugin/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<command id="vscode.java.resolveClasspath"/>
88
<command id="vscode.java.buildWorkspace"/>
99
<command id="vscode.java.fetchUsageData"/>
10+
<command id="vscode.java.configLogLevel"/>
1011
</delegateCommandHandler>
1112
</extension>
1213
</plugin>

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugDelegateCommandHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler
2828

2929
public static String BUILD_WORKSPACE = "vscode.java.buildWorkspace";
3030

31+
public static String CONFIG_LOG_LEVEL = "vscode.java.configLogLevel";
32+
33+
3134
@Override
3235
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor progress) throws Exception {
3336
if (DEBUG_STARTSESSION.equals(commandId)) {
@@ -41,6 +44,8 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
4144
// TODO
4245
} else if (FETCH_USER_DATA.equals(commandId)) {
4346
return UsageDataStore.getInstance().fetchAll();
47+
} else if (CONFIG_LOG_LEVEL.equals(commandId)) {
48+
return LogUtils.configLogLevel(arguments);
4449
}
4550

4651
throw new UnsupportedOperationException(String.format("Java debug plugin doesn't support the command '%s'.", commandId));

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebuggerServerPlugin.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@
2121

2222
public class JavaDebuggerServerPlugin implements BundleActivator {
2323
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
24-
private static final Logger usageDataLogger = Logger.getLogger(Configuration.USAGE_DATA_LOGGER_NAME);
2524

2625
public static final String PLUGIN_ID = "com.microsoft.java.debug";
2726
public static BundleContext context = null;
2827

2928
@Override
3029
public void start(BundleContext context) throws Exception {
3130
JavaDebuggerServerPlugin.context = context;
32-
logger.addHandler(new JdtLogHandler());
33-
logger.addHandler(new UsageDataLogHandler(Level.SEVERE));
34-
usageDataLogger.addHandler(new UsageDataLogHandler(Level.ALL));
31+
LogUtils.initialize(Level.INFO);
3532
logger.info("Starting " + PLUGIN_ID);
3633
}
3734

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtLogHandler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,21 @@
2121

2222
class JdtLogHandler extends Handler {
2323

24+
@Override
2425
public void close() {
2526
// do nothing
2627
}
2728

29+
@Override
2830
public void flush() {
2931
// do nothing
3032
}
3133

34+
@Override
3235
public void publish(LogRecord record) {
36+
if (!isLoggable(record)) {
37+
return;
38+
}
3339
int severity = IStatus.INFO;
3440
if (record.getLevel() == Level.SEVERE) {
3541
severity = IStatus.ERROR;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2017 Microsoft Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Microsoft Corporation - initial API and implementation
10+
*******************************************************************************/
11+
12+
package com.microsoft.java.debug.plugin.internal;
13+
14+
import java.util.List;
15+
import java.util.logging.Level;
16+
import java.util.logging.Logger;
17+
18+
import org.apache.commons.lang3.StringUtils;
19+
20+
import com.microsoft.java.debug.core.Configuration;
21+
22+
public final class LogUtils {
23+
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
24+
private static final Logger usageDataLogger = Logger.getLogger(Configuration.USAGE_DATA_LOGGER_NAME);
25+
26+
27+
/**
28+
* Initialize logger for logger level and logger handler.
29+
* @param level the logger level for java debugger.
30+
*/
31+
public static void initialize(Level level) {
32+
logger.addHandler(new JdtLogHandler());
33+
logger.addHandler(new UsageDataLogHandler(Level.SEVERE));
34+
usageDataLogger.addHandler(new UsageDataLogHandler(Level.ALL));
35+
logger.setLevel(level);
36+
}
37+
38+
/**
39+
* Configure log level setting for java debugger.
40+
* @param arguments the first element of the arguments should be the String representation of level(info, fine, warning..).
41+
*/
42+
public static Object configLogLevel(List<Object> arguments) {
43+
if (arguments != null && arguments.size() == 1 && arguments.get(0) instanceof String) {
44+
try {
45+
logger.setLevel(Level.parse((String) arguments.get(0)));
46+
logger.info(String.format("Set log level to : %s", arguments.get(0)));
47+
return logger.getLevel().toString();
48+
} catch (IllegalArgumentException e) {
49+
logger.severe(String.format("Invalid log level: %s", arguments.get(0)));
50+
}
51+
52+
} else {
53+
logger.severe(String.format("Invalid parameters for configLogLevel: %s", StringUtils.join(arguments)));
54+
}
55+
return null;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)