Skip to content

Commit 57b40d9

Browse files
authored
Update warning logs (#369)
* update how we log warnings * add stack trace * use name of class as type * update casing * update messages * rename method
1 parent de329ae commit 57b40d9

File tree

10 files changed

+37
-39
lines changed

10 files changed

+37
-39
lines changed

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/AWSSystemExitHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.logging.Handler;
99
import java.util.logging.Logger;
1010
import org.code.protocol.JavabuilderContext;
11-
import org.code.protocol.JavabuilderThrowableMessageUtils;
11+
import org.code.protocol.LoggerUtils;
1212

1313
public class AWSSystemExitHelper implements SystemExitHelper {
1414
private final String connectionId;
@@ -42,7 +42,7 @@ private void cleanUpResources() {
4242
// Handle any other exceptions so that shut down proceeds normally. If this is an
4343
// IllegalStateException, it indicates that the connection was already shut down for
4444
// some reason.
45-
Logger.getLogger(MAIN_LOGGER).warning(JavabuilderThrowableMessageUtils.getLoggingString(e));
45+
LoggerUtils.logTrackingExceptionAsWarning(e);
4646
}
4747
// clean up log handler to avoid duplicate logs in future runs.
4848
Handler[] allHandlers = Logger.getLogger(MAIN_LOGGER).getHandlers();

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/AssetFileStubber.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.code.javabuilder;
22

3-
import static org.code.protocol.LoggerNames.MAIN_LOGGER;
4-
5-
import java.util.logging.Logger;
3+
import org.code.protocol.LoggerUtils;
64

75
/**
86
* Generates URLs for local, static asset files that can be used to stub asset file requests when
@@ -38,8 +36,9 @@ public String getStubAssetUrl(String filename) {
3836
return stubAudioUrl;
3937
}
4038

41-
Logger.getLogger(MAIN_LOGGER)
42-
.warning(String.format("Unknown file %s. Cannot provide stubbed asset URL.", filename));
39+
LoggerUtils.logWarning(
40+
"Unknown Asset File",
41+
String.format("Unknown file %s. Cannot provide stubbed asset URL.", filename));
4342
return null;
4443
}
4544

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/CodeExecutionManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private void onPostExecute() {
161161
// If there was an issue clearing the temp directory, this may be because too many files are
162162
// open. Force the JVM to quit in order to release the resources for the next use of the
163163
// container. Temporarily logging the exception for investigation purposes.
164-
LoggerUtils.logTrackingException(e);
164+
LoggerUtils.logTrackingExceptionAsWarning(e);
165165
this.systemExitHelper.exit(TEMP_DIRECTORY_CLEANUP_ERROR_CODE);
166166
} finally {
167167
// Replace System in/out with original System in/out

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/ExceptionHandler.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package org.code.javabuilder;
22

3-
import static org.code.protocol.LoggerNames.MAIN_LOGGER;
4-
5-
import java.util.logging.Logger;
63
import org.code.javabuilder.util.LambdaUtils;
74
import org.code.protocol.*;
85

@@ -57,7 +54,7 @@ public void handle(Throwable e) {
5754
// Internal facing exceptions are caused by us (essentially an HTTP 5xx error), but don't affect
5855
// the user. Log only.
5956
if (e instanceof InternalFacingException || e instanceof InternalFacingRuntimeException) {
60-
Logger.getLogger(MAIN_LOGGER).warning(((LoggableProtocol) e).getLoggingString());
57+
LoggerUtils.logTrackingExceptionAsWarning(e);
6158
return;
6259
}
6360

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/LambdaRequestHandler.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ private void shutDown(
315315
// so these don't need to be reported to the user.
316316
final InternalFacingRuntimeException internal =
317317
new InternalFacingRuntimeException("Exception during shutdown", e);
318-
Logger.getLogger(MAIN_LOGGER).warning(internal.getLoggingString());
318+
LoggerUtils.logTrackingExceptionAsWarning(internal);
319319
}
320320
}
321321

@@ -391,7 +391,7 @@ private void cleanUpAWSResources(String connectionId, AmazonApiGatewayManagement
391391
// Handle any other exceptions so that shut down proceeds normally. If this is an
392392
// IllegalStateException, it indicates that the connection was already shut down for
393393
// some reason.
394-
Logger.getLogger(MAIN_LOGGER).warning(JavabuilderThrowableMessageUtils.getLoggingString(e));
394+
LoggerUtils.logTrackingExceptionAsWarning(e);
395395
}
396396
// clean up log handler to avoid duplicate logs in future runs.
397397
Handler[] allHandlers = Logger.getLogger(MAIN_LOGGER).getHandlers();
@@ -408,9 +408,7 @@ private void verifyApiClient(String connectionId) {
408408
} catch (IllegalStateException e) {
409409
// This can occur if the api client has been shut down, which we have seen happen on occasion.
410410
// Recreate the api client in this case. Log a warning so we can track when this happens.
411-
Logger.getLogger(MAIN_LOGGER)
412-
.warning(
413-
"Received illegal state exception when trying to talk to API Gateway. Recreating api client.");
411+
LoggerUtils.logWarning("API Gateway Client Gone", e.getMessage());
414412
this.apiClient =
415413
AmazonApiGatewayManagementApiClientBuilder.standard()
416414
.withEndpointConfiguration(

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/UserClassLoader.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.code.javabuilder;
22

3-
import static org.code.protocol.LoggerNames.MAIN_LOGGER;
4-
53
import java.lang.invoke.LambdaMetafactory;
64
import java.lang.invoke.StringConcatFactory;
75
import java.lang.reflect.InvocationTargetException;
@@ -11,9 +9,7 @@
119
import java.util.HashSet;
1210
import java.util.List;
1311
import java.util.Set;
14-
import java.util.logging.Logger;
15-
import org.code.protocol.LoggerConstants;
16-
import org.json.JSONObject;
12+
import org.code.protocol.LoggerUtils;
1713

1814
/**
1915
* Custom class loader for user-provided code. This class loader only allows certain classes to be
@@ -60,10 +56,7 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
6056

6157
// Log that we are going to throw an exception. Log as a warning
6258
// as it is most likely user error, but we want to track it.
63-
JSONObject eventData = new JSONObject();
64-
eventData.put(LoggerConstants.TYPE, "invalidClass");
65-
eventData.put(LoggerConstants.CLASS_NAME, name);
66-
Logger.getLogger(MAIN_LOGGER).warning(eventData.toString());
59+
LoggerUtils.logWarning("Invalid Class", name);
6760
throw new ClassNotFoundException(name);
6861
}
6962

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/UserCodeCompiler.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package org.code.javabuilder;
22

3-
import static org.code.protocol.LoggerNames.MAIN_LOGGER;
4-
53
import java.io.File;
64
import java.io.IOException;
75
import java.io.Reader;
86
import java.util.*;
9-
import java.util.logging.Logger;
107
import javax.tools.*;
118
import javax.tools.JavaCompiler.CompilationTask;
129
import org.code.javabuilder.util.JarUtils;
@@ -157,10 +154,10 @@ private String getCustomCompilerError(Diagnostic<? extends JavaFileObject> diagn
157154
*/
158155
private String getCompilerError(Diagnostic<? extends JavaFileObject> diagnostic) {
159156
if (diagnostic.getSource() == null || diagnostic.getLineNumber() == Diagnostic.NOPOS) {
160-
Logger.getLogger(MAIN_LOGGER)
161-
.warning(
162-
"Falling back to default compiler error, diagnostic source was null or line number was -1. Diagnostic error code is "
163-
+ diagnostic.getCode());
157+
LoggerUtils.logWarning(
158+
"Unknown Compiler Error",
159+
"Falling back to default compiler error, diagnostic source was null or line number was -1. Diagnostic error code is "
160+
+ diagnostic.getCode());
164161
return diagnostic.toString();
165162
}
166163
// Subtract 1 from the line number to account for our auto-import

org-code-javabuilder/lib/src/main/java/org/code/javabuilder/util/LambdaUtils.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package org.code.javabuilder.util;
22

33
import static org.code.javabuilder.InternalFacingExceptionTypes.CONNECTION_TERMINATED;
4-
import static org.code.protocol.LoggerNames.MAIN_LOGGER;
54

6-
import java.util.logging.Logger;
75
import org.code.javabuilder.InternalFacingRuntimeException;
86
import org.code.protocol.ClientMessage;
9-
import org.code.protocol.JavabuilderThrowableMessageUtils;
7+
import org.code.protocol.LoggerUtils;
108
import org.code.protocol.OutputAdapter;
119

1210
public final class LambdaUtils {
@@ -30,11 +28,11 @@ public static void safelySendMessage(
3028
} catch (InternalFacingRuntimeException e) {
3129
// Unless logOnLostConnection is true, only log for messages that aren't CONNECTION_TERMINATED
3230
if (logOnLostConnection || !e.getMessage().equals(CONNECTION_TERMINATED)) {
33-
Logger.getLogger(MAIN_LOGGER).warning(e.getLoggingString());
31+
LoggerUtils.logTrackingExceptionAsWarning(e);
3432
}
3533
} catch (Exception e) {
3634
// Catch any other exceptions here to prevent them from propagating.
37-
Logger.getLogger(MAIN_LOGGER).warning(JavabuilderThrowableMessageUtils.getLoggingString(e));
35+
LoggerUtils.logTrackingExceptionAsWarning(e);
3836
}
3937
}
4038
}

org-code-javabuilder/protocol/src/main/java/org/code/protocol/LoggerConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class LoggerConstants {
1414
public static final String CHANNEL_ID = "channelId";
1515
public static final String MINI_APP_TYPE = "miniAppType";
1616
public static final String SESSION_METADATA = "sessionMetadata";
17+
public static final String DETAIL = "detail";
18+
public static final String STACK_TRACE = "stackTrace";
1719

1820
// Constants associated with disk space metrics
1921
public static final String DIRECTORY = "directory";

org-code-javabuilder/protocol/src/main/java/org/code/protocol/LoggerUtils.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static org.code.protocol.LoggerNames.MAIN_LOGGER;
44

55
import java.io.File;
6+
import java.io.PrintWriter;
7+
import java.io.StringWriter;
68
import java.nio.file.Path;
79
import java.nio.file.Paths;
810
import java.util.logging.Logger;
@@ -99,19 +101,31 @@ public static void logSevereException(Throwable e) {
99101
* Exceptions logged in this way are intended to be informative, but should be removed after a
100102
* period of time.
101103
*/
102-
public static void logTrackingException(Throwable e) {
104+
public static void logTrackingExceptionAsWarning(Throwable e) {
103105
JSONObject eventData = new JSONObject();
106+
eventData.put(LoggerConstants.TYPE, e.getClass().getName());
104107
eventData.put(LoggerConstants.EXCEPTION_MESSAGE, e.getMessage());
105108
if (e.getCause() != null) {
106109
eventData.put(LoggerConstants.CAUSE, e.getCause());
107110
}
111+
StringWriter sw = new StringWriter();
112+
PrintWriter pw = new PrintWriter(sw);
113+
e.printStackTrace(pw);
114+
eventData.put(LoggerConstants.STACK_TRACE, sw.toString());
108115
Logger.getLogger(MAIN_LOGGER).warning(eventData.toString());
109116
}
110117

111118
public static void logInfo(String info) {
112119
Logger.getLogger(MAIN_LOGGER).info(info);
113120
}
114121

122+
public static void logWarning(String type, String detail) {
123+
JSONObject eventData = new JSONObject();
124+
eventData.put(LoggerConstants.TYPE, type);
125+
eventData.put(LoggerConstants.DETAIL, detail);
126+
Logger.getLogger(MAIN_LOGGER).warning(eventData.toString());
127+
}
128+
115129
private static void sendDiskSpaceLogs(String type) {
116130
File f = Paths.get(System.getProperty("java.io.tmpdir")).toFile();
117131
JSONObject eventData = new JSONObject();

0 commit comments

Comments
 (0)