code: def _add_file_handler(
      _logger: logging.Logger,
      path: Optional[Union[str, Path]] = None,
      level: str = "INFO",
      remove_other_handlers: bool = False,
      mode: str = "w",
  ):
      if path is None:
          path = Path.cwd()
      if isinstance(path, str):
          path = Path(path)
      if not isinstance(path, Path):
          raise TypeError(
              "Parameter `path` can only be `str` or `pathlib.Path` type."
          )
      if not path.exists():
          head, tail = os.path.splitext(path)
          if tail == "":  # 说明没有后缀，理解为是一个folder
              path.mkdir(parents=True, exist_ok=True)
          else:
              # 主进程会帮助我们创建文件夹，但是由于主从进程几乎是同步的，因此到这里时子进程也会尝试创建文件夹，即使主进程会做这件事情；
              dirname = os.path.dirname(path)
              os.makedirs(dirname, exist_ok=True)
      if path.is_dir():
          if os.environ.get(FASTNLP_BACKEND_LAUNCH, "0") == "1":
              # 如果是通过 python -m xxx.launch 等启动的话，FASTNLP_LAUNCH_TIME这个名称可能是不一致的。
              path = path.joinpath(
                  f"RANK-{os.environ.get(FASTNLP_GLOBAL_RANK, '0')}-"
                  + os.environ.get(FASTNLP_LAUNCH_TIME)
                  + ".log"
              )
          else:
              path = path.joinpath(
                  os.environ.get(FASTNLP_LAUNCH_TIME) + ".log"
              )
  
      if not isinstance(remove_other_handlers, bool):
          raise TypeError(
              "Parameter `remove_other_handlers` can only be `bool` type."
          )
  
      if not isinstance(mode, str):
          raise TypeError(
              "Parameter 'evaluate_fn' can only be `str` type."
          )
      if mode not in {"w", "a"}:
          raise ValueError(
              "Parameter `evaluate_fn` can only be one of these values: ('w', 'a')."
          )
  
      for h in _logger.handlers:
          if isinstance(h, logging.FileHandler):
              if os.path.abspath(path) == h.baseFilename:
                  # file path already added
                  return
  
      # File Handler
      if int(os.environ.get(FASTNLP_GLOBAL_RANK, 0)) == 0:
          if os.path.exists(path):
              assert os.path.isfile(path)
              warnings.warn("log already exists in {}".format(path))
  
      dirname = os.path.abspath(os.path.dirname(path))
      os.makedirs(dirname, exist_ok=True)
  
      # 这里只要检测到是分布式训练，我们就将 evaluate_fn 改为 "a"；这样会导致的一个问题在于，如果第二次训练也是分布式训练，logger记录的log不会重新
      #  覆盖掉原文件，而是会接着上一次的 log 继续添加；
      # 这样做主要是为了解决这样的情形所导致的问题：在分布式训练中，进程 1 比 进程 0 先运行到这里，然后使得进程 0 将进程 1 的 log 覆盖掉；
      if (
          is_cur_env_distributed()
      ):  # and int(os.environ.get(FASTNLP_GLOBAL_RANK, 0)) != 0:
          mode = "a"
  
      file_handler = logging.FileHandler(path, mode=mode)
      logger.info(f"Writing log to file:{os.path.abspath(path)}")
      file_handler.setLevel(_get_level(level))
  
      if is_cur_env_distributed():
          file_formatter = logging.Formatter(
              fmt="Rank: %(rank)s - %(asctime)s - %(module)s - [%(levelname)s] - %(message)s",
              datefmt="%Y/%m/%d %H:%M:%S",
          )
      else:
          file_formatter = logging.Formatter(
              fmt="%(asctime)s - %(module)s - [%(levelname)s] - %(message)s",
              datefmt="%Y/%m/%d %H:%M:%S",
          )
  
      file_handler.setFormatter(file_formatter)
      _logger.addHandler(file_handler)
  
      if remove_other_handlers:
          _need_remove_handlers = []
          for i, h in enumerate(_logger.handlers):
              if not isinstance(h, logging.FileHandler):
                  _need_remove_handlers.append(h)
          for handler in _need_remove_handlers:
              _logger.removeHandler(handler)
  
      return file_handler
  
imports: import logging
  import logging.config
  from logging import DEBUG, ERROR, INFO, WARNING, CRITICAL, raiseExceptions
  import os
  import sys
  import warnings
  from pathlib import Path
  from typing import Optional, Union
  from rich.logging import RichHandler
  from fastNLP.core.log.handler import StdoutStreamHandler, TqdmLoggingHandler
  from fastNLP.envs.env import FASTNLP_LOG_LEVEL, FASTNLP_GLOBAL_RANK, FASTNLP_LAUNCH_TIME, FASTNLP_BACKEND_LAUNCH
  from fastNLP.envs.distributed import is_cur_env_distributed
label_desc: Use image node_id 2 for calling a global function with example usage: _add_file_handler(_logger, path, level, remove_other_handlers, mode) and returns: file_handler