/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.contrib.staticinstrumenter.util;

import java.text.MessageFormat;
import java.util.regex.Pattern;

/**
 * A custom logger that uses System.err. This is necessary because we shouldn't configure any
 * conventional logger in {@code
 * io.opentelemetry.contrib.staticinstrumenter.agent.OpenTelemetryStaticAgent} (see {@code
 * io.opentelemetry.javaagent.OpenTelemetryAgent} docs).
 */
@SuppressWarnings("SystemOut")
public class SystemLogger {

  enum LogLevel {
    INFO,
    DEBUG,
    ERROR
  }

  private final Class<?> clazz;

  public static SystemLogger getLogger(Class<?> clazz) {
    return new SystemLogger(clazz);
  }

  private SystemLogger(Class<?> clazz) {
    this.clazz = clazz;
  }

  public void info(String message, Object... args) {
    System.err.println(format(LogLevel.INFO, message, args));
  }

  public void debug(String message, Object... args) {
    System.err.println(format(LogLevel.DEBUG, message, args));
  }

  public void error(String message, Object... args) {
    System.err.println(format(LogLevel.ERROR, message, args));
  }

  public void error(String message, Throwable t, Object... args) {
    System.err.println(format(LogLevel.ERROR, message, args));
    t.printStackTrace();
  }

  // visible for testing
  String format(LogLevel level, String message, Object... args) {
    int i = 2;
    while (message.contains("{}")) {
      message = message.replaceFirst(Pattern.quote("{}"), "{" + i++ + "}");
    }
    Object[] newArgs = new Object[args.length + 2];
    newArgs[0] = level;
    newArgs[1] = clazz.getName();

    System.arraycopy(args, 0, newArgs, 2, args.length);

    return MessageFormat.format("{0} {1} - " + message, newArgs);
  }
}

@@ -546,16 +546,155 @@
 gger %7B%0A%0A
+  private static final String DEBUG_SYSTEM_LOGGER = %22otel.javaagent.static-instrumentation.debug%22;%0A%0A  private final boolean debugEnabled;%0A%0A
   enum L
@@ -703,16 +703,16 @@
 Level %7B%0A
-
     INFO
@@ -907,24 +907,89 @@
 ?%3E clazz) %7B%0A
+    this.debugEnabled = Boolean.getBoolean(DEBUG_SYSTEM_LOGGER);%0A
     this.cla
@@ -1171,32 +1171,58 @@
 ject... args) %7B%0A
+    if (debugEnabled) %7B%0A  
     System.err.p
@@ -1260,32 +1260,38 @@
 essage, args));%0A
+    %7D%0A
   %7D%0A%0A  public vo

Disable by default the internal debug logs of static instrumentation (#492)