diff --git a/azkaban-common/src/main/java/azkaban/utils/PatternLayoutEscaped.java b/azkaban-common/src/main/java/azkaban/utils/PatternLayoutEscaped.java
new file mode 100644
index 0000000..ff5ad87
--- /dev/null
+++ b/azkaban-common/src/main/java/azkaban/utils/PatternLayoutEscaped.java
@@ -0,0 +1,39 @@
+package azkaban.utils;
+
+import org.apache.log4j.PatternLayout;
+import org.apache.log4j.spi.LoggingEvent;
+
+/**
+ * When we use log4j to send a JSON message the official PatternLayout will send corrupt JSON if certain characters are
+ * present. We use this PatternLayoutEscaped class as a thin interface around PatternLayout to escape all these
+ * characters.
+ */
+public class PatternLayoutEscaped extends PatternLayout {
+ public String format(final LoggingEvent event) {
+ if (event.getMessage() instanceof String) {
+ return super.format(copyAndEscapeEvent(event));
+ }
+ return super.format(event);
+ }
+
+ /**
+ * Create a copy of event, but escape backslashes, tabs, newlines and quotes in its message
+ */
+ private LoggingEvent copyAndEscapeEvent(LoggingEvent event) {
+ String message = event.getMessage().toString();
+ message = message
+ .replace("\\", "\\\\")
+ .replace("\n", "\\n")
+ .replace("\"", "\\\"")
+ .replace("\t", "\\t");
+
+ Throwable throwable = event.getThrowableInformation() == null ? null
+ : event.getThrowableInformation().getThrowable();
+ return new LoggingEvent(event.getFQNOfLoggerClass(),
+ event.getLogger(),
+ event.getTimeStamp(),
+ event.getLevel(),
+ message,
+ throwable);
+ }
+}
diff --git a/azkaban-common/src/test/java/azkaban/utils/PatternLayoutEscapedTest.java b/azkaban-common/src/test/java/azkaban/utils/PatternLayoutEscapedTest.java
new file mode 100644
index 0000000..b92b233
--- /dev/null
+++ b/azkaban-common/src/test/java/azkaban/utils/PatternLayoutEscapedTest.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2016 LinkedIn Corp.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package azkaban.utils;
+
+import org.apache.log4j.PatternLayout;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.spi.LoggingEvent;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test output of PatternLayoutEscapedTest
+ * It should be escaping new lines, quotes, tabs and backslashes
+ * This is necessary when we are logging these messages out as JSON objects
+ */
+public class PatternLayoutEscapedTest {
+ private Logger logger = Logger.getLogger(this.getClass());
+ private PatternLayout layout = new PatternLayoutEscaped();
+
+ @Before
+ public void beforeTest() {
+ layout.setConversionPattern("%m");
+ }
+
+ @Test
+ public void testNewLine() {
+ LoggingEvent event = createMessageEvent("This message contains \n new lines");
+ assertTrue(layout.format(event).equals("This message contains \\n new lines"));
+ }
+
+ @Test
+ public void testQuote() {
+ LoggingEvent event = createMessageEvent("This message contains \" quotes");
+ assertTrue(layout.format(event).equals("This message contains \\\" quotes"));
+ }
+
+ @Test
+ public void testTab() {
+ LoggingEvent event = createMessageEvent("This message contains a tab \t");
+ assertTrue(layout.format(event).equals("This message contains a tab \\t"));
+ }
+
+ @Test
+ public void testBackSlash() {
+ LoggingEvent event = createMessageEvent("This message contains a backslash \\");
+ assertTrue(layout.format(event).equals("This message contains a backslash \\\\"));
+ }
+
+ private LoggingEvent createMessageEvent(String message) {
+ return new LoggingEvent(this.getClass().getCanonicalName(),
+ logger,
+ 0,
+ Level.toLevel("INFO"),
+ message,
+ new Exception());
+ }
+}