Skip to content

Commit

Permalink
Remove useless error log when use device login (#2690)
Browse files Browse the repository at this point in the history
* Remove useless error log when use device login

* Close the correspond class logger instead of root logger

* Put setLevel into try-catch closure to ensure log level will be restored
  • Loading branch information
Flanker32 authored Jan 22, 2019
1 parent dbb136c commit 2621de5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions PluginsAndFeatures/azure-toolkit-for-intellij/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ repositories {

configurations {
compile.exclude module:'slf4j-api'
compile.exclude module:'log4j'
cucumberRuntime {
extendsFrom testRuntime
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
import com.microsoft.aad.adal4j.AuthenticationResult;
import com.microsoft.aad.adal4j.DeviceCode;
import com.microsoft.intellij.ui.components.AzureDialogWrapper;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.Nullable;

import java.awt.Desktop;
import java.awt.Toolkit;
import java.awt.Window;
Expand Down Expand Up @@ -87,20 +91,29 @@ private void pullAuthenticationResult(final AuthenticationContext ctx, final Dev
final AuthenticationCallback<AuthenticationResult> callback) {
final long interval = deviceCode.getInterval();
long remaining = deviceCode.getExpiresIn();
while (remaining > 0 && authenticationResult == null) {
try {
remaining -= interval;
Thread.sleep(interval * 1000);
authenticationResult = ctx.acquireTokenByDeviceCode(deviceCode, callback).get();
} catch (ExecutionException | InterruptedException e) {
if (e.getCause() instanceof AuthenticationException &&
((AuthenticationException) e.getCause()).getErrorCode() == AdalErrorCode.AUTHORIZATION_PENDING) {
// swallow the pending exception
} else {
e.printStackTrace();
break;
// Close adal logger for it will write useless error log
// for issue #2368 https://github.com/Microsoft/azure-tools-for-java/issues/2368
Logger authLogger = Logger.getLogger(AuthenticationContext.class);
Level authLoggerLevel = authLogger.getLevel();
authLogger.setLevel(Level.OFF);
try {
while (remaining > 0 && authenticationResult == null) {
try {
remaining -= interval;
Thread.sleep(interval * 1000);
authenticationResult = ctx.acquireTokenByDeviceCode(deviceCode, callback).get();
} catch (ExecutionException | InterruptedException e) {
if (e.getCause() instanceof AuthenticationException &&
((AuthenticationException) e.getCause()).getErrorCode() == AdalErrorCode.AUTHORIZATION_PENDING) {
// swallow the pending exception
} else {
e.printStackTrace();
break;
}
}
}
} finally {
authLogger.setLevel(authLoggerLevel);
}
closeDialog();
}
Expand Down

0 comments on commit 2621de5

Please sign in to comment.