如何将消息写入AWS Glue上的输出日志?

时间:2018-02-21 19:51:49

标签: pyspark aws-glue

AWS Glue作业默认情况下将输出和错误记录到两个不同的CloudWatch日志/aws-glue/jobs/error/aws-glue/jobs/output。当我在脚本中包含print()语句进行调试时,它们会被写入错误日志(/aws-glue/jobs/error)。

我尝试过使用:

log4jLogger = sparkContext._jvm.org.apache.log4j 
log = log4jLogger.LogManager.getLogger(__name__) 
log.warn("Hello World!")

但是“Hello World!”不会出现在我运行的测试作业的任何一个日志中。

有谁知道如何将调试日志语句写入输出日志(/aws-glue/jobs/output)?

TIA!

编辑:

事实证明以上实际上确实有效。发生的事情是我在AWS Glue Script编辑器窗口中运行该工作,该窗口捕获Command-F组合键并仅在当前脚本中搜索。因此,当我尝试在页面内搜索日志记录输出时,似乎它没有被记录。

注意:我确实通过测试第一响应者的建议发现AWS Glue脚本似乎没有输出任何低于WARN的日志消息!

6 个答案:

答案 0 :(得分:6)

尝试使用logging模块中的内置python记录器,默认情况下它会将消息写入标准输出流。

import logging

MSG_FORMAT = '%(asctime)s %(levelname)s %(name)s: %(message)s'
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(format=MSG_FORMAT, datefmt=DATETIME_FORMAT)
logger = logging.getLogger(<logger-name-here>)

logger.setLevel(logging.INFO)

...

logger.info("Test log message")

答案 1 :(得分:1)

我遇到了同样的问题。我通过添加解决了 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))

在完全没有打印之前,甚至没有错误级别

这个主意是从这里得到的 https://medium.com/tieto-developers/how-to-do-application-logging-in-aws-745114ac6eb7

另一种选择是登录到stdout并将AWS日志记录粘贴到stdout(使用stdout实际上是云日志记录的最佳实践之一)。

更新:它仅适用于setLevel(“ WARNING”)以及在打印ERROR或WARING时起作用。我没有找到如何在INFO级别进行管理的地方:(

答案 2 :(得分:1)

我注意到以上答案都是用python编写的。对于Scala,您可以执行以下操作

import com.amazonaws.services.glue.log.GlueLogger

object GlueApp {
  def main(sysArgs: Array[String]) {
    val logger = new GlueLogger
    logger.info("info message")
    logger.warn("warn message")
    logger.error("error message")
  }
}

您可以从官方文档here中找到Python和Scala解决方案

答案 3 :(得分:1)

以防万一这有帮助。这可以更改日志级别。

sc = SparkContext()
sc.setLogLevel('DEBUG')
glueContext = GlueContext(sc)
logger = glueContext.get_logger()
logger.info('Hello Glue')

答案 4 :(得分:0)

我知道该文章不是新文章,但可能对某人有所帮助: 对我来说,登录胶水可以使用以下代码行:

# create glue context
glueContext = GlueContext(sc)
# set custom logging on
logger = glueContext.get_logger()
...
#write into the log file with:
logger.info("s3_key:" + your_value)

答案 5 :(得分:0)

这适用于 Glue Python 作业中的 INFO 级别:

import sys

root = logging.getLogger()
root.setLevel(logging.DEBUG)

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
root.info("check")

source

相关问题