Skip to content

Commit

Permalink
Fix: try 2 - prevent logging config clobbering (#10192)
Browse files Browse the repository at this point in the history
* fix: try 2 - prevent logging config clobbering

Previous behavior: loading this repository with `torch.hub.load` clobbers the existing logging configuration by modifying the root logger's configuration.
New behavior: loading this repository with `torch.hub.load` only clobbers the logging configuration for logger `yolov5` and its descendants. This is done in a way compatible with Google Colab

Signed-off-by: Ryan Echols <ryan@shadylakemedia.com>

* chore: fill in comment

no-op so a pre-commit hook can auto-format files

Signed-off-by: Ryan Echols <ryan@shadylakemedia.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Signed-off-by: Ryan Echols <ryan@shadylakemedia.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
rkechols and pre-commit-ci[bot] committed Nov 17, 2022
1 parent 1510111 commit ff6e6e3
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import glob
import inspect
import logging
import logging.config
import math
import os
import platform
Expand Down Expand Up @@ -111,23 +112,33 @@ def is_writeable(dir, test=False):
return False


def set_logging(name=None, verbose=VERBOSE):
# Sets level and returns logger
if is_kaggle() or is_colab():
for h in logging.root.handlers:
logging.root.removeHandler(h) # remove all handlers associated with the root logger object
rank = int(os.getenv('RANK', -1)) # rank in world for Multi-GPU trainings
level = logging.INFO if verbose and rank in {-1, 0} else logging.ERROR
log = logging.getLogger(name)
log.setLevel(level)
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(message)s"))
handler.setLevel(level)
log.addHandler(handler)
LOGGING_NAME = "yolov5"


set_logging() # run before defining LOGGER
LOGGER = logging.getLogger("yolov5") # define globally (used in train.py, val.py, detect.py, etc.)
def set_logging(name=LOGGING_NAME, verbose=True):
# sets up logging for the given name
rank = int(os.getenv('RANK', -1)) # rank in world for Multi-GPU trainings
level = logging.INFO if verbose and rank in {-1, 0} else logging.ERROR
logging.config.dictConfig({
"version": 1,
"disable_existing_loggers": False,
"formatters": {
name: {
"format": "%(message)s"}},
"handlers": {
name: {
"class": "logging.StreamHandler",
"formatter": name,
"level": level,}},
"loggers": {
name: {
"level": level,
"handlers": [name],
"propagate": False,}}})


set_logging(LOGGING_NAME) # run before defining LOGGER
LOGGER = logging.getLogger(LOGGING_NAME) # define globally (used in train.py, val.py, detect.py, etc.)
if platform.system() == 'Windows':
for fn in LOGGER.info, LOGGER.warning:
setattr(LOGGER, fn.__name__, lambda x: fn(emojis(x))) # emoji safe logging
Expand Down

0 comments on commit ff6e6e3

Please sign in to comment.