Skip to content

Commit

Permalink
Make it possible to propagate logs to parent logger and disable stdou…
Browse files Browse the repository at this point in the history
…t logging.

Summary: In order to make it possible to use a single central logger that will capture all logs to stderr/stdout while still using logging to a file through D2, add parameters. Due to the default values set, the behavior should not be changed.

Reviewed By: miqueljubert, wat3rBro

Differential Revision: D44278486

fbshipit-source-id: 04fcb48d6f979f01d000b1adc77e470956d71683
  • Loading branch information
crassirostris authored and facebook-github-bot committed Mar 31, 2023
1 parent af614bf commit d779ea6
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions detectron2/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ def formatMessage(self, record):

@functools.lru_cache() # so that calling setup_logger multiple times won't add many handlers
def setup_logger(
output=None, distributed_rank=0, *, color=True, name="detectron2", abbrev_name=None
output=None,
distributed_rank=0,
*,
color=True,
name="detectron2",
abbrev_name=None,
enable_propagation: bool = False,
configure_stdout: bool = True
):
"""
Initialize the detectron2 logger and set its verbosity level to "DEBUG".
Expand All @@ -55,13 +62,16 @@ def setup_logger(
Set to "" to not log the root module in logs.
By default, will abbreviate "detectron2" to "d2" and leave other
modules unchanged.
enable_propagation (bool): whether to propagate logs to the parent logger.
configure_stdout (bool): whether to configure logging to stdout.
Returns:
logging.Logger: a logger
"""
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
logger.propagate = False
logger.propagate = enable_propagation

if abbrev_name is None:
abbrev_name = "d2" if name == "detectron2" else name
Expand All @@ -70,7 +80,7 @@ def setup_logger(
"[%(asctime)s] %(name)s %(levelname)s: %(message)s", datefmt="%m/%d %H:%M:%S"
)
# stdout logging: master only
if distributed_rank == 0:
if configure_stdout and distributed_rank == 0:
ch = logging.StreamHandler(stream=sys.stdout)
ch.setLevel(logging.DEBUG)
if color:
Expand Down

0 comments on commit d779ea6

Please sign in to comment.