I have a Scala Maven project using Spark and am trying to log in using Logback. I compile my application into a jar and deploy it to an EC2 instance where the Spark distribution is installed. My pom.xml includes dependencies for Spark and Logback as follows:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
When I submit my Spark application, I print the slf4j binding on the command line. If I execute jars code using java, the binding will be to Logback. However, if I use Spark (e.g. spark-submit), binding to log4j.
val logger: Logger = LoggerFactory.getLogger(this.getClass)
val sc: SparkContext = new SparkContext()
val rdd = sc.textFile("myFile.txt")
val slb: StaticLoggerBinder = StaticLoggerBinder.getSingleton
System.out.println("Logger Instance: " + slb.getLoggerFactory)
System.out.println("Logger Class Type: " + slb.getLoggerFactoryClassStr)
gives
Logger Instance: org.slf4j.impl.Log4jLoggerFactory@a64e035
Logger Class Type: org.slf4j.impl.Log4jLoggerFactory
, log4j-1.2.17.jar slf4j-log4j12-1.7.16.jar /usr/local/spark/jars, Spark, , , pom.xml, , , ClassNotFoundException spark-submit.
: , Logback, Spark. Spark STDOUT.