public final class

Log

extends java.lang.Object

 java.lang.Object

↳androidx.media3.common.util.Log

Gradle dependencies

compile group: 'androidx.media3', name: 'media3-common', version: '1.5.0-alpha01'

  • groupId: androidx.media3
  • artifactId: media3-common
  • version: 1.5.0-alpha01

Artifact androidx.media3:media3-common:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

Wrapper around which allows to set the log level and to specify a custom log output.

Summary

Fields
public static final intLOG_LEVEL_ALL

Log level to log all messages.

public static final intLOG_LEVEL_ERROR

Log level to only log error messages.

public static final intLOG_LEVEL_INFO

Log level to only log informative, warning and error messages.

public static final intLOG_LEVEL_OFF

Log level to disable all logging.

public static final intLOG_LEVEL_WARNING

Log level to only log warning and error messages.

Methods
public static java.lang.StringappendThrowableString(java.lang.String message, java.lang.Throwable throwable)

Appends the result of Log.getThrowableString(Throwable) (if non-empty) to message.

public static voidd(java.lang.String tag, java.lang.String message)

Logs a debug-level message.

public static voidd(java.lang.String tag, java.lang.String message, java.lang.Throwable throwable)

Logs a debug-level message with an optional associated java.lang.Throwable.

public static voide(java.lang.String tag, java.lang.String message)

Logs an error-level message.

public static voide(java.lang.String tag, java.lang.String message, java.lang.Throwable throwable)

Logs an error-level message with an optional associated java.lang.Throwable.

public static intgetLogLevel()

Returns current Log.LogLevel for ExoPlayer logcat logging.

public static java.lang.StringgetThrowableString(java.lang.Throwable throwable)

Returns a string representation of a java.lang.Throwable suitable for logging, taking into account whether Log.setLogStackTraces(boolean) stack trace logging} is enabled.

public static voidi(java.lang.String tag, java.lang.String message)

Logs an information-level message.

public static voidi(java.lang.String tag, java.lang.String message, java.lang.Throwable throwable)

Logs an information-level message with an optional associated java.lang.Throwable.

public static voidsetLogger(Log.Logger logger)

Sets a custom Log.Logger as the output.

public static voidsetLogLevel(int logLevel)

Sets the Log.LogLevel for ExoPlayer logcat logging.

public static voidsetLogStackTraces(boolean logStackTraces)

Sets whether stack traces of java.lang.Throwables will be logged to logcat.

public static voidw(java.lang.String tag, java.lang.String message)

Logs a warning-level message.

public static voidw(java.lang.String tag, java.lang.String message, java.lang.Throwable throwable)

Logs a warning-level message with an optional associated java.lang.Throwable..

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final int LOG_LEVEL_ALL

Log level to log all messages.

public static final int LOG_LEVEL_INFO

Log level to only log informative, warning and error messages.

public static final int LOG_LEVEL_WARNING

Log level to only log warning and error messages.

public static final int LOG_LEVEL_ERROR

Log level to only log error messages.

public static final int LOG_LEVEL_OFF

Log level to disable all logging.

Methods

public static int getLogLevel()

Returns current Log.LogLevel for ExoPlayer logcat logging.

public static void setLogLevel(int logLevel)

Sets the Log.LogLevel for ExoPlayer logcat logging.

Parameters:

logLevel: The new Log.LogLevel.

public static void setLogStackTraces(boolean logStackTraces)

Sets whether stack traces of java.lang.Throwables will be logged to logcat. Stack trace logging is enabled by default.

Parameters:

logStackTraces: Whether stack traces will be logged.

public static void setLogger(Log.Logger logger)

Sets a custom Log.Logger as the output.

Parameters:

logger: The Log.Logger.

public static void d(java.lang.String tag, java.lang.String message)

Logs a debug-level message.

Parameters:

tag: The tag of the message.
message: The message.

public static void d(java.lang.String tag, java.lang.String message, java.lang.Throwable throwable)

Logs a debug-level message with an optional associated java.lang.Throwable.

Parameters:

tag: The tag of the message.
message: The message.
throwable: The java.lang.Throwable associated with the message, or null if not specified.

public static void i(java.lang.String tag, java.lang.String message)

Logs an information-level message.

Parameters:

tag: The tag of the message.
message: The message.

public static void i(java.lang.String tag, java.lang.String message, java.lang.Throwable throwable)

Logs an information-level message with an optional associated java.lang.Throwable.

Parameters:

tag: The tag of the message.
message: The message.
throwable: The java.lang.Throwable associated with the message, or null if not specified.

public static void w(java.lang.String tag, java.lang.String message)

Logs a warning-level message.

Parameters:

tag: The tag of the message.
message: The message.

public static void w(java.lang.String tag, java.lang.String message, java.lang.Throwable throwable)

Logs a warning-level message with an optional associated java.lang.Throwable..

Parameters:

tag: The tag of the message.
message: The message.
throwable: The java.lang.Throwable associated with the message, or null if not specified.

public static void e(java.lang.String tag, java.lang.String message)

Logs an error-level message.

Parameters:

tag: The tag of the message.
message: The message.

public static void e(java.lang.String tag, java.lang.String message, java.lang.Throwable throwable)

Logs an error-level message with an optional associated java.lang.Throwable.

Parameters:

tag: The tag of the message.
message: The message.
throwable: The java.lang.Throwable associated with the message, or null if not specified.

public static java.lang.String getThrowableString(java.lang.Throwable throwable)

Returns a string representation of a java.lang.Throwable suitable for logging, taking into account whether Log.setLogStackTraces(boolean) stack trace logging} is enabled.

Stack trace logging may be unconditionally suppressed for some expected failure modes (e.g., Throwables that are expected if the device doesn't have network connectivity) to avoid log spam.

Parameters:

throwable: The java.lang.Throwable.

Returns:

The string representation of the java.lang.Throwable, or null if throwable is null.

public static java.lang.String appendThrowableString(java.lang.String message, java.lang.Throwable throwable)

Appends the result of Log.getThrowableString(Throwable) (if non-empty) to message.

Source

/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * 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 androidx.media3.common.util;

import static java.lang.annotation.ElementType.TYPE_USE;

import android.text.TextUtils;
import androidx.annotation.GuardedBy;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.annotation.Size;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.net.UnknownHostException;
import org.checkerframework.dataflow.qual.Pure;

/**
 * Wrapper around {@link android.util.Log} which allows to set the log level and to specify a custom
 * log output.
 */
@UnstableApi
public final class Log {

  /**
   * Log level for ExoPlayer logcat logging. One of {@link #LOG_LEVEL_ALL}, {@link #LOG_LEVEL_INFO},
   * {@link #LOG_LEVEL_WARNING}, {@link #LOG_LEVEL_ERROR} or {@link #LOG_LEVEL_OFF}.
   */
  @Documented
  @Retention(RetentionPolicy.SOURCE)
  @Target(TYPE_USE)
  @IntDef({LOG_LEVEL_ALL, LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR, LOG_LEVEL_OFF})
  public @interface LogLevel {}

  /** Log level to log all messages. */
  public static final int LOG_LEVEL_ALL = 0;

  /** Log level to only log informative, warning and error messages. */
  public static final int LOG_LEVEL_INFO = 1;

  /** Log level to only log warning and error messages. */
  public static final int LOG_LEVEL_WARNING = 2;

  /** Log level to only log error messages. */
  public static final int LOG_LEVEL_ERROR = 3;

  /** Log level to disable all logging. */
  public static final int LOG_LEVEL_OFF = Integer.MAX_VALUE;

  /**
   * Interface for a logger that can output messages with a tag.
   *
   * <p>Use {@link #DEFAULT} to output to {@link android.util.Log}.
   */
  public interface Logger {

    /** The default instance logging to {@link android.util.Log}. */
    Logger DEFAULT =
        new Logger() {
          @Override
          public void d(String tag, String message, @Nullable Throwable throwable) {
            android.util.Log.d(tag, appendThrowableString(message, throwable));
          }

          @Override
          public void i(String tag, String message, @Nullable Throwable throwable) {
            android.util.Log.i(tag, appendThrowableString(message, throwable));
          }

          @Override
          public void w(String tag, String message, @Nullable Throwable throwable) {
            android.util.Log.w(tag, appendThrowableString(message, throwable));
          }

          @Override
          public void e(String tag, String message, @Nullable Throwable throwable) {
            android.util.Log.e(tag, appendThrowableString(message, throwable));
          }
        };

    /**
     * Logs a debug-level message with an optional associated {@link Throwable}.
     *
     * @param tag The tag of the message.
     * @param message The message.
     * @param throwable The {@link Throwable} associated with the message, or null if not specified.
     */
    void d(String tag, String message, @Nullable Throwable throwable);

    /**
     * Logs an information-level message with an optional associated {@link Throwable}.
     *
     * @param tag The tag of the message.
     * @param message The message.
     * @param throwable The {@link Throwable} associated with the message, or null if not specified.
     */
    void i(String tag, String message, @Nullable Throwable throwable);

    /**
     * Logs a warning-level message with an optional associated {@link Throwable}.
     *
     * @param tag The tag of the message.
     * @param message The message.
     * @param throwable The {@link Throwable} associated with the message, or null if not specified.
     */
    void w(String tag, String message, @Nullable Throwable throwable);

    /**
     * Logs an error-level message with an optional associated {@link Throwable}.
     *
     * @param tag The tag of the message.
     * @param message The message.
     * @param throwable The {@link Throwable} associated with the message, or null if not specified.
     */
    void e(String tag, String message, @Nullable Throwable throwable);
  }

  private static final Object lock = new Object();

  @GuardedBy("lock")
  private static int logLevel = LOG_LEVEL_ALL;

  @GuardedBy("lock")
  private static boolean logStackTraces = true;

  @GuardedBy("lock")
  private static Logger logger = Logger.DEFAULT;

  private Log() {}

  /** Returns current {@link LogLevel} for ExoPlayer logcat logging. */
  @Pure
  public static @LogLevel int getLogLevel() {
    synchronized (lock) {
      return logLevel;
    }
  }

  /**
   * Sets the {@link LogLevel} for ExoPlayer logcat logging.
   *
   * @param logLevel The new {@link LogLevel}.
   */
  public static void setLogLevel(@LogLevel int logLevel) {
    synchronized (lock) {
      Log.logLevel = logLevel;
    }
  }

  /**
   * Sets whether stack traces of {@link Throwable}s will be logged to logcat. Stack trace logging
   * is enabled by default.
   *
   * @param logStackTraces Whether stack traces will be logged.
   */
  public static void setLogStackTraces(boolean logStackTraces) {
    synchronized (lock) {
      Log.logStackTraces = logStackTraces;
    }
  }

  /**
   * Sets a custom {@link Logger} as the output.
   *
   * @param logger The {@link Logger}.
   */
  public static void setLogger(Logger logger) {
    synchronized (lock) {
      Log.logger = logger;
    }
  }

  /**
   * Logs a debug-level message.
   *
   * @param tag The tag of the message.
   * @param message The message.
   */
  @Pure
  public static void d(@Size(max = 23) String tag, String message) {
    synchronized (lock) {
      if (logLevel == LOG_LEVEL_ALL) {
        logger.d(tag, message, /* throwable= */ null);
      }
    }
  }

  /**
   * Logs a debug-level message with an optional associated {@link Throwable}.
   *
   * @param tag The tag of the message.
   * @param message The message.
   * @param throwable The {@link Throwable} associated with the message, or null if not specified.
   */
  @Pure
  public static void d(@Size(max = 23) String tag, String message, @Nullable Throwable throwable) {
    synchronized (lock) {
      if (logLevel == LOG_LEVEL_ALL) {
        logger.d(tag, message, throwable);
      }
    }
  }

  /**
   * Logs an information-level message.
   *
   * @param tag The tag of the message.
   * @param message The message.
   */
  @Pure
  public static void i(@Size(max = 23) String tag, String message) {
    synchronized (lock) {
      if (logLevel <= LOG_LEVEL_INFO) {
        logger.i(tag, message, /* throwable= */ null);
      }
    }
  }

  /**
   * Logs an information-level message with an optional associated {@link Throwable}.
   *
   * @param tag The tag of the message.
   * @param message The message.
   * @param throwable The {@link Throwable} associated with the message, or null if not specified.
   */
  @Pure
  public static void i(@Size(max = 23) String tag, String message, @Nullable Throwable throwable) {
    synchronized (lock) {
      if (logLevel <= LOG_LEVEL_INFO) {
        logger.i(tag, message, throwable);
      }
    }
  }

  /**
   * Logs a warning-level message.
   *
   * @param tag The tag of the message.
   * @param message The message.
   */
  @Pure
  public static void w(@Size(max = 23) String tag, String message) {
    synchronized (lock) {
      if (logLevel <= LOG_LEVEL_WARNING) {
        logger.w(tag, message, /* throwable= */ null);
      }
    }
  }

  /**
   * Logs a warning-level message with an optional associated {@link Throwable}..
   *
   * @param tag The tag of the message.
   * @param message The message.
   * @param throwable The {@link Throwable} associated with the message, or null if not specified.
   */
  @Pure
  public static void w(@Size(max = 23) String tag, String message, @Nullable Throwable throwable) {
    synchronized (lock) {
      if (logLevel <= LOG_LEVEL_WARNING) {
        logger.w(tag, message, throwable);
      }
    }
  }

  /**
   * Logs an error-level message.
   *
   * @param tag The tag of the message.
   * @param message The message.
   */
  @Pure
  public static void e(@Size(max = 23) String tag, String message) {
    synchronized (lock) {
      if (logLevel <= LOG_LEVEL_ERROR) {
        logger.e(tag, message, /* throwable= */ null);
      }
    }
  }

  /**
   * Logs an error-level message with an optional associated {@link Throwable}.
   *
   * @param tag The tag of the message.
   * @param message The message.
   * @param throwable The {@link Throwable} associated with the message, or null if not specified.
   */
  @Pure
  public static void e(@Size(max = 23) String tag, String message, @Nullable Throwable throwable) {
    synchronized (lock) {
      if (logLevel <= LOG_LEVEL_ERROR) {
        logger.e(tag, message, throwable);
      }
    }
  }

  /**
   * Returns a string representation of a {@link Throwable} suitable for logging, taking into
   * account whether {@link #setLogStackTraces(boolean)} stack trace logging} is enabled.
   *
   * <p>Stack trace logging may be unconditionally suppressed for some expected failure modes (e.g.,
   * {@link Throwable Throwables} that are expected if the device doesn't have network connectivity)
   * to avoid log spam.
   *
   * @param throwable The {@link Throwable}.
   * @return The string representation of the {@link Throwable}, or null if {@code throwable} is
   *     null.
   */
  @Nullable
  @Pure
  public static String getThrowableString(@Nullable Throwable throwable) {
    if (throwable == null) {
      return null;
    }
    synchronized (lock) {
      if (isCausedByUnknownHostException(throwable)) {
        // UnknownHostException implies the device doesn't have network connectivity.
        // UnknownHostException.getMessage() may return a string that's more verbose than desired
        // for
        // logging an expected failure mode. Conversely, android.util.Log.getStackTraceString has
        // special handling to return the empty string, which can result in logging that doesn't
        // indicate the failure mode at all. Hence we special case this exception to always return a
        // concise but useful message.
        return "UnknownHostException (no network)";
      } else if (!logStackTraces) {
        return throwable.getMessage();
      } else {
        return android.util.Log.getStackTraceString(throwable).trim().replace("\t", "    ");
      }
    }
  }

  /**
   * Appends the result of {@link #getThrowableString(Throwable)} (if non-empty) to {@code message}.
   */
  @Pure
  public static String appendThrowableString(String message, @Nullable Throwable throwable) {
    @Nullable String throwableString = getThrowableString(throwable);
    if (!TextUtils.isEmpty(throwableString)) {
      message += "\n  " + throwableString.replace("\n", "\n  ") + '\n';
    }
    return message;
  }

  @Pure
  private static boolean isCausedByUnknownHostException(@Nullable Throwable throwable) {
    while (throwable != null) {
      if (throwable instanceof UnknownHostException) {
        return true;
      }
      throwable = throwable.getCause();
    }
    return false;
  }
}