I am very confused with setting up logging with Apache Spark. Apache spark used Log4j for logging and it generates huge amount of log data. Is there a way to setup log4j for spark logs and use logback for application log. I am quite conversant with logback but it seems spark only support log4j. Below piece of code was working fine till i introduced apache spark. Any help in this regard will be helpful.
import com.typesafe.scalalogging.LazyLogging
import scala.util.{Failure, Success}
import scala.xml.{Elem, XML}
object MainApp extends App with LazyLogging {
val currency = new YahooCurrencyLoader() with CurrencyParameters
val ccy = currency.getXML(currency.ccyUrl) match {
case Success(v) => XML.save("PreviousRun.xml",v); logger.info("XML has been saved for use")
case Failure(ex) => logger.error("XML extraction failed. Look at Yahoo extraction class. ${ex.getMessage}" )
}
val xmllocation: String = "./PreviousRun.xml"
val loadxml: Elem = XML.loadFile(xmllocation)
//print(loadxml)
//print(currency.findCurrency(loadxml,"GBP"))
logger.info("USD CAD Cross is " + currency.findCurrency(loadxml,"CAD").head)
I have used the following imports:
Sample code as shown below.
And you are sorted.
I do not know if you use
sbt
ormaven
but that is where it should all start. Myself I usesbt
so I will give you an example how we have solved this problem.1. Apache Spark uses log4j 1.2.xx
That is true and it is really problematic if you do not want to use the same logging implementation.But there is help!
First, exclude the following libs from
spark
dependencies:log4j
slf4j-log4j12
For
sbt
(usingsbt-assembly
) it looks like this:2. Redirect log4j logging to slf4j
A detailed description can be found here: https://www.slf4j.org/legacy.html
And the module that is in our interest is:
log4j-over-slf4j
So we can have all the logs redirected back to
slf4j
from where some other logging implementation could pick it up.Easy, simply add this dependency to your application
3. Add desired logging implementation
In our case it was (like yours)
logback
, so we added it as dependency:Add some
logback.xml
configuration to your classpath, for example insrc/main/resources
and enjoy!spark-submit
If you need help using
Logback
while deploying your app withspark-submit
please follow this answer: https://stackoverflow.com/a/45480145/1549135