从Apache Spark日志

时间:2017-04-25 03:17:24

标签: apache-spark logging

在我的spark应用程序中,我记录如下:

Logger log = spark.log();

log.info("**************************************************");
log.info("Pi is roughly " + 4.0 * count / n);
log.info("**************************************************");

但是,我的日志目前看起来像这样:

Apr 24, 2017 10:54:38 PM org.apache.spark.launcher.OutputRedirector redirect
INFO: [CJC]17/04/24 22:54:38 INFO SparkSession: **************************************************
Apr 24, 2017 10:54:38 PM org.apache.spark.launcher.OutputRedirector redirect
INFO: [CJC]17/04/24 22:54:38 INFO SparkSession: Pi is roughly 3.138936
Apr 24, 2017 10:54:38 PM org.apache.spark.launcher.OutputRedirector redirect
INFO: [CJC]17/04/24 22:54:38 INFO SparkSession: **************************************************

我希望它能够清理它,所以它看起来像这样:

INFO: [CJC]17/04/24 22:54:38 INFO SparkSession: **************************************************
INFO: [CJC]17/04/24 22:54:38 INFO SparkSession: Pi is roughly 3.138936
INFO: [CJC]17/04/24 22:54:38 INFO SparkSession: **************************************************

在这种情况下,log4j.properties文件似乎没有帮助我,因为似乎所有日志记录都通过OutputRedirector类。有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

tldr;

隐藏标题

Apr 24, 2017 10:54:38 PM org.apache.spark.launcher.OutputRedirector redirect

将此代码添加到您的代码中 System.setProperty("java.util.logging.SimpleFormatter.format","%5$s%6$s%n");


client模式下针对独立的Spark集群运行Spark使用两个日志记录框架。

    org.apache.spark.launcher.SparkLauncher使用的
  • java.util.logger
  • 绑定到slf4j的嵌入式驱动程序的日志记录框架。在我的情况下是log4j。

要控制嵌入驱动程序的日志记录格式,请使用以下
如先前答案中所述的系统属性:

  • spark.driver.extraJavaOptions=-Dlog4j.configuration=file:<path>/log4j.xml
  • spark.executor.extraJavaOptions=-Dlog4j.configuration=file:<path>/log4j.xml

禁止显示Apr 24, 2017 10:54:38 PM org.apache.spark.launcher.OutputRedirector redirect

  1. 将嵌入式驱动程序日志重定向到记录器


sparkLauncher.redirectToLog(createLogger("spark-pi"));

private java.util.logging.Logger createLogger(String appName) throws IOException {
    final java.util.logging.Logger logger = getRootLogger();
    final FileHandler handler = new FileHandler("./" + appName + "-%u-%g.log", 10_000_000, 5, true);
    handler.setFormatter(new SimpleFormatter());
    logger.addHandler(handler);
    logger.setLevel(Level.INFO);
    return logger;
}

private java.util.logging.Logger getRootLogger() {
    final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(java.util.logging.Logger.GLOBAL_LOGGER_NAME);
    Arrays.stream(logger.getHandlers()).forEach(logger::removeHandler);
    //Without this the logging will go to the Console and to a file.
    logger.setUseParentHandlers(false);
    return logger;
}

  1. System.setProperty("java.util.logging.SimpleFormatter.format","%5$s%6$s%n");

这将更改SimpleFormatter的默认日志记录模式。参见jdk docs