Log messages
Logging message is important to oversee the program status. MetaDrive adopts Python’s logging
module to log messages.
Global logger
We provide a wrapper in metadrive/engine/logger
to customize some features. We recommend using it to log messages to make the logging consistent. It is a global variable, so you can get the logger at anywhere of your code. Some classes like BaseEnv
may have a variable called logger
as well, which is actually the same as the global logger.
from metadrive.envs.base_env import BaseEnv
from metadrive.engine.logger import get_logger # get_logger
logger = get_logger()
# create environment
logger.info("Create environment\n")
env = BaseEnv(dict(use_render=False))
assert logger is env.logger # the environment.logger is the same as global logger
# reset environment
logger.info("Reset environment")
env.reset()
try:
for i in range(5):
logger.info("Simulate step {}".format(i))
env.step(env.action_space.sample())
finally:
logger.info("Close environment")
env.close()
[INFO] Create environment
[INFO] Environment: BaseEnv
[INFO] MetaDrive version: 0.4.3
[INFO] Sensors: [lidar: Lidar(), side_detector: SideDetector(), lane_line_detector: LaneLineDetector()]
[INFO] Render Mode: none
[INFO] Horizon (Max steps per agent): None
[WARNING] You are using DummyObservation which doesn't collect information from the environment. (observation_base.py:50)
[INFO] Reset environment
[INFO] Assets version: 0.4.3
[INFO] Known Pipes: glxGraphicsPipe
[WARNING] Can not find `start_seed` or `start_scenario_index`. Use 0 as `start_seed` (base_engine.py:582)
[INFO] Start Scenario Index: 0, Num Scenarios : 1
[WARNING] Can not find `start_seed` or `start_scenario_index`. Use 0 as `start_seed` (base_engine.py:582)
[WARNING] No map is provided. Set vehicle to position (0, 0) with heading 0 (base_vehicle.py:341)
[WARNING] Reward function is not implemented. Return reward = 0 (base_env.py:481)
[WARNING] Done function is not implemented. Return Done = False (base_env.py:489)
[WARNING] Cost function is not implemented. Return cost = 0 (base_env.py:485)
[INFO] Simulate step 0
[INFO] Simulate step 1
[INFO] Simulate step 2
[INFO] Simulate step 3
[INFO] Simulate step 4
[INFO] Close environment
The logger can be used at everywhere. For example, it can be used in your manager.
from metadrive.envs.base_env import BaseEnv
from metadrive.engine.logger import get_logger # get_logger
from metadrive.manager.base_manager import BaseManager
logger = get_logger()
class MyMgr(BaseManager):
def __init__(self):
super(MyMgr, self).__init__()
logger.info("Init MyMgr...")
def after_step(self):
logger.info("Step {}...".format(self.episode_step))
return dict()
# create environment
env = BaseEnv(dict(use_render=False))
# reset environment
env.reset()
# add manager
env.engine.register_manager("my_mgr", MyMgr())
try:
for i in range(5):
env.step(env.action_space.sample())
finally:
env.close()
[INFO] Environment: BaseEnv
[INFO] MetaDrive version: 0.4.3
[INFO] Sensors: [lidar: Lidar(), side_detector: SideDetector(), lane_line_detector: LaneLineDetector()]
[INFO] Render Mode: none
[INFO] Horizon (Max steps per agent): None
[WARNING] You are using DummyObservation which doesn't collect information from the environment. (observation_base.py:50)
[INFO] Assets version: 0.4.3
[INFO] Known Pipes: glxGraphicsPipe
[WARNING] Can not find `start_seed` or `start_scenario_index`. Use 0 as `start_seed` (base_engine.py:582)
[INFO] Start Scenario Index: 0, Num Scenarios : 1
[WARNING] Can not find `start_seed` or `start_scenario_index`. Use 0 as `start_seed` (base_engine.py:582)
[WARNING] No map is provided. Set vehicle to position (0, 0) with heading 0 (base_vehicle.py:341)
[WARNING] Reward function is not implemented. Return reward = 0 (base_env.py:481)
[WARNING] Done function is not implemented. Return Done = False (base_env.py:489)
[WARNING] Cost function is not implemented. Return cost = 0 (base_env.py:485)
[INFO] Init MyMgr...
[INFO] Step 1...
[INFO] Step 2...
[INFO] Step 3...
[INFO] Step 4...
[INFO] Step 5...
Log level
Similar to logging
module, one can change the log level to decide which log messages to be printed. Generally, MetaDrive uses logging.INFO
, logging.DEBUG
, and logging.WARNING
to classify log messages. By default, the log level is logging.INFO
and thus debug messages will be hidden. The log level can be set in environment config. The following example shows how to print more debug message by setting log_level=logging.DEBUG
. In addition, if debug=True
in the environment config, the log level will be set to logging.DEBUG
as well.
from metadrive.envs.base_env import BaseEnv
import logging
from metadrive.engine.logger import get_logger # get_logger
logger = get_logger()
# create environment
env = BaseEnv(dict(use_render=False, log_level=logging.DEBUG))
assert logger is env.logger # the environment.logger is the same as global logger
# reset environment
logger.debug("Reset environment")
env.reset()
try:
for i in range(5):
logger.debug("Simulate step {}".format(i))
env.step(env.action_space.sample())
finally:
logger.debug("Close environment")
env.close()
[INFO] Environment: BaseEnv
[INFO] MetaDrive version: 0.4.3
[INFO] Sensors: [lidar: Lidar(), side_detector: SideDetector(), lane_line_detector: LaneLineDetector()]
[INFO] Render Mode: none
[INFO] Horizon (Max steps per agent): None
[WARNING] You are using DummyObservation which doesn't collect information from the environment. (observation_base.py:50)
[DEBUG] Reset environment (416816667.py:12)
[INFO] Assets version: 0.4.3
[INFO] Known Pipes: glxGraphicsPipe
[DEBUG] Load Vehicle Module: Lidar (distance_detector.py:114)
[DEBUG] Load Vehicle Module: SideDetector (distance_detector.py:114)
[DEBUG] Load Vehicle Module: LaneLineDetector (distance_detector.py:114)
[WARNING] Can not find `start_seed` or `start_scenario_index`. Use 0 as `start_seed` (base_engine.py:582)
[INFO] Start Scenario Index: 0, Num Scenarios : 1
[WARNING] Can not find `start_seed` or `start_scenario_index`. Use 0 as `start_seed` (base_engine.py:582)
[WARNING] No map is provided. Set vehicle to position (0, 0) with heading 0 (base_vehicle.py:341)
[DEBUG] DefaultVehicle is attached to the world. (base_object.py:235)
[DEBUG] Terrain is attached to the world. (base_object.py:235)
[WARNING] Reward function is not implemented. Return reward = 0 (base_env.py:481)
[WARNING] Done function is not implemented. Return Done = False (base_env.py:489)
[WARNING] Cost function is not implemented. Return cost = 0 (base_env.py:485)
[DEBUG] Simulate step 0 (416816667.py:16)
[DEBUG] Simulate step 1 (416816667.py:16)
[DEBUG] Simulate step 2 (416816667.py:16)
[DEBUG] Simulate step 3 (416816667.py:16)
[DEBUG] Simulate step 4 (416816667.py:16)
[DEBUG] Close environment (416816667.py:19)
[DEBUG] DefaultVehicle is detached from the world. (base_object.py:253)
[DEBUG] Finish cleaning 6 node path for DefaultVehicle. (base_object.py:284)
[DEBUG] Before del taskMgr: task_chain_num=2, all_tasks=[PythonTask resetPrevTransform, PythonTask dataLoop, PythonTask eventManager, PythonTask ivalLoop, PythonTask collisionLoop, PythonTask igLoop] (engine_core.py:404)
[DEBUG] After del taskMgr: task_chain_num=2, all_tasks=[PythonTask resetPrevTransform, PythonTask dataLoop, PythonTask eventManager, PythonTask ivalLoop, PythonTask collisionLoop, PythonTask igLoop] (engine_core.py:412)
[DEBUG] Terrain is detached from the world. (base_object.py:253)
[DEBUG] Finish cleaning 1 node path for Terrain. (base_object.py:284)
If you would like to turn off all log messages, changing the log_level to logging.WARNING
(or logging.CRITICAL
) can suppress messages logged by logging.info()
, logging.debug()
(or even logging.warn
).
from metadrive.engine.logger import set_log_level # get_logger
def run(): # define the simulation loop
# reset environment
logger.debug("Reset environment")
env.reset()
try:
for i in range(5):
logger.debug("Simulate step {}".format(i))
env.step(env.action_space.sample())
finally:
logger.debug("Close environment")
env.close()
set_log_level(logging.WARNING) # Or set it in environment config
run()
print("Turn off all messages...")
set_log_level(logging.CRITICAL) # Or set it in environment config
run()
[WARNING] Can not find `start_seed` or `start_scenario_index`. Use 0 as `start_seed` (base_engine.py:582)
[WARNING] Can not find `start_seed` or `start_scenario_index`. Use 0 as `start_seed` (base_engine.py:582)
[WARNING] No map is provided. Set vehicle to position (0, 0) with heading 0 (base_vehicle.py:341)
[WARNING] Reward function is not implemented. Return reward = 0 (base_env.py:481)
[WARNING] Done function is not implemented. Return Done = False (base_env.py:489)
[WARNING] Cost function is not implemented. Return cost = 0 (base_env.py:485)
Turn off all messages...
Log once
Sometimes, we may want the message to be logged only once in one episode. In this case, we can use the log_once
key to notify the logger to show some messages only once until the next reset
is called.
from metadrive.envs.base_env import BaseEnv
import logging
# create environment
env = BaseEnv(dict(use_render=False, log_level=logging.CRITICAL))
# reset environment
env.reset()
try:
for i in range(5):
env.logger.critical("Step... (I will only show once)".format(i), extra={"log_once": True})
env.step(env.action_space.sample())
finally:
env.close()
[CRITICAL] Step... (I will only show once) (2663424590.py:11)