python - 如何从 Databricks 中的 Python 笔记本创建日志记录到 ADLS Gen2(无需安装)?
问题描述
我正在尝试在 Databricks Python 笔记本中创建一个日志记录机制。尝试使用下面的代码来实现相同的 -
import logging
def create_logger(name,log_path=None):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(levelname)-8s - %(message)s")
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
if log_path is not None:
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
return logger
但是,每当我尝试调用如下函数时 -
from datetime import date, datetime
current_date = date.today()
current_timestamp = datetime.strftime(datetime.now(),"%Y%m%d%H%M%S")
name = "temp_logs"
log_path = f"abfss://{storageContainer}@{storageAccount}.dfs.core.windows.net/{target_dir}/logs/{current_date}/{name}_{current_timestamp}.txt"
logger = create_logger(name = name,log_path = log_path)
这给出了错误 -
[Errno 2] No such file or directory: /databricks/driver/abfss:/temp-ontainer@teststorage.dfs.core.windows.net/test/logs/2021-09-13/temp_logs_20210913101150.txt'
有没有办法处理这个(不使用挂载点位置)?
解决方案
我们可以尝试通过导入“BlobStorageRotatingFileHandler”来使用它,如下所示:
from azure_storage_logging.handlers import BlobStorageRotatingFileHandler
可以参考有关 azure-storage-logging 的 python 文档,因为它提供了将标准 Python 日志记录 API 的输出发送到 Microsoft Azure 存储的功能。
示例代码如下:
import logging
from azure_storage_logging.handlers import TableStorageHandler
# configure the handler and add it to the logger
logger = logging.getLogger('example')
handler = TableStorageHandler(account_name='mystorageaccountname',
account_key='mystorageaccountkey',
extra_properties=('%(hostname)s',
'%(levelname)s'))
logger.addHandler(handler)
# output log messages
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
使用上述日志记录错误信息。
对于“找不到目录”,我们需要检查路径。请查看文档
推荐阅读
- android - Android 键盘未显示(不适用于任何应用程序)
- asp.net-core - AKKA.NET:可以在多个正在运行的应用程序上创建唯一的演员吗?
- neural-network - 用于识别 0 到 20 的语音数字的深度神经网络
- bash - Makefile 中的 ${foobar} 和 $(foobar) 有什么区别?
- pyspark - 如何在 EMR 笔记本中安装 .jar 依赖项?
- python - 获取此 POST 方法 json API 结果的正确方法是什么?
- testing - 在 Katalon Studio 移动测试中,我在定位一个元素“org.openqa.selenium.InvalidSelectorException”时遇到了这个错误
- javascript - 选择前端元素后从数据库中获取对应的值
- powerbi - if 语句之间的 DAX 日期
- html - 如何从下拉菜单中获取选项(每个可能的选择)以嵌入图像