解決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內容如下
  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

三. 日誌級別設置效果


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章