package internal
Type Members
- trait LogKey extends AnyRef
All structured logging
keysused inMDCmust be extendsLogKeyAll structured logging
keysused inMDCmust be extendsLogKeyLogKeys serve as identifiers for mapped diagnostic contexts (MDC) within logs. Follow these guidelines when adding a new LogKey:- Define all structured logging keys in
LogKeys.java, and sort them alphabetically for ease of search.- Use
UPPER_SNAKE_CASEfor key names.- Key names should be both simple and broad, yet include specific identifiers like
STAGE_ID,TASK_ID, andJOB_IDwhen needed for clarity. For instance, useMAX_ATTEMPTSas a general key instead of creating separate keys for each scenario such asEXECUTOR_STATE_SYNC_MAX_ATTEMPTSandMAX_TASK_FAILURES. This balances simplicity with the detail needed for effective logging.- Use abbreviations in names if they are widely understood, such as
APP_IDfor APPLICATION_ID, andK8Sfor KUBERNETES.- For time-related keys, use milliseconds as the unit of time.
- sealed final class LogKeys extends Enum[LogKeys] with LogKey
Various keys used for mapped diagnostic contexts(MDC) in logging.
Various keys used for mapped diagnostic contexts(MDC) in logging. All structured logging keys should be defined here for standardization.
- final class MDC extends Record
Mapped Diagnostic Context (MDC) that will be used in log messages.
Mapped Diagnostic Context (MDC) that will be used in log messages. The values of the MDC will be inline in the log message, while the key-value pairs will be part of the ThreadContext.
- class SparkLogger extends AnyRef
Guidelines for the Structured Logging Framework - Java Logging
Guidelines for the Structured Logging Framework - Java Logging
Use the
org.apache.spark.internal.SparkLoggerFactoryto get the logger instance in Java code: Getting Logger Instance: Instead of usingorg.slf4j.LoggerFactory, useorg.apache.spark.internal.SparkLoggerFactoryto ensure structured logging.import org.apache.spark.internal.SparkLogger; import org.apache.spark.internal.SparkLoggerFactory; private static final SparkLogger logger = SparkLoggerFactory.getLogger(JavaUtils.class);
Logging Messages with Variables: When logging messages with variables, wrap all the variables with
MDCs and they will be automatically added to the Mapped Diagnostic Context (MDC).import org.apache.spark.internal.LogKeys; import org.apache.spark.internal.MDC; logger.error("Unable to delete file for partition {}", MDC.of(LogKeys.PARTITION_ID, i));
Constant String Messages: For logging constant string messages, use the standard logging methods.
logger.error("Failed to abort the writer after failing to write map output.", e);
If you want to output logs in
java codethrough the structured log framework, you can definecustom LogKeyand use it injavacode as follows:// Add a
CustomLogKeys, implementLogKeypublic enum CustomLogKeys implements LogKey { CUSTOM_LOG_KEY } import org.apache.spark.internal.MDC; logger.error("Unable to delete key {} for cache", MDC.of(CUSTOM_LOG_KEY, "key")); - class SparkLoggerFactory extends AnyRef