Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback behavior for inadvertent string formatters in log messages #9

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

Add fallback behavior for bad string formatting

  • Loading branch information
josefdlange committed May 8, 2016
commit 26087727945152e5b6afa647732edbf86a30e1c5
@@ -155,9 +155,15 @@ public void w(Throwable t, String message, Object... args) {
* @return JSON string
*/
private String toJson(Level level, String message, Object... args) {
return String.format("{\"level\": \"%1$s\", \"message\": \"%2$s\"}",
level,
String.format(message, args).replace("\"", "\\\""));
try {
return String.format("{\"level\": \"%1$s\", \"message\": \"%2$s\"}",
level,
String.format(message, args).replace("\"", "\\\""));
} catch(MissingFormatArgumentException exception) {
return String.format("{\"level\": \"%1$s\", \"message\": \"%2$s\"}",
level,
message.replace("\"", "\\\""));
}
}

/**
@@ -180,10 +186,18 @@ private String formatThrowable(Throwable t) {
* @return JSON string
*/
private String toJson(Level level, String message, Throwable t, Object... args) {
return String.format("{\"level\": \"%1$s\", \"message\": \"%2$s\", \"exception\": \"%3$s\"}",
level,
String.format(message, args).replace("\"", "\\\""),
formatThrowable(t));
try {
return String.format("{\"level\": \"%1$s\", \"message\": \"%2$s\", \"exception\": \"%3$s\"}",
level,
String.format(message, args).replace("\"", "\\\""),
formatThrowable(t));
} catch(MissingFormatArgumentException exception) {
return String.format("{\"level\": \"%1$s\", \"message\": \"%2$s\", \"exception\": \"%3$s\"}",
level,
message.replace("\"", "\\\""),
formatThrowable(t));
}

}

/**
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.