Skip to content

Globally setting the logging level in in gym_unity/envs/__init__.py conflicts with user setting. #4987

Closed
@julienroyd

Description

@julienroyd

I am using Python 3.7.4 and gym_unity==0.23.0

In gym_unity/envs/__init__.py, the logging level is set globally:
https://github.com/Unity-Technologies/ml-agents/blob/master/gym-unity/gym_unity/envs/__init__.py#L22

This causes the logging settings to be overwritten for loggers defined by the users simply by importing the UnityToGymWrapper and leads to double printing (in red) in the console. Here is a minimal example to reproduce the issue:

import logging
import sys

from gym_unity.envs import UnityToGymWrapper

def create_logger(name, loglevel):
    logger = logging.getLogger(name)
    logger.setLevel(loglevel)
    handler = logging.StreamHandler(stream=sys.stdout)
    formatter = logging.Formatter(fmt=f'%(asctime)s - {name} - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger

if __name__ == "__main__":
    logger = create_logger(name="USER LOGGER", loglevel=logging.INFO)
    for i in range(10):
        logger.info(f"i={i}")

Which outputs:

output

(commenting out from gym_unity.envs import UnityToGymWrapper resolves the issue)

Suggested fix:

By simply setting the logging level for the logger instance only seem to solve the issue (Line 22 of gym_unity/envs/__init__.py):

logger.setLevel(logging.INFO)

Metadata

Metadata

Assignees

Labels

bugIssue describes a potential bug in ml-agents.staleIssues that have been idle for a while. Automatically closed by a bot if idle for too long.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions