itholic commented on code in PR #47145: URL: https://github.com/apache/spark/pull/47145#discussion_r1677312000
########## python/pyspark/logger/logger.py: ########## @@ -0,0 +1,150 @@ +# -*- encoding: utf-8 -*- +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import io +import logging +import json +from typing import Any, Optional, Union + + +class JSONFormatter(logging.Formatter): + """ + Custom JSON formatter for logging records. + + This formatter converts the log record to a JSON object with the following fields: + - timestamp: The time the log record was created. + - level: The log level of the record. + - name: The name of the logger. + - message: The log message. + - kwargs: Any additional keyword arguments passed to the logger. + """ + + def format(self, record: logging.LogRecord) -> str: + """ + Format the specified record as a JSON string. + + Parameters + ---------- + record : logging.LogRecord + The log record to be formatted. + + Returns + ------- + str + The formatted log record as a JSON string. + """ + log_entry = { + "ts": self.formatTime(record, self.datefmt), + "level": record.levelname, + "logger": record.name, + "msg": record.getMessage(), + "context": record.__dict__.get("kwargs", {}), + } + return json.dumps(log_entry, ensure_ascii=False) + + +class PySparkLogger(logging.Logger): + """ + Custom logger for PySpark that logs messages in a structured JSON format. + + This logger provides methods for logging messages at different levels (info, warning, error) + with additional keyword arguments that are included in the JSON log entry. + """ + + def __init__( + self, + name: str = "PySparkLogger", + level: int = logging.INFO, + stream: Optional[Union[None, io.TextIOWrapper]] = None, + filename: Optional[str] = None, + ): + super().__init__(name, level) + if not self.hasHandlers(): Review Comment: Just realized that we should leverage the `logging.getLogger` after `setLoggerClass(PySparkLogger)` when initializing the logger to keep the consistency with `logging` module. I will update it -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org