pyspark日志:在错误的日志级别打印信息

yuvru6vn  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(999)

谢谢你的时间!
我想在调试代码时创建并打印我的(大量)数据的清晰摘要到我的输出,但是一旦完成就停止创建和打印这些摘要以加快速度。有人建议我使用我实现的日志记录。它可以像预期的那样将文本字符串作为消息打印到输出中—但是,当打印Dataframe的摘要时,它似乎忽略了日志级别,创建它们并始终打印它们。
日志记录是正确的,还是有更好的方法?我可以#阻止代码行或使用if语句等,但这是一个庞大的代码,我知道我需要在未来做进一步的元素添加相同的检查-似乎正是什么日志应该工作。

  1. from pyspark.sql.functions import col,count
  2. import logging
  3. logging.basicConfig(level=logging.INFO)
  4. logger = logging.getLogger(__name__)
  5. df = spark.createDataFrame([(1,2),(3,4)],["COLA","COLB"])
  6. print "1"
  7. logger.setLevel(logging.DEBUG)
  8. logger.debug("1 - DEBUG - Print the message and show the table")
  9. logger.debug(df.show())
  10. print "2"
  11. logger.setLevel(logging.INFO)
  12. logger.debug("2 - INFO - Don't print the message or show the table")
  13. logger.debug(df.show())
  14. print "3"
  15. logger.setLevel(logging.INFO)
  16. logger.debug("3 - INFO - Don't print the message or show the collected data")
  17. logger.debug(df.collect())
  18. print "4"
  19. logger.setLevel(logging.DEBUG)
  20. logger.debug("4 - DEBUG - Print the message and the collected data")
  21. logger.debug(df.collect())

输出:

  1. 1
  2. DEBUG:__main__:1 - DEBUG - Print the message and show the table
  3. +----+----+
  4. |COLA|COLB|
  5. +----+----+
  6. | 1| 2|
  7. | 3| 4|
  8. +----+----+
  9. DEBUG:__main__:None
  10. 2
  11. +----+----+
  12. |COLA|COLB|
  13. +----+----+
  14. | 1| 2|
  15. | 3| 4|
  16. +----+----+
  17. 3
  18. 4
  19. DEBUG:__main__:4 - DEBUG - Print the message and the collected data
  20. DEBUG:__main__:[Row(COLA=1, COLB=2), Row(COLA=3, COLB=4)]
zour9fqk

zour9fqk1#

如果我们使用 df.show() (或) df.collect() 那么,即使它们处于 logger.debug .
如果我们将日志级别设置为 DEBUG 然后我们就可以看到了 INFO 水平测井。
如果我们将日志级别设置为 INFO 那我们就看不见了 DEBUG 水平测井。
您可以通过存储 collect()/take(n) 将结果转换为变量,然后在日志中使用该变量。

  1. from pyspark.sql.functions import col,count
  2. import logging
  3. logging.basicConfig(level=logging.INFO)
  4. logger = logging.getLogger(__name__)
  5. df = spark.createDataFrame([(1,2),(3,4)],["COLA","COLB"])
  6. # storing results but don't use collect on huge dataset instead use `.take`
  7. res=df.collect()
  8. # get 10 records from df
  9. res=df.take(10)
  10. print "1"
  11. # 1
  12. logger.setLevel(logging.DEBUG)
  13. logger.debug("1 - DEBUG - Print the message and show the table")
  14. # DEBUG:__main__:1 - DEBUG - Print the message and show the table
  15. logger.debug(res)
  16. # DEBUG:__main__:[Row(COLA=1, COLB=2), Row(COLA=3, COLB=4)]
  17. print "2"
  18. # 2
  19. logger.setLevel(logging.INFO)
  20. logger.debug("2 - INFO - Don't print the message or show the table")
  21. logger.debug(res) #this won't print as loglevel is INFO.
  22. logger.info("result: " + str(res)) #this will get printed out
  23. # INFO:__main__:result: [Row(COLA=1, COLB=2), Row(COLA=3, COLB=4)]

使用 .take 而不是 .collect() .

展开查看全部

相关问题