

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Konfigurasikan properti Apache Log4j2 untuk Amazon EMR Tanpa Server
<a name="log4j2"></a>

Halaman ini menjelaskan cara mengonfigurasi [properti Apache Log4j 2.x](https://logging.apache.org/log4j/2.x/) kustom untuk pekerjaan EMR Tanpa Server di. `StartJobRun` Jika Anda ingin mengonfigurasi klasifikasi Log4j di tingkat aplikasi, lihat. [Konfigurasi aplikasi default untuk EMR Tanpa Server](default-configs.md)

## Konfigurasikan properti Spark Log4j2 untuk Amazon EMR Tanpa Server
<a name="log4j2-spark"></a>

Dengan Amazon EMR merilis 6.8.0 dan yang lebih tinggi, Anda dapat menyesuaikan properti [Apache Log4j 2.x untuk menentukan konfigurasi log](https://logging.apache.org/log4j/2.x/) berbutir halus. Ini menyederhanakan pemecahan masalah pekerjaan Spark Anda di EMR Tanpa Server. Untuk mengkonfigurasi properti ini, gunakan `spark-driver-log4j2` dan `spark-executor-log4j2` klasifikasi.

**Topics**
+ [Klasifikasi Log4j2 untuk Spark](#log4j2-spark-class)
+ [Contoh konfigurasi Log4j2 untuk Spark](#log4j2-spark-example)
+ [Log4j2 dalam contoh pekerjaan Spark](#log4j2-spark-jobs)
+ [Pertimbangan Log4j2 untuk Spark](#log4j2-spark-considerations)

### Klasifikasi Log4j2 untuk Spark
<a name="log4j2-spark-class"></a>

Untuk menyesuaikan konfigurasi log Spark, gunakan klasifikasi berikut dengan. [https://docs.aws.amazon.com/emr-serverless/latest/APIReference/API_ConfigurationOverrides.html#emrserverless-Type-ConfigurationOverrides-applicationConfiguration](https://docs.aws.amazon.com/emr-serverless/latest/APIReference/API_ConfigurationOverrides.html#emrserverless-Type-ConfigurationOverrides-applicationConfiguration) Untuk mengkonfigurasi properti Log4j 2.x, gunakan yang berikut ini. [https://docs.aws.amazon.com/emr-serverless/latest/APIReference/API_Configuration.html#emrserverless-Type-Configuration-properties](https://docs.aws.amazon.com/emr-serverless/latest/APIReference/API_Configuration.html#emrserverless-Type-Configuration-properties)

**`spark-driver-log4j2`**  
Klasifikasi ini menetapkan nilai dalam `log4j2.properties` file untuk driver.

**`spark-executor-log4j2`**  
Klasifikasi ini menetapkan nilai dalam `log4j2.properties` file untuk pelaksana.

### Contoh konfigurasi Log4j2 untuk Spark
<a name="log4j2-spark-example"></a>

Contoh berikut menunjukkan cara mengirimkan pekerjaan Spark dengan untuk menyesuaikan konfigurasi Log4j2 `applicationConfiguration` untuk driver dan eksekutor Spark.

Untuk mengonfigurasi klasifikasi Log4j di tingkat aplikasi, bukan saat Anda mengirimkan pekerjaan, lihat. [Konfigurasi aplikasi default untuk EMR Tanpa Server](default-configs.md)

```
aws emr-serverless start-job-run \
    --application-id application-id \
    --execution-role-arn job-role-arn \
    --job-driver '{
        "sparkSubmit": {
            "entryPoint": "/usr/lib/spark/examples/jars/spark-examples.jar",
            "entryPointArguments": ["1"],
            "sparkSubmitParameters": "--class org.apache.spark.examples.SparkPi --conf spark.executor.cores=4 --conf spark.executor.memory=20g --conf spark.driver.cores=4 --conf spark.driver.memory=8g --conf spark.executor.instances=1"
        }
    }'
    --configuration-overrides '{
        "applicationConfiguration": [
             {
                "classification": "spark-driver-log4j2",
                "properties": {
                    "rootLogger.level":"error", // will only display Spark error logs
                    "logger.IdentifierForClass.name": "classpath for setting logger",
                    "logger.IdentifierForClass.level": "info"
                   
                }
            },
            {
                "classification": "spark-executor-log4j2",
                "properties": {
                    "rootLogger.level":"error", // will only display Spark error logs
                    "logger.IdentifierForClass.name": "classpath for setting logger",
                    "logger.IdentifierForClass.level": "info"
                }
            }
       ]
    }'
```

### Log4j2 dalam contoh pekerjaan Spark
<a name="log4j2-spark-jobs"></a>

Contoh kode berikut menunjukkan cara membuat aplikasi Spark saat Anda menginisialisasi konfigurasi Log4j2 kustom untuk aplikasi.

------
#### [ Python ]

**Example - Menggunakan Log4j2 untuk pekerjaan Spark dengan Python**  

```
import os
import sys

from pyspark import SparkConf, SparkContext
from pyspark.sql import SparkSession

app_name = "PySparkApp"
if __name__ == "__main__":
    spark = SparkSession\
        .builder\
        .appName(app_name)\
        .getOrCreate()
    
    spark.sparkContext._conf.getAll()
    sc = spark.sparkContext
    log4jLogger = sc._jvm.org.apache.log4j
    LOGGER = log4jLogger.LogManager.getLogger(app_name)

    LOGGER.info("pyspark script logger info")
    LOGGER.warn("pyspark script logger warn")
    LOGGER.error("pyspark script logger error")
    
    // your code here
    
    spark.stop()
```
Untuk menyesuaikan Log4j2 untuk driver saat Anda menjalankan pekerjaan Spark, gunakan konfigurasi berikut:  

```
{
   "classification": "spark-driver-log4j2",
      "properties": {
          "rootLogger.level":"error", // only display Spark error logs
          "logger.PySparkApp.level": "info", 
          "logger.PySparkApp.name": "PySparkApp"
      }
}
```

------
#### [ Scala ]

**Example - Menggunakan Log4j2 untuk pekerjaan Spark dengan Scala**  

```
import org.apache.log4j.Logger
import org.apache.spark.sql.SparkSession

object ExampleClass {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession
    .builder
    .appName(this.getClass.getName)
    .getOrCreate()

    val logger = Logger.getLogger(this.getClass);
    logger.info("script logging info logs")
    logger.warn("script logging warn logs")
    logger.error("script logging error logs")

// your code here
    spark.stop()
  }
}
```
Untuk menyesuaikan Log4j2 untuk driver saat Anda menjalankan pekerjaan Spark, gunakan konfigurasi berikut:  

```
{
   "classification": "spark-driver-log4j2",
      "properties": {
          "rootLogger.level":"error", // only display Spark error logs
          "logger.ExampleClass.level": "info", 
          "logger.ExampleClass.name": "ExampleClass"
      }
}
```

------

### Pertimbangan Log4j2 untuk Spark
<a name="log4j2-spark-considerations"></a>

Properti Log4j2.x berikut tidak dapat dikonfigurasi untuk proses Spark:
+ `rootLogger.appenderRef.stdout.ref`
+ `appender.console.type`
+ `appender.console.name`
+ `appender.console.target`
+ `appender.console.layout.type`
+ `appender.console.layout.pattern`

[Untuk informasi rinci tentang properti Log4j2.x yang dikonfigurasi, lihat file di. `log4j2.properties.template`](https://github.com/apache/spark/blob/v3.3.0/conf/log4j2.properties.template) GitHub