cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Logging inside foreachbatch

AndriusVitkausk
New Contributor III

Im having difficulty carrying out logging inside a foreachbatch.

Seems that all logging outside the foreachbatch works as expected, but inside they are only visable from the spark UI in driver logs. Is there a way to get this to work (inc serverless)?

1 REPLY 1

Ashwin_DSA
Databricks Employee
Databricks Employee

Hi @AndriusVitkausk,

This is expected behaviour on Databricks. Code inside foreachBatch runs in the background as part of the longโ€‘lived streaming query, so print() and standard logging go to the driver logs / Spark UI, not back to the notebook output. In Databricks Runtime 14.0+, this is explicitly documented: โ€œprint() commands write output to the driver logsโ€ for foreachBatch on compute with standard access mode.foreachlogs.png

There isnโ€™t a switch to redirect those logs into the notebook, including on serverless. If you need visibility outside the driver logs, Databricks recommends using structured approaches such as writing metrics or diagnostics to a Delta/UC table from inside foreachBatch, or using StreamingQueryListener + observable metrics to push perโ€‘microbatch metrics to external monitoring systems. You may find this useful.

If this answer resolves your question, could you mark it as โ€œAccept as Solutionโ€? That helps other users quickly find the correct fix.

Regards,
Ashwin | Delivery Solution Architect @ Databricks
Helping you build and scale the Data Intelligence Platform.
***Opinions are my own***