使用Python Logging模块在Databricks中将日志写入Azure数据湖不起作用。

14

我正在尝试在Databricks中的Python-Notebook中使用Python logging模块将自己的日志文件写入Azure Datalake Gen 2。但是,我无法让它工作。虽然文件夹已创建,但没有包含日志内容的文件被创建。即使文件存在,也没有任何东西被写入其中。本地Python脚本可以正常工作,但我无法在Databricks中让它正常工作。以下是我的代码:

# mount
if not any(mount.mountPoint == '/mnt/log' for mount in dbutils.fs.mounts()):
  dbutils.fs.mount(
    source = "abfss://log@datalake.dfs.core.windows.net/",
    mount_point = "/mnt/log",
    extra_configs = configs)

# vars
folder_log = '/mnt/log/test/2019'
file_log = '201904.log'

# add folder if not existent
dbutils.fs.mkdirs(folder_log)

# setup logging
import logging
logging.basicConfig(
  filename=folder_log+'/'+file_log,
  format='%(asctime)s | %(name)s | %(levelname)s | %(message)s',
  datefmt='%Y-%m-%d %H:%M:%S UTC (%z)',
  level=logging.NOTSET
)

# test
logging.info('Hello World.')

安装看起来没问题。

使用dbutils添加和写入文件也很好:

dbutils.fs.put(folder_log+'/'+file_log, 'Hello World.')

像这样写入文件也可以:

f = open('/dbfs/mnt/log/test/2019/201904.log', 'w+')
f.write("This is line %d\r\n")
f.close()

也尝试将 "dbfs" 添加到路径中

filename='/dbfs'+folder_log+'/'+file_log,

有什么想法吗?


这个问题有任何更新吗? - benjamin
2
Databricks 对内置的 Python 日志记录模块和 getLogger 进行了一些奇怪的处理。我不得不通过基于 py4j 的 log4j 钩子来解决问题,参考了 https://dev59.com/1F8e5IYBdhLWcg3wwMhW#34683626 中的解决方案。 - Scott H
我建议那些没有使用Azure的人可以查看@ScottH发布的链接。 - k88
2个回答

6
您可以使用 azure_storage_logging 处理程序:
import logging
from azure_storage_logging.handlers import BlobStorageRotatingFileHandler
log = logging.getLogger('service_logger')
azure_blob_handler = BlobStorageRotatingFileHandler(filename, 
                                                    account_name,
                                                    account_key,
                                                    maxBytes,
                                                    container)
log.addHandler(azure_blob_handler)

看起来这个 azure_storage_logging 模块已经不再维护,还存在一些未解决的问题。 - brokkoo
这会在第二次运行时给我一个错误,操作不支持。 - codebot

-2

让我解释一下使用Python访问或执行Azure数据湖存储的写操作的步骤

1)在Azure AD中注册一个应用程序

enter image description here

enter image description here

2) 为您注册的应用程序在数据湖中授予权限

enter image description here

enter image description here

enter image description here

enter image description here

3) 请从Azure AD获取您已注册的应用程序的客户端密钥。

4) 您需要编写一段代码来挂载Azure数据湖中的目录,如下所示:

dbutils.fs.mkdirs("/mnt/mountdatalake")

config = {"dfs.adls.oauth2.access.token.provider.type": "ClientCredential",
           "dfs.adls.oauth2.client.id": "Registered_Client_Id_From_Azure_Portal",
             "dfs.adls.oauth2.credential": "Cleint_Secret_Obtained_By_Azure_Portal",
               "dfs.adls.oauth2.refresh.url":"https://login.microsoftonline.com/Your_Directory_ID/oauth2/token"}

dbutils.fs.amount(
               source="adl://mydata.azuredatalakestore.net/mountdatabricks",
               mount_point ="/mnt/mountdatalake",
extra_configs=configs)

一旦使用应用程序客户端凭据完成配置/挂载,您就可以访问目录并记录它。

例如,下面我从SQL服务器中提取了几条记录,并将其存储在Azure数据湖中。

enter image description here

希望这能有所帮助。

感谢您的回复。我正在使用与存储帐户合并的Data Lake Storage Gen2,因此连接和挂载看起来与您对Data Lake Storage Gen1的建议有些不同。应用程序已注册,并通过App ID/Object ID和Service Principal ID授予访问权限。连接通常似乎不是问题,因为使用dbutils.fs.putf.write()进行读写工作正常。但是它在使用Python日志记录模块时无法正常工作,我不知道为什么以及这与其他文件写入方法有何不同。 - Dominik Braun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接