解决Spark应用日志级别设置

最近在研究Spark的相关知识,本地搭建了一个开发环境Windows7+Eclipse+JDK1.7。

一. 日志效率原因

开发时,控制台输出一大堆日志信息,严重影响查看日志效率。

从控制台输出日志我们可以看出,应用程序是默认加载Spark-core包下面的log4j-defaults.properties日志文件。查看log4j-defaults.properties文件

由上图可知,Spark-core包设置默认的日志级别为info,所以我们才看到一大堆日志信i息。
那针对以上问题,在开发过程中我们如何解决?

二. 日志级别解决方法

方式一.局部应用设置

针对SparkContext应用,Spark有专门的api设置日志级别,如下:
上述方法,只针对SparkContext相关的应用,而对Spark-streaming等应用无效果。

方式二.全局应用设置

针对spark所有应用,可以Java工程目录中新建/src/main/resources目录,把log4j.properties放置该目录。

log4j.properties生成:
1. Spark中conf默认配置文件是log4j.properties.template,可以将其改名为log4j.properties;
2. 将Spark-core包中的log4j-default.properties内容复制到log4j.properties文件。
#log4j内容如下
[html] view plain copy
  1. #  
  2. # Licensed to the Apache Software Foundation (ASF) under one or more  
  3. # contributor license agreements.  See the NOTICE file distributed with  
  4. # this work for additional information regarding copyright ownership.  
  5. # The ASF licenses this file to You under the Apache License, Version 2.0  
  6. # (the "License"); you may not use this file except in compliance with  
  7. # the License.  You may obtain a copy of the License at  
  8. #  
  9. #    http://www.apache.org/licenses/LICENSE-2.0  
  10. #  
  11. # Unless required by applicable law or agreed to in writing, software  
  12. # distributed under the License is distributed on an "AS IS" BASIS,  
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  14. # See the License for the specific language governing permissions and  
  15. # limitations under the License.  
  16. #  
  17.   
  18. # Set everything to be logged to the console  
  19. log4j.rootCategory=WARN, console  
  20. log4j.appender.console=org.apache.log4j.ConsoleAppender  
  21. log4j.appender.console.target=System.err  
  22. log4j.appender.console.layout=org.apache.log4j.PatternLayout  
  23. log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n  
  24.   
  25. # Settings to quiet third party logs that are too verbose  
  26. log4j.logger.org.spark-project.jetty=WARN  
  27. log4j.logger.org.spark-project.jetty.util.component.AbstractLifeCycle=ERROR  
  28. log4j.logger.org.apache.spark.repl.SparkIMain$exprTyper=INFO  
  29. log4j.logger.org.apache.spark.repl.SparkILoop$SparkILoopInterpreter=INFO  
  30. log4j.logger.org.apache.parquet=ERROR  
  31. log4j.logger.parquet=ERROR  
  32.   
  33. # SPARK-9183: Settings to avoid annoying messages when looking up nonexistent UDFs in SparkSQL with Hive support  
  34. log4j.logger.org.apache.hadoop.hive.metastore.RetryingHMSHandler=FATAL  
  35. log4j.logger.org.apache.hadoop.hive.ql.exec.FunctionRegistry=ERROR  
在开发工程中,我们可以设置日志级别为WARN,即:
log4j.rootCategory=WARN, console

三. 日志级别设置效果