static String createFailureLog(
     String user,
     String operation,
     String perm,
     String target,
     String description,
     ApplicationId appId,
     ApplicationAttemptId attemptId,
     ContainerId containerId,
     CallerContext callerContext) {
   StringBuilder b = new StringBuilder();
   start(Keys.USER, user, b);
   addRemoteIP(b);
   add(Keys.OPERATION, operation, b);
   add(Keys.TARGET, target, b);
   add(Keys.RESULT, AuditConstants.FAILURE, b);
   add(Keys.DESCRIPTION, description, b);
   add(Keys.PERMISSIONS, perm, b);
   if (appId != null) {
     add(Keys.APPID, appId.toString(), b);
   }
   if (attemptId != null) {
     add(Keys.APPATTEMPTID, attemptId.toString(), b);
   }
   if (containerId != null) {
     add(Keys.CONTAINERID, containerId.toString(), b);
   }
   appendCallerContext(b, callerContext);
   return b.toString();
 }
 /** A helper api to add remote IP address */
 static void addRemoteIP(StringBuilder b) {
   InetAddress ip = Server.getRemoteIp();
   // ip address can be null for testcases
   if (ip != null) {
     add(Keys.IP, ip.getHostAddress(), b);
   }
 }
 /** A helper api for creating an audit log for a successful event. */
 static String createSuccessLog(
     String user,
     String operation,
     String target,
     ApplicationId appId,
     ApplicationAttemptId attemptId,
     ContainerId containerId,
     CallerContext callerContext) {
   StringBuilder b = new StringBuilder();
   start(Keys.USER, user, b);
   addRemoteIP(b);
   add(Keys.OPERATION, operation, b);
   add(Keys.TARGET, target, b);
   add(Keys.RESULT, AuditConstants.SUCCESS, b);
   if (appId != null) {
     add(Keys.APPID, appId.toString(), b);
   }
   if (attemptId != null) {
     add(Keys.APPATTEMPTID, attemptId.toString(), b);
   }
   if (containerId != null) {
     add(Keys.CONTAINERID, containerId.toString(), b);
   }
   appendCallerContext(b, callerContext);
   return b.toString();
 }
  private static void appendCallerContext(StringBuilder sb, CallerContext callerContext) {
    String context = null;
    byte[] signature = null;

    if (callerContext != null) {
      context = callerContext.getContext();
      signature = callerContext.getSignature();
    }

    if (context != null) {
      add(Keys.CALLERCONTEXT, context, sb);
    }

    if (signature != null) {
      try {
        String sigStr = new String(signature, "UTF-8");
        add(Keys.CALLERSIGNATURE, sigStr, sb);
      } catch (UnsupportedEncodingException e) {
        // ignore this signature
      }
    }
  }