code: def add_handler(
      logger: logging.Logger, file: Path | None = None
  ) -> logging.Handler:
      """
      Add a logging handler.
      If ``file`` is specified, log to file.
      Otherwise, add a handler to stdout.
      """
      fmt = "[%(asctime)s] %(levelname)s (%(threadName)s:%(name)s) %(message)s"
      datefmt = "%Y-%m-%d %H:%M:%S"
  
      formatter = logging.Formatter(fmt, datefmt)
      if file is None:
          # Log to stdout.
          handler = logging.StreamHandler(sys.stdout)
      else:
          handler = logging.FileHandler(file)
      handler.setLevel(level=logging.DEBUG)  # Print all the logs.
      handler.setFormatter(formatter)
  
      logger.addHandler(handler)
  
      return handler
  
file_path: nni/nni/contrib/training_service/utils.py
imports: from __future__ import annotations
  import logging
  import signal
  import sys
  import time
  from pathlib import Path
  from subprocess import Popen, PIPE
label_desc: Use image node_id 4 for calling a global function with example usage: add_handler(logger, file) and returns: handler