You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import os
  17. import os.path
  18. import logging
  19. from logging.handlers import RotatingFileHandler
  20. def get_project_base_directory():
  21. PROJECT_BASE = os.path.abspath(
  22. os.path.join(
  23. os.path.dirname(os.path.realpath(__file__)),
  24. os.pardir,
  25. os.pardir,
  26. )
  27. )
  28. return PROJECT_BASE
  29. def initRootLogger(logfile_basename: str, log_level: int = logging.INFO, log_format: str = "%(asctime)-15s %(levelname)-8s %(process)d %(message)s"):
  30. logger = logging.getLogger()
  31. if logger.hasHandlers():
  32. return
  33. log_path = os.path.abspath(os.path.join(get_project_base_directory(), "logs", f"{logfile_basename}.log"))
  34. os.makedirs(os.path.dirname(log_path), exist_ok=True)
  35. logger.setLevel(log_level)
  36. formatter = logging.Formatter(log_format)
  37. handler1 = RotatingFileHandler(log_path, maxBytes=10*1024*1024, backupCount=5)
  38. handler1.setLevel(log_level)
  39. handler1.setFormatter(formatter)
  40. logger.addHandler(handler1)
  41. handler2 = logging.StreamHandler()
  42. handler2.setLevel(log_level)
  43. handler2.setFormatter(formatter)
  44. logger.addHandler(handler2)
  45. logging.captureWarnings(True)
  46. msg = f"{logfile_basename} log path: {log_path}"
  47. logger.info(msg)