Skip to content

Commit

Permalink
[Core] Allow env setup logger encoding (#46242)
Browse files Browse the repository at this point in the history
Signed-off-by: Gene Su <[email protected]>
  • Loading branch information
GeneDer authored Jun 29, 2024
1 parent dfab0f4 commit b359c97
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
2 changes: 2 additions & 0 deletions python/ray/_private/ray_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,5 @@ def gcs_actor_scheduling_enabled():
RAY_NODE_IP_FILENAME = "node_ip_address.json"

PLACEMENT_GROUP_BUNDLE_RESOURCE_NAME = "bundle"

RAY_LOGGING_CONFIG_ENCODING = os.environ.get("RAY_LOGGING_CONFIG_ENCODING")
17 changes: 13 additions & 4 deletions python/ray/_private/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1406,8 +1406,14 @@ def init(
logging.getLogger("ray").handlers.clear()

# Configure the logging settings for the driver process.
if logging_config:
dict_config = logging_config._get_dict_config()
if logging_config or ray_constants.RAY_LOGGING_CONFIG_ENCODING:
dict_config = (
logging_config._get_dict_config()
if logging_config
else LoggingConfig(
encoding=ray_constants.RAY_LOGGING_CONFIG_ENCODING
)._get_dict_config()
)
logging.config.dictConfig(dict_config)

# Parse the hidden options:
Expand Down Expand Up @@ -1584,8 +1590,11 @@ def sigterm_handler(signum, frame):

# Pass the logging_config to job_config to configure loggers of all worker
# processes belonging to the job.
if logging_config:
job_config.set_py_logging_config(logging_config)
if logging_config or ray_constants.RAY_LOGGING_CONFIG_ENCODING:
job_config.set_py_logging_config(
logging_config
or LoggingConfig(encoding=ray_constants.RAY_LOGGING_CONFIG_ENCODING)
)

redis_address, gcs_address = None, None
bootstrap_address = services.canonicalize_bootstrap_address(address, _temp_dir)
Expand Down
76 changes: 76 additions & 0 deletions python/ray/tests/test_logging_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,82 @@ def print_message(self):
for s in should_exist:
assert s in stderr

@pytest.mark.parametrize(
"ray_start_cluster_head_with_env_vars",
[
{
"env_vars": {
"RAY_LOGGING_CONFIG_ENCODING": "TEXT",
},
}
],
indirect=True,
)
def test_env_setup_logger_encoding(
self, ray_start_cluster_head_with_env_vars, shutdown_only
):
script = """
import ray
import logging
ray.init()
@ray.remote
class actor:
def __init__(self):
pass
def print_message(self):
logger = logging.getLogger(__name__)
logger.info("This is a Ray actor")
actor_instance = actor.remote()
ray.get(actor_instance.print_message.remote())
"""
stderr = run_string_as_driver(script)
should_exist = [
"job_id",
"worker_id",
"node_id",
"actor_id",
"task_id",
"INFO",
"This is a Ray actor",
]
for s in should_exist:
assert s in stderr

def test_logger_not_set(self, shutdown_only):
script = """
import ray
import logging
ray.init()
@ray.remote
class actor:
def __init__(self):
pass
def print_message(self):
logger = logging.getLogger(__name__)
logger.info("This is a Ray actor")
actor_instance = actor.remote()
ray.get(actor_instance.print_message.remote())
"""
stderr = run_string_as_driver(script)
should_not_exist = [
"job_id",
"worker_id",
"node_id",
"actor_id",
"task_id",
"This is a Ray actor",
]
for s in should_not_exist:
assert s not in stderr


if __name__ == "__main__":
if os.environ.get("PARALLEL_CI"):
Expand Down

0 comments on commit b359c97

Please sign in to comment.