Since I could not find suitable alternatives in guava, I gave it another option using the StringUtilsfrom class lang3. I made a little utility function that escapes newlines and tabs. Suggestions are welcome, but for now it will be done.
public static String escapeForLogs(String input) {
return org.apache.commons.lang3.StringUtils.replaceEach(
input,
new String[] { "\r\n", "\r", "\n", "\t" },
new String[] { "\\\\n", "\\\\n", "\\\\n", "\\\\t" }
);
}
I run the following tests:
@Test
public void testEscapeForLogs() {
assertEquals("without linebreaks/tabs stays the same", "lala", e scapeForLogs("lala"));
assertEquals("empty string is fine", "", escapeForLogs(""));
assertEquals("newline gets escaped", "\\\\n", escapeForLogs("\n"));
assertEquals("two newlines", "\\\\n\\\\n", escapeForLogs("\n\n"));
assertEquals("tab", "\\\\t", escapeForLogs("\t"));
assertEquals("return carridge gets escaped", "\\\\n", escapeForLogs("\r"));
assertEquals("return carridge+newline gets converted", "\\\\n", escapeForLogs("\r\n"));
assertEquals("newline before cr+nl", "\\\\n\\\\n", escapeForLogs("\n\r\n"));
assertEquals("2 cr+nl", "\\\\n\\\\n", escapeForLogs("\r\n\r\n"));
assertEquals("some combination", "lala\\\\nlalala\\\\n\\\\nla\\\\tla", escapeForLogs("lala\nlalala\n\nla\tla"));
}
source
share