如何在IntelliJ中控制Spark日志记录

4

我通常使用JUnit来运行程序,但我也尝试过从main运行,但没有任何区别。


我已经阅读了将近两打的SO问题、博客文章和文章,并尝试了几乎所有方法来让Spark减少日志记录。


我尝试过以下方法:

  • 在资源文件夹(srctest)中添加log4j.properties
  • 使用spark-submit添加log4j.properties,但返回"error: missing application resources"
  • Logger.getLogger("com").setLevel(Level.WARN);
  • Logger.getLogger("org").setLevel(Level.WARN);
  • Logger.getLogger("akka").setLevel(Level.WARN);
  • Logger.getRootLogger().setLevel(Level.WARN);
  • spark.sparkContext().setLogLevel("WARN");

  • 在另一个项目中,我通过以下方式消除了日志:

    Logger.getLogger("org").setLevel(Level.WARN);
    Logger.getLogger("akka").setLevel(Level.WARN);
    

    但这里它无法正常工作。


    我是如何创建我的SparkSession的:

    SparkSession spark = SparkSession
        .builder()
        .appName("RS-LDA")
        .master("local")
        .getOrCreate();
    


    如果你想查看更多我的代码,请告诉我。

    谢谢


    1
    你尝试过更改log4j-defaults.properties中Spark的默认值吗?这个默认属性文件通常位于你的Spark安装路径(/etc/spark2/...)。 - Michael Heil
    2个回答

    0

    我正在使用IntelliJ和Spark,这对我很有效:

    Logger.getRootLogger.setLevel(Level.ERROR)
    

    您也可以更改Log Spark配置。

    $ cd SPARK_HOME/conf
    $ gedit log4j.properties.template
    
    # find this lines in the file
    # Set everything to be logged to the console
    log4j.rootCategory=INFO, console
    
    and change to ERROR
    
    log4j.rootCategory=ERROR, console
    
    In this file you have other options tho change too
    
    # Set the default spark-shell log level to WARN. When running the spark-shell, the
    # log level for this class is used to overwrite the root logger's log level, so that
    # the user can have different defaults for the shell and regular Spark apps.
    log4j.logger.org.apache.spark.repl.Main=WARN
    
    # Settings to quiet third party logs that are too verbose
    .....
    
    And finally rename the log4j.properties.template file
     $ mv log4j.properties.template log4j.properties
    

    您可以通过以下链接进行进一步配置:

    使用Log4j在Spark中记录日志

    或者也可以使用以下链接:

    使用Log4j在Spark中记录日志。如何为YARN集群模式自定义驱动程序和执行器。


    0

    这可能是一个老问题,但我刚遇到了同样的问题。 为了解决它,我所做的是:

    1. private static Logger log = LoggerFactory.getLogger(Spark.class);
      添加为该类的字段。
    2. 在创建Spark会话后,加入 spark.sparkContext().setLogLevel("WARN");

    步骤2只有在步骤1之后才能起作用。


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接