spark代码中添加logger_Spark的日志配置
在测试spark计算时,将作业提交到yarn(模式–master yarn-cluster)上,想查看print到控制台这是imposible的,因为作业是提交到yarn的集群上,so 去yarn集群上看日志是很麻烦的,但有特别想看下print的信息,方便调试或者别的目的
在Spark的conf目录下,把log4j.properties.template修改为log4j.properties,原来的内容如下:
#Set everything to be logged to the console
log4j.rootCategory=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n
#Settings to quiet third party logs that are too verbose
log4j.logger.org.spark-project.jetty=WARN
log4j.logger.org.spark-project.jetty.util.component.AbstractLifeCycle=ERROR
log4j.logger.org.apache.spark.repl.SparkIMain$exprTyper=INFO
log4j.logger.org.apache.spark.repl.SparkILoop$SparkILoopInterpreter=INFO
把log4j.rootCategory=INFO, console改为log4j.rootCategory=WARN, console即可抑制Spark把INFO级别的日志打到控制台上。如果要显示全面的信息,则把INFO改为DEBUG。
如果希望一方面把代码中的println打印到控制台,另一方面又保留spark 本身输出的日志,可以将它输出到日志文件中
log4j.rootCategory=INFO, console,FILE
# Settings to quiet third party logs that are too verbose
log4j.logger.org.eclipse.jetty=WARN
log4j.logger.org.eclipse.jetty.util.component.AbstractLifeCycle=ERROR
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.Threshold=DEBUG
log4j.appender.FILE.file=/home/hadoop/spark.log
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[%-5p] [%d{yyyy-MM-dd HH:mm:ss}] [%C{1}:%M:%L] %m%n
# spark
log4j.logger.org.apache.spark=INFO
上面的操作,spark的日志一方面打印到控制台,一方面写入到/home/hadoop/spark.log中了,这是日志的继承特性,后面再来改进,目前把log4j.rootCategory=INFO, console,FILE改为log4j.rootCategory=INFO, FILE即可