首页 > 解决方案 > 如何在 IntelliJ 中控制 Spark 日志记录

问题描述

我通常从 JUnit 运行东西,但我也尝试过运行main它并没有什么区别。


我已经阅读了近两打 SO 问题、博客文章和文章,并尝试了几乎所有方法来让 Spark 停止记录这么多。


我尝试过的事情:

  • log4j.properties在资源文件夹中(在srctest
  • 用于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();
    


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

    谢谢

    标签: javaapache-sparkintellij-idealog4j

    解决方案


    我正在使用 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 集群模式的驱动程序和执行程序。


    推荐阅读