/*
 * Copyright 2015-present Facebook, Inc.
 *
 * 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 com.facebook.buck.log;

import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Formatter;
import java.util.IllegalFormatException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.LogRecord;

@SuppressWarnings("serial")
public class AppendableLogRecord extends LogRecord {
  private final String displayLevel;

  public AppendableLogRecord(Level level, String displayLevel, String msg) {
    super(level, msg);
    this.displayLevel = displayLevel;
  }

  public void appendFormattedMessage(StringBuilder sb) {
    // Unfortunately, there's no public API to reset a Formatter's
    // Appendable. If this proves to be a perf issue, we can do
    // runtime introspection to access the private Formatter.init()
    // API to replace the Appendable.

    try (Formatter f = new Formatter(sb, Locale.US)) {
      f.format(getMessage(), getParameters());
    } catch (IllegalFormatException e) {
      sb.append("Invalid format string: ");
      sb.append(displayLevel);
      sb.append(" '");
      sb.append(getMessage());
      sb.append("' ");
      sb.append(Arrays.asList(getParameters()).toString());
    } catch (ConcurrentModificationException originalException) {
      // This way we may be at least able to figure out where offending log was created.
      throw new ConcurrentModificationException(
          "Concurrent modification when logging for message " + getMessage(),
          originalException);
    }
  }
}

@@ -1718,24 +1718,133 @@
 pend(%22' %22);%0A
+      Object%5B%5D params = getParameters();%0A      if (params == null) %7B%0A        params = new Object%5B0%5D;%0A      %7D%0A
       sb.app
@@ -1861,31 +1861,22 @@
 .asList(
-getParameters()
+params
 ).toStri

Add null check