Switch the Gym automated logger setup to configure the root logger rather than just the 'gym' logger

Python doesn't make it easy for libraries to take responsibility for
logging configuration (which we do to make simple usage much easier),
and as we see more Gym plugins, we want their loggers to have an
appropriate log level too. So we may as well configure the root logger
level.
This commit is contained in:
Greg Brockman
2016-09-21 14:55:04 -07:00
parent 46b47725a3
commit ada730a5b1
3 changed files with 24 additions and 8 deletions

View File

@@ -264,6 +264,8 @@ You can also run tests in a specific directory by using the ``-s`` option, or by
What's new What's new
---------- ----------
- 2016-09-21: Switch the Gym automated logger setup to configure the
root logger rather than just the 'gym' logger.
- 2016-08-17: Calling `close` on an env will also close the monitor - 2016-08-17: Calling `close` on an env will also close the monitor
and any rendering windows. and any rendering windows.
- 2016-08-17: The monitor will no longer write manifest files in - 2016-08-17: The monitor will no longer write manifest files in

View File

@@ -1,5 +1,7 @@
import argparse
import logging import logging
import os, sys import os
import sys
import gym import gym
@@ -12,13 +14,25 @@ class RandomAgent(object):
return self.action_space.sample() return self.action_space.sample()
if __name__ == '__main__': if __name__ == '__main__':
# You can optionally set up the logger. Also fine to set the level parser = argparse.ArgumentParser(description=None)
# to logging.DEBUG or logging.WARN if you want to change the parser.add_argument('env_id', nargs='?', default='CartPole-v0', help='Select the environment to run')
# amount of output. args = parser.parse_args()
# Call `undo_logger_setup` if you want to undo Gym's logger setup
# and configure things manually. (The default should be fine most
# of the time.)
gym.undo_logger_setup()
logger = logging.getLogger() logger = logging.getLogger()
formatter = logging.Formatter('[%(asctime)s] %(message)s')
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(formatter)
logger.addHandler(handler)
# You can set the level to logging.DEBUG or logging.WARN if you
# want to change the amount of output.
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
env = gym.make('CartPole-v0' if len(sys.argv)<2 else sys.argv[1]) env = gym.make(args.env_id)
# You provide the directory to write to (can be an existing # You provide the directory to write to (can be an existing
# directory, including one with existing data -- all monitor files # directory, including one with existing data -- all monitor files

View File

@@ -17,10 +17,10 @@ handler.setFormatter(formatter)
# at initialization time. # at initialization time.
def logger_setup(gym_logger): def logger_setup(gym_logger):
root_logger.addHandler(handler) root_logger.addHandler(handler)
gym_logger.setLevel(logging.INFO) root_logger.setLevel(logging.INFO)
# When set to INFO, this will print out the hostname of every # When set to INFO, this will print out the hostname of every
# connection it makes. # connection it makes.
# requests_logger.setLevel(logging.WARN) requests_logger.setLevel(logging.WARN)
def undo_logger_setup(): def undo_logger_setup():
"""Undoes the automatic logging setup done by OpenAI Gym. You should call """Undoes the automatic logging setup done by OpenAI Gym. You should call
@@ -33,5 +33,5 @@ def undo_logger_setup():
logger.addHandler(logging.StreamHandler(sys.stderr)) logger.addHandler(logging.StreamHandler(sys.stderr))
""" """
root_logger.removeHandler(handler) root_logger.removeHandler(handler)
gym.logger.setLevel(logging.NOTSET) root_logger.setLevel(logging.NOTSET)
requests_logger.setLevel(logging.NOTSET) requests_logger.setLevel(logging.NOTSET)