Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing log with python logging module in databricks to azure datalake not working

I'm trying to write my own log files to Azure Datalake Gen 2 in a Python-Notebook within Databricks. I'm trying to achieve that by using the Python logging module.

Unfortunately I can't get it working. No errors are raised, the folders are created but no file with logging content is created. Even if the files exists, nothing is written to it.

A local python script works just fine, but I can't get it working in Databricks.

Here is my code:

# mount
if not any(mount.mountPoint == '/mnt/log' for mount in dbutils.fs.mounts()):
  dbutils.fs.mount(
    source = "abfss://[email protected]/",
    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.')

Mounting seems to be ok.

Adding and writing files with dbutils works fine:

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

Writing to file like that works fine too:

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

Also tried adding "dbfs" to path

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

Any ideas?

like image 258
Dominik Braun Avatar asked Apr 15 '19 12:04

Dominik Braun


People also ask

How do I enable logging in Databricks?

As an admin, go to the Azure Databricks admin console. Click Workspace settings. Next to Verbose Audit Logs, enable or disable the feature.

Is log4j used in Python?

log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License. log4j is a popular logging package written in Java. log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.


1 Answers

You can use azure_storage_logging handler:

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)
like image 169
gilgorio Avatar answered Sep 28 '22 20:09

gilgorio