Skip to content

Commit ebeb034

Browse files
author
Chris Elion
authored
Fix how we set logging levels (#3703)
* cleanup logging * comments and cleanup * pylint, gym
1 parent fe10020 commit ebeb034

File tree

24 files changed

+108
-59
lines changed

24 files changed

+108
-59
lines changed

.pylintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ disable =
4444
# Appears to be https://github.com/PyCQA/pylint/issues/2981
4545
W0201,
4646

47+
# Using the global statement
48+
W0603,

gym-unity/gym_unity/envs/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import logging
21
import itertools
32
import numpy as np
43
from typing import Any, Dict, List, Optional, Tuple, Union
@@ -8,6 +7,7 @@
87

98
from mlagents_envs.environment import UnityEnvironment
109
from mlagents_envs.base_env import BatchedStepResult
10+
from mlagents_envs import logging_util
1111

1212

1313
class UnityGymException(error.Error):
@@ -18,9 +18,8 @@ class UnityGymException(error.Error):
1818
pass
1919

2020

21-
logging.basicConfig(level=logging.INFO)
22-
logger = logging.getLogger("gym_unity")
23-
21+
logger = logging_util.get_logger(__name__)
22+
logging_util.set_log_level(logging_util.INFO)
2423

2524
GymSingleStepResult = Tuple[np.ndarray, float, bool, Dict]
2625
GymMultiStepResult = Tuple[List[np.ndarray], List[float], List[bool], Dict]

ml-agents-envs/mlagents_envs/environment.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import atexit
22
import glob
33
import uuid
4-
import logging
54
import numpy as np
65
import os
76
import subprocess
87
from typing import Dict, List, Optional, Any
98

109
import mlagents_envs
10+
11+
from mlagents_envs.logging_util import get_logger
1112
from mlagents_envs.side_channel.side_channel import SideChannel, IncomingMessage
1213

1314
from mlagents_envs.base_env import (
@@ -47,7 +48,7 @@
4748
import struct
4849

4950

50-
logger = logging.getLogger("mlagents_envs")
51+
logger = get_logger(__name__)
5152

5253

5354
class UnityEnvironment(BaseEnv):
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import logging # noqa I251
2+
3+
CRITICAL = logging.CRITICAL
4+
FATAL = logging.FATAL
5+
ERROR = logging.ERROR
6+
WARNING = logging.WARNING
7+
INFO = logging.INFO
8+
DEBUG = logging.DEBUG
9+
NOTSET = logging.NOTSET
10+
11+
_loggers = set()
12+
_log_level = NOTSET
13+
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
14+
LOG_FORMAT = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
15+
16+
17+
def get_logger(name: str) -> logging.Logger:
18+
"""
19+
Create a logger with the specified name. The logger will use the log level
20+
specified by set_log_level()
21+
"""
22+
logger = logging.getLogger(name=name)
23+
24+
# If we've already set the log level, make sure new loggers use it
25+
if _log_level != NOTSET:
26+
logger.setLevel(_log_level)
27+
28+
# Keep track of this logger so that we can change the log level later
29+
_loggers.add(logger)
30+
return logger
31+
32+
33+
def set_log_level(log_level: int) -> None:
34+
"""
35+
Set the ML-Agents logging level. This will also configure the logging format (if it hasn't already been set).
36+
"""
37+
global _log_level
38+
_log_level = log_level
39+
40+
# Configure the log format.
41+
# In theory, this would be sufficient, but if another library calls logging.basicConfig
42+
# first, it doesn't have any effect.
43+
logging.basicConfig(level=_log_level, format=LOG_FORMAT, datefmt=DATE_FORMAT)
44+
45+
for logger in _loggers:
46+
logger.setLevel(log_level)

ml-agents-envs/mlagents_envs/side_channel/outgoing_message.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from typing import List
22
import struct
33

4-
import logging
4+
from mlagents_envs.logging_util import get_logger
55

6-
logger = logging.getLogger(__name__)
6+
logger = get_logger(__name__)
77

88

99
class OutgoingMessage:

ml-agents-envs/mlagents_envs/side_channel/side_channel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from abc import ABC, abstractmethod
22
from typing import List
33
import uuid
4-
import logging
54

65
from mlagents_envs.side_channel import IncomingMessage, OutgoingMessage
6+
from mlagents_envs.logging_util import get_logger
77

8-
logger = logging.getLogger(__name__)
8+
logger = get_logger(__name__)
99

1010

1111
class SideChannel(ABC):

ml-agents/mlagents/logging_util.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

ml-agents/mlagents/model_serialization.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from distutils.util import strtobool
22
import os
3-
import logging
43
from typing import Any, List, Set, NamedTuple
54
from distutils.version import LooseVersion
65

@@ -19,14 +18,16 @@
1918

2019
from tensorflow.python.platform import gfile
2120
from tensorflow.python.framework import graph_util
21+
22+
from mlagents_envs.logging_util import get_logger
2223
from mlagents.trainers import tensorflow_to_barracuda as tf2bc
2324

2425
if LooseVersion(tf.__version__) < LooseVersion("1.12.0"):
2526
# ONNX is only tested on 1.12.0 and later
2627
ONNX_EXPORT_ENABLED = False
2728

29+
logger = get_logger(__name__)
2830

29-
logger = logging.getLogger("mlagents.trainers")
3031

3132
POSSIBLE_INPUT_NODES = frozenset(
3233
[

ml-agents/mlagents/trainers/components/reward_signals/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import logging
21
from typing import Any, Dict, List
32
from collections import namedtuple
43
import numpy as np
54
import abc
65

76
from mlagents.tf_utils import tf
87

8+
from mlagents_envs.logging_util import get_logger
99
from mlagents.trainers.exception import UnityTrainerException
1010
from mlagents.trainers.policy.tf_policy import TFPolicy
1111
from mlagents.trainers.buffer import AgentBuffer
1212

13-
logger = logging.getLogger("mlagents.trainers")
13+
14+
logger = get_logger(__name__)
1415

1516
RewardSignalResult = namedtuple(
1617
"RewardSignalResult", ["scaled_reward", "unscaled_reward"]

ml-agents/mlagents/trainers/curriculum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
from .exception import CurriculumConfigError, CurriculumLoadingError
66

7-
import logging
7+
from mlagents_envs.logging_util import get_logger
88

9-
logger = logging.getLogger("mlagents.trainers")
9+
logger = get_logger(__name__)
1010

1111

1212
class Curriculum:

0 commit comments

Comments
 (0)