<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: write both logging error Pyspark and Python exceptions in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/write-both-logging-error-pyspark-and-python-exceptions/m-p/144144#M52270</link>
    <description>&lt;P&gt;Thanks a lot&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/110502"&gt;@szymon_dybczak&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 15 Jan 2026 11:13:56 GMT</pubDate>
    <dc:creator>seefoods</dc:creator>
    <dc:date>2026-01-15T11:13:56Z</dc:date>
    <item>
      <title>write both logging error Pyspark and Python exceptions</title>
      <link>https://community.databricks.com/t5/data-engineering/write-both-logging-error-pyspark-and-python-exceptions/m-p/144125#M52268</link>
      <description>&lt;P&gt;Hello guyz,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Happy new year and best wishes for all of us. I am catching both Pyspark and Python exceptions but i want to write this logging error inside a delta table when i logging. Someone knows the best practise for this ?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thanks&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Cordially,&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jan 2026 10:16:39 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/write-both-logging-error-pyspark-and-python-exceptions/m-p/144125#M52268</guid>
      <dc:creator>seefoods</dc:creator>
      <dc:date>2026-01-15T10:16:39Z</dc:date>
    </item>
    <item>
      <title>Re: write both logging error Pyspark and Python exceptions</title>
      <link>https://community.databricks.com/t5/data-engineering/write-both-logging-error-pyspark-and-python-exceptions/m-p/144143#M52269</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/65591"&gt;@seefoods&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;You can write decorator function and use it to perform custom logging.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from functools import wraps
from pyspark.sql import SparkSession
from datetime import datetime
import traceback

spark = SparkSession.builder.getOrCreate()

def logger(success_table: str, error_table: str):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            try:
                result = func(*args, **kwargs)

                # Success log
                log_df = spark.createDataFrame([{
                    "function_name": func.__name__,
                    "status": "SUCCESS",
                    "timestamp": datetime.now()
                }])
                log_df.write.format("delta").mode("append").saveAsTable(success_table)

                return result
            except Exception as e:
                # Error log
                error_df = spark.createDataFrame([{
                    "function_name": func.__name__,
                    "status": "ERROR",
                    "timestamp": datetime.now(),
                    "error_message": str(e),
                    "stack_trace": traceback.format_exc()
                }])
                error_df.write.format("delta").mode("append").saveAsTable(error_table)

                raise  # Optional: re-raise exception
        return wrapper
    return decorator

#function definition
@logger(success_table="LOG_SUCCESS", error_table="LOG_ERROR")
def load_data(a, b):
    print(a/b)
    return "Done"

#calling the function
load_data(20, 10) #this will log the success message to LOG_SUCCESS
load_data(10, 0) #this will log the exception message to LOG_ERROR&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Check below article for more details:&lt;/P&gt;&lt;P&gt;&lt;A href="https://medium.com/@krishnanand654/custom-logging-to-delta-tables-in-databricks-reduce-code-redundancy-484151fe2c57" target="_blank"&gt;Custom Logging to Delta Tables in Databricks: Reduce Code Redundancy | by Krishnanand A | Medium&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jan 2026 11:04:16 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/write-both-logging-error-pyspark-and-python-exceptions/m-p/144143#M52269</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2026-01-15T11:04:16Z</dc:date>
    </item>
    <item>
      <title>Re: write both logging error Pyspark and Python exceptions</title>
      <link>https://community.databricks.com/t5/data-engineering/write-both-logging-error-pyspark-and-python-exceptions/m-p/144144#M52270</link>
      <description>&lt;P&gt;Thanks a lot&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/110502"&gt;@szymon_dybczak&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jan 2026 11:13:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/write-both-logging-error-pyspark-and-python-exceptions/m-p/144144#M52270</guid>
      <dc:creator>seefoods</dc:creator>
      <dc:date>2026-01-15T11:13:56Z</dc:date>
    </item>
    <item>
      <title>Re: write both logging error Pyspark and Python exceptions</title>
      <link>https://community.databricks.com/t5/data-engineering/write-both-logging-error-pyspark-and-python-exceptions/m-p/144149#M52272</link>
      <description>&lt;P&gt;No problem&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/65591"&gt;@seefoods&lt;/a&gt;&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Jan 2026 11:45:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/write-both-logging-error-pyspark-and-python-exceptions/m-p/144149#M52272</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2026-01-15T11:45:56Z</dc:date>
    </item>
  </channel>
</rss>

