logback-android/src/main/java/ch/qos/logback/core/helpers/Transform.java
public static String escapeTags(final StringBuffer buf) {
    for (int i = 0; i < buf.length(); i++) {
      char ch = buf.charAt(i);
      switch (ch) {
        case '\t':
        case '\n':
        case '\r':
          // These characters are below '\u0020' but are allowed:
          break;
        case '&':
          buf.replace(i, i + 1, "&amp;");
          break;
        case '<':
          buf.replace(i, i + 1, "&lt;");
          break;
        case '>':
          buf.replace(i, i + 1, "&gt;");
          break;
        case '"':
          buf.replace(i, i + 1, "&quot;");
          break;
        case '\'':
          buf.replace(i, i + 1, "&#39;");
          break;
        default:
          if (ch < '\u0020') {
            // These characters are not allowed,
            // replace them with "Object replacement character":
            buf.replace(i, i + 1, "\uFFFD");
          }
          break;
      }
    }
    return buf.toString();
  }
public static String escapeTags(final StringBuffer buf) {
    for (int i = 0; i < buf.length(); i++) {
      char ch = buf.charAt(i);
      switch (ch) {
        case '\t':
        case '\n':
        case '\r':
          // These characters are below '\u0020' but are allowed:
          break;
        case '&':
          buf.replace(i, i + 1, "&amp;");
          break;
        case '<':
          buf.replace(i, i + 1, "&lt;");
          break;
        case '>':
          buf.replace(i, i + 1, "&gt;");
          break;
        case '"':
          buf.replace(i, i + 1, "&quot;");
          break;
        case '\'':
          buf.replace(i, i + 1, "&#39;");
          break;
        default:
          if (ch < '\u0020') {
            // These characters are not allowed,
            // replace them with "Object replacement character":
            buf.replace(i, i + 1, "\uFFFD");
          }
          break;
      }
    }
    return buf.toString();
  }
This method takes a StringBuilder which may contain HTML tags (ie, &lt;b&gt;,
&lt;table&gt;, etc) and replaces any '&lt;' and '&gt;' characters with
respective predefined entity references.
@param buf StringBuffer to transform
@return the string with the tags escaped