Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose configure_logging #48802

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions python/pyspark/sql/connect/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,35 @@
import os
from typing import Optional

__all__ = [
"getLogLevel",
]
__all__ = ["configure_logging", "getLogLevel"]


def _configure_logging() -> logging.Logger:
"""Configure logging for the Spark Connect clients."""
def configure_logging(level: Optional[str] = None) -> logging.Logger:
"""
Configure log level for Spark Connect components.
When not specified as a parameter, log level will be picked up from the SPARK_CONNECT_LOG_LEVEL environment variable.
When both are absent, logging is disabled.

.. versionadded:: 4.0.0
"""
logger = PySparkLogger.getLogger(__name__)
handler = logging.StreamHandler()
handler.setFormatter(
logging.Formatter(fmt="%(asctime)s %(process)d %(levelname)s %(funcName)s %(message)s")
)
logger.addHandler(handler)

# Check the environment variables for log levels:
if "SPARK_CONNECT_LOG_LEVEL" in os.environ:
if level is not None:
logger.setLevel(level.upper())
elif "SPARK_CONNECT_LOG_LEVEL" in os.environ:
logger.setLevel(os.environ["SPARK_CONNECT_LOG_LEVEL"].upper())
else:
logger.disabled = True
return logger


# Instantiate the logger based on the environment configuration.
logger = _configure_logging()
logger = configure_logging()


def getLogLevel() -> Optional[int]:
Expand Down