-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
Global logging format changes #32741
Merged
rkooo567
merged 2 commits into
ray-project:master
from
peytondmurray:global-logging-config-format
Apr 5, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import logging | ||
import re | ||
from logging.config import dictConfig | ||
import threading | ||
|
||
|
||
class ContextFilter(logging.Filter): | ||
"""A filter that adds ray context info to log records. | ||
|
||
This filter adds a package name to append to the message as well as information | ||
about what worker emitted the message, if applicable. | ||
""" | ||
|
||
logger_regex = re.compile(r"ray(\.(?P<subpackage>\w+))?(\..*)?") | ||
package_message_names = { | ||
"air": "AIR", | ||
"data": "Data", | ||
"rllib": "RLlib", | ||
"serve": "Serve", | ||
"train": "Train", | ||
"tune": "Tune", | ||
"workflow": "Workflow", | ||
} | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
def filter(self, record: logging.LogRecord) -> bool: | ||
"""Add context information to the log record. | ||
|
||
This filter adds a package name from where the message was generated as | ||
well as the worker IP address, if applicable. | ||
|
||
Args: | ||
record: Record to be filtered | ||
|
||
Returns: | ||
True if the record is to be logged, False otherwise. (This filter only | ||
adds context, so records are always logged.) | ||
""" | ||
match = self.logger_regex.search(record.name) | ||
if match and match["subpackage"] in self.package_message_names: | ||
record.package = f"[Ray {self.package_message_names[match['subpackage']]}]" | ||
else: | ||
record.package = "" | ||
|
||
return True | ||
|
||
|
||
class PlainRayHandler(logging.StreamHandler): | ||
"""A plain log handler. | ||
|
||
This handler writes to whatever sys.stderr points to at emit-time, | ||
not at instantiation time. See docs for logging._StderrHandler. | ||
""" | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.plain_handler = logging._StderrHandler() | ||
self.plain_handler.level = self.level | ||
self.plain_handler.formatter = logging.Formatter(fmt="%(message)s") | ||
|
||
def emit(self, record: logging.LogRecord): | ||
"""Emit the log message. | ||
|
||
If this is a worker, bypass fancy logging and just emit the log record. | ||
If this is the driver, emit the message using the appropriate console handler. | ||
|
||
Args: | ||
record: Log record to be emitted | ||
""" | ||
import ray | ||
|
||
if ( | ||
hasattr(ray, "_private") | ||
and ray._private.worker.global_worker.mode | ||
== ray._private.worker.WORKER_MODE | ||
): | ||
self.plain_handler.emit(record) | ||
else: | ||
logging._StderrHandler.emit(self, record) | ||
|
||
|
||
logger_initialized = False | ||
logging_config_lock = threading.Lock() | ||
|
||
|
||
def generate_logging_config(): | ||
"""Generate the default Ray logging configuration.""" | ||
with logging_config_lock: | ||
global logger_initialized | ||
if logger_initialized: | ||
return | ||
logger_initialized = True | ||
|
||
formatters = { | ||
"plain": { | ||
"datefmt": "[%Y-%m-%d %H:%M:%S]", | ||
"format": "%(asctime)s %(package)s %(levelname)s %(name)s::%(message)s", | ||
}, | ||
} | ||
filters = {"context_filter": {"()": ContextFilter}} | ||
handlers = { | ||
"default": { | ||
"()": PlainRayHandler, | ||
"formatter": "plain", | ||
"filters": ["context_filter"], | ||
} | ||
} | ||
|
||
loggers = { | ||
# Default ray logger; any log message that gets propagated here will be | ||
# logged to the console | ||
"ray": { | ||
"level": "INFO", | ||
"handlers": ["default"], | ||
}, | ||
# Special handling for ray.rllib: only warning-level messages passed through | ||
# See https://github.com/ray-project/ray/pull/31858 for related PR | ||
"ray.rllib": { | ||
"level": "WARN", | ||
}, | ||
} | ||
|
||
dictConfig( | ||
{ | ||
"version": 1, | ||
"formatters": formatters, | ||
"filters": filters, | ||
"handlers": handlers, | ||
"loggers": loggers, | ||
} | ||
) |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the default logging level be "WARNING"? "INFO" is normally quite chatty (at least for RLlib).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default logging level is
INFO
- except for RLlib, which isWARNING
because of the issue raised in this PR. Maybe I should add a note about this to the documentation?We currently have a test to ensure that the logging level of ray and each subpackage is not inadvertently modified, so that this doesn't happen in the future either.