我正在尝试为一些Jupyter Notebook代码(运行Pyspark3)添加日志记录。
在Stack Overflow上查找后,我发现有些答案说使用basicConfig()
无效,因为Notebook会启动自己的日志记录会话。有些变通的答案指出可以运行reload(logging)
来解决这个问题。基于这个想法,我设置我的日志记录如下:
from importlib import reload # Not needed in Python 2
import logging
reload(logging)
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
level=logging.INFO,
datefmt="%y/%m/%d %H:%M:%S",
)
logger = logging.getLogger(__name__)
然后我运行了一个信息语句:logger.info("this is a test")
,但出现了 I/O 值错误?我不确定这是什么意思。
--- Logging error ---
Traceback (most recent call last):
File "/usr/lib64/python3.6/logging/__init__.py", line 994, in emit
stream.write(msg)
File "/tmp/2950371398694308674", line 534, in write
super(UnicodeDecodingStringIO, self).write(s)
ValueError: I/O operation on closed file
Call stack:
File "/tmp/2950371398694308674", line 700, in <module>
sys.exit(main())
File "/tmp/2950371398694308674", line 672, in main
response = handler(content)
File "/tmp/2950371398694308674", line 318, in execute_request
result = node.execute()
File "/tmp/2950371398694308674", line 229, in execute
exec(code, global_dict)
File "<stdin>", line 1, in <module>
Message: 'this is a test'
Arguments: ()
这与记录与stdout/stderr的交互有关,但我不确定如何解决。
logging
不兼容所致。我正在尝试捕获并忽略该错误,因为日志记录确实发生了。如果成功,我会发布代码。 - Marc Maxmeister