Use Python logging
to send logs to both stderr
and display the logs in the notebook output.
import logging
# Create a logger
logger = logging.getLogger('synapse_logger')
logger.setLevel(logging.DEBUG)
# Create a StreamHandler for stderr (default for logging)
stderr_handler = logging.StreamHandler()
stderr_handler.setLevel(logging.DEBUG)
# Create formatter and add it to the handler
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
stderr_handler.setFormatter(formatter)
# Add the handler to the logger
logger.addHandler(stderr_handler)
# Example log messages
logger.info("This is an info message.")
logger.error("This is an error message.")
If you want to use Spark log4j
logging and ensure logs are also displayed in the notebook output, you can use Spark's built-in logging with a PySpark wrapper.
from pyspark import SparkContext
from pyspark.sql import SparkSession
# Initialize Spark session if not already done
spark = SparkSession.builder.getOrCreate()
# Get logger from log4j
log4jLogger = spark._jvm.org.apache.log4j
logger = log4jLogger.LogManager.getLogger("synapse_pyspark")
# Log some messages
logger.info("This is an info message from log4j.")
logger.error("This is an error message from log4j.")
Normally, both approaches will make the logs appear in the notebook's cell output by default. If not, check that the Spark or Python log levels aren't being filtered out in your Synapse workspace configuration.