Connecting Log4j to ipython notebook stderr in a java/python project using py4j

走远了吗. 提交于 2019-12-11 08:25:21

问题


I have a project that uses java, scala and Apache Spark to do distributed computations on genomic data. Using py4j and mimicking the PySpark model, we expose a python API that calls into the JVM. Our goal has been to bring this model into jupyter notebooks, which has been pretty easy so far, with one lingering problem: logging.

The problem

We (and Spark) use log4j to write log messages to a log file and stderr. This stderr is the stderr for the java process, so if I run two commands from the jupyter notebook:

print('foo')
info('bar')  # calls log4j logger.info in JVM

I see 'foo' written to the jupyter cell, but 'bar' is written to the terminal running the jupyter process.

My goal

Connect log4j to the jupyter notebook so that log4j messages are written to jupyter cells, instead of the terminal.

What I've tried

The java log4j.ConsoleAppender is writing to the java stderr. So, we're going to need to route the java stderr through jupyter somehow, right? This may involve using System.setOut(...) with a PrintStream object hooked up to the jupyter process, but I'm not yet sure how to do that.


回答1:


We solved this by using a separate socket to communicate between Java and Python. Here's the commit diff: https://github.com/hail-is/hail/commit/93d7e95a82ab39501eede7ecb301538bcd013ea8



来源:https://stackoverflow.com/questions/41885220/connecting-log4j-to-ipython-notebook-stderr-in-a-java-python-project-using-py4j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!