我想查看我在 AWS Sagemaker JupyterLab 笔记本(使用 Glue 开发终端节点)内创建的自定义日志。我想将它们视为笔记本单元的输出。
我尝试过:
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.info("I want to see it as output in my notebook cell.")
还有:
sc = SparkContext.getOrCreate()
glueContext = GlueContext(sc)
logger = glueContext.get_logger()
logger.info("I want to see it as output in my notebook cell.")
但是两者都没有给出输出,而
print("Hi")
则在笔记本上正确给出了“Hi”输出。
虽然我想要笔记本本身上的日志,但我也检查了它们是否可以作为 CloudWatch Logs 使用,但是我的笔记本的日志看起来像
[W 10:42:34.540 NotebookApp] zmq message arrived on closed channel
(无论如何,我希望它们在笔记本上,而不是在云观察)。
非常感谢
import sys
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.info('that should work :-)')
我使用此代码将消息从 jupyter 笔记本实例记录到 cloudwatch。
pip install watchtower
import logging
import boto3
from botocore.exceptions import NoCredentialsError
from watchtower import CloudWatchLogHandler
# Configure the logger
logger = logging.getLogger('my_logger')
logger.setLevel(logging.INFO)
# Create a CloudWatch Logs handler
client = boto3.client('logs', region')
log_group_name = 'log_name'
log_stream_name = 'log_stream_name'
handler = boto3.session.Session().get_credentials()
handler = CloudWatchLogHandler(log_group_name, log_stream_name, client)
# Add the CloudWatch Logs handler to the logger
logger.addHandler(handler)
# Log a message
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')