Files
Gymnasium/gymnasium/logger.py

49 lines
1.2 KiB
Python
Raw Normal View History

"""Set of functions for logging messages."""
2024-06-10 17:07:47 +01:00
import warnings
2022-09-08 10:10:07 +01:00
from gymnasium.utils import colorize
WARN = 30
ERROR = 40
min_level = 30
2021-07-29 02:26:34 +02:00
# Ensure DeprecationWarning to be displayed (#2685, #3059)
2022-09-08 10:10:07 +01:00
warnings.filterwarnings("once", "", DeprecationWarning, module=r"^gymnasium\.")
def warn(
msg: str,
*args: object,
category: type[Warning] | None = None,
stacklevel: int = 1,
):
"""Raises a warning to the user if the min_level <= WARN.
Args:
msg: The message to warn the user
*args: Additional information to warn the user
category: The category of warning
stacklevel: The stack level to raise to
"""
if min_level <= WARN:
warnings.warn(
colorize(f"WARN: {msg % args}", "yellow"),
category=category,
stacklevel=stacklevel + 1,
)
def deprecation(msg: str, *args: object):
"""Logs a deprecation warning to users."""
warn(msg, *args, category=DeprecationWarning, stacklevel=2)
2021-07-29 02:26:34 +02:00
def error(msg: str, *args: object):
"""Logs an error message if min_level <= ERROR in red on the sys.stderr."""
if min_level <= ERROR:
warnings.warn(colorize(f"ERROR: {msg % args}", "red"), stacklevel=3)