问题描述
我与Apache Spark设置记录非常困惑. Apache Spark使用Log4J进行日志记录,并生成大量日志数据. 有没有办法设置log4j以进行火花日志,并使用日志返还进行应用程序日志.我非常熟悉记录,但似乎 spark仅支持log4j. 下面的代码工作正常,直到我引入了Apache Spark.在这方面的任何帮助都会有所帮助.
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)
推荐答案
我不知道您是否使用sbt或maven,但这都是应该开始的.我自己使用sbt,所以我将举一个例子,我们如何解决此问题.
- apache spark使用log4j 1.2.xx
是真的,如果您不想使用相同的日志记录实现,这确实是有问题的.但是有帮助!
首先,从spark依赖项中排除以下液体:
- log4j
- slf4j-log4j12
for sbt(使用sbt-assembly)看起来像这样:
lazy val spark16 = Seq("spark-core", "spark-sql", "spark-hive") .map("org.apache.spark" %% _ % "1.6.1") .map(_.excludeAll( ExclusionRule(name = "log4j"), ExclusionRule(name = "slf4j-log4j12") ))
- 重定向log4j记录到slf4j
可以在此处找到详细的描述: https://www.slf4j.org/legacy.html
符合我们利益的模块是:log4j-over-slf4j
log4j-over-slf4j模块包含最广泛使用的log4j类的替代品,即org.apache.log4j.category,org.apache.log4j.logger,org.apache.log4j.priority,org.apache.apache.log.log.log4j.level, org.apache.log4j.mdc和org.apache.log4j.basicconfigurator. 这些替换类将所有工作重定向到相应的SLF4J类.
因此,我们可以将所有日志重定向到slf4j,从其他一些日志记录实现中可以拾取它.
简单,只需将此依赖项添加到您的应用程序
"org.slf4j" % "log4j-over-slf4j" % "1.7.25"
- 添加所需的记录实现
在我们的情况下,它是(像您的)logback,因此我们将其添加为依赖性:
"ch.qos.logback" % "logback-classic" % "1.2.3"
在您的类路径中添加一些logback.xml配置,例如在src/main/resources中享受!
Spark-Submit
如果您需要使用Logback的帮助,则在使用spark-submit部署应用程序时,请按照以下答案: https://stackoverflow. com/a/45480145/1549135
其他推荐答案
我使用了以下导入:
import org.slf4j.Logger import org.slf4j.LoggerFactory
示例代码如下所示.
Object SparkCode { val logger = LoggerFactory.getLogger(this.getClass.getName) def main(args: Array[String]) { logger.info("Connection Started . ") // Create Spark Context and go on.. } }
您进行了排序.
问题描述
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 do not know if you use sbt or maven but that is where it should all start. Myself I use sbt so I will give you an example how we have solved this problem.
- 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 (using sbt-assembly) it looks like this:
lazy val spark16 = Seq("spark-core", "spark-sql", "spark-hive") .map("org.apache.spark" %% _ % "1.6.1") .map(_.excludeAll( ExclusionRule(name = "log4j"), ExclusionRule(name = "slf4j-log4j12") ))
- 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
The log4j-over-slf4j module contains replacements of most widely used log4j classes, namely org.apache.log4j.Category, org.apache.log4j.Logger, org.apache.log4j.Priority, org.apache.log4j.Level, org.apache.log4j.MDC, and org.apache.log4j.BasicConfigurator. These replacement classes redirect all work to their corresponding SLF4J classes.
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
"org.slf4j" % "log4j-over-slf4j" % "1.7.25"
- Add desired logging implementation
In our case it was (like yours) logback, so we added it as dependency:
"ch.qos.logback" % "logback-classic" % "1.2.3"
Add some logback.xml configuration to your classpath, for example in src/main/resources and enjoy!
spark-submit
If you need help using Logback while deploying your app with spark-submit please follow this answer: https://stackoverflow.com/a/45480145/1549135
其他推荐答案
I have used the following imports:
import org.slf4j.Logger import org.slf4j.LoggerFactory
Sample code as shown below.
Object SparkCode { val logger = LoggerFactory.getLogger(this.getClass.getName) def main(args: Array[String]) { logger.info("Connection Started . ") // Create Spark Context and go on.. } }
And you are sorted.