forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core][2/n] Core structured logging: add Text formatter and pass log …
…config to worker process (ray-project#45344) Signed-off-by: kaihsun <[email protected]> Signed-off-by: Richard Liu <[email protected]>
- Loading branch information
1 parent
ebeec56
commit 8409e87
Showing
10 changed files
with
312 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,55 @@ | ||
import logging | ||
import json | ||
from ray._private.structured_logging.constants import LogKey, LOGRECORD_STANDARD_ATTRS | ||
from ray._private.ray_constants import LOGGER_FORMAT | ||
|
||
|
||
class JSONFormatter(logging.Formatter): | ||
def format(self, record): | ||
record_format = { | ||
LogKey.ASCTIME: self.formatTime(record), | ||
def generate_record_format_attrs( | ||
formatter: logging.Formatter, | ||
record: logging.LogRecord, | ||
exclude_standard_attrs, | ||
) -> dict: | ||
record_format_attrs = {} | ||
# If `exclude_standard_attrs` is False, include the standard attributes. | ||
# Otherwise, include only Ray and user-provided context. | ||
if not exclude_standard_attrs: | ||
record_format_attrs = { | ||
LogKey.ASCTIME: formatter.formatTime(record), | ||
LogKey.LEVELNAME: record.levelname, | ||
LogKey.MESSAGE: record.getMessage(), | ||
LogKey.FILENAME: record.filename, | ||
LogKey.LINENO: record.lineno, | ||
} | ||
if record.exc_info: | ||
if not record.exc_text: | ||
record.exc_text = self.formatException(record.exc_info) | ||
record_format[LogKey.EXC_TEXT] = record.exc_text | ||
|
||
for key, value in record.__dict__.items(): | ||
# Both Ray and user-provided context are stored in `record_format`. | ||
if key not in LOGRECORD_STANDARD_ATTRS: | ||
record_format[key] = value | ||
return json.dumps(record_format) | ||
record.exc_text = formatter.formatException(record.exc_info) | ||
record_format_attrs[LogKey.EXC_TEXT] = record.exc_text | ||
|
||
for key, value in record.__dict__.items(): | ||
# Both Ray and user-provided context are stored in `record_format`. | ||
if key not in LOGRECORD_STANDARD_ATTRS: | ||
record_format_attrs[key] = value | ||
return record_format_attrs | ||
|
||
|
||
class JSONFormatter(logging.Formatter): | ||
def format(self, record): | ||
record_format_attrs = generate_record_format_attrs( | ||
self, record, exclude_standard_attrs=False | ||
) | ||
return json.dumps(record_format_attrs) | ||
|
||
|
||
class TextFormatter(logging.Formatter): | ||
def __init__(self) -> None: | ||
self._inner_formatter = logging.Formatter(LOGGER_FORMAT) | ||
|
||
def format(self, record: logging.LogRecord) -> str: | ||
s = self._inner_formatter.format(record) | ||
record_format_attrs = generate_record_format_attrs( | ||
self, record, exclude_standard_attrs=True | ||
) | ||
additional_attrs = " ".join( | ||
[f"{key}={value}" for key, value in record_format_attrs.items()] | ||
) | ||
return f"{s} {additional_attrs}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from ray._private.structured_logging.constants import LOG_MODE_DICT | ||
from ray.util.annotations import PublicAPI | ||
|
||
from dataclasses import dataclass | ||
|
||
|
||
@PublicAPI(stability="alpha") | ||
@dataclass | ||
class LoggingConfig: | ||
""" | ||
Logging configuration for a Ray job. These configurations are used to set up the | ||
root logger of the driver process and all Ray tasks and actor processes that belong | ||
to the job. | ||
Examples: | ||
.. testcode:: | ||
import ray | ||
import logging | ||
ray.init( | ||
logging_config=ray.LoggingConfig(encoding="TEXT", log_level="INFO") | ||
) | ||
@ray.remote | ||
def f(): | ||
logger = logging.getLogger(__name__) | ||
logger.info("This is a Ray task") | ||
ray.get(f.remote()) | ||
.. testoutput:: | ||
:options: +MOCK | ||
2024-06-03 07:53:50,815 INFO test.py:11 -- This is a Ray task job_id=01000000 worker_id=0dbbbd0f17d5343bbeee8228fa5ff675fe442445a1bc06ec899120a8 node_id=577706f1040ea8ebd76f7cf5a32338d79fe442e01455b9e7110cddfc task_id=c8ef45ccd0112571ffffffffffffffffffffffff01000000 | ||
Args: | ||
encoding: Encoding type for the logs. The valid value is 'TEXT' | ||
log_level: Log level for the logs. Defaults to 'INFO'. You can set | ||
it to 'DEBUG' to receive more detailed debug logs. | ||
""" # noqa: E501 | ||
|
||
encoding: str = "TEXT" | ||
log_level: str = "INFO" | ||
|
||
def __post_init__(self): | ||
if self.encoding not in LOG_MODE_DICT: | ||
raise ValueError( | ||
f"Invalid encoding type: {self.encoding}. " | ||
f"Valid encoding types are: {list(LOG_MODE_DICT.keys())}" | ||
) | ||
|
||
def _get_dict_config(self) -> dict: | ||
"""Get the logging configuration based on the encoding type. | ||
Returns: | ||
dict: The logging configuration. | ||
""" | ||
return LOG_MODE_DICT[self.encoding](self.log_level) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.