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.

log_utils.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_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. formatter = logging.Formatter(log_format)
  36. handler1 = RotatingFileHandler(log_path, maxBytes=10*1024*1024, backupCount=5)
  37. handler1.setFormatter(formatter)
  38. logger.addHandler(handler1)
  39. handler2 = logging.StreamHandler()
  40. handler2.setFormatter(formatter)
  41. logger.addHandler(handler2)
  42. logging.captureWarnings(True)
  43. LOG_LEVELS = os.environ.get("LOG_LEVELS", "")
  44. pkg_levels = {}
  45. for pkg_name_level in LOG_LEVELS.split(","):
  46. terms = pkg_name_level.split("=")
  47. if len(terms)!= 2:
  48. continue
  49. pkg_name, pkg_level = terms[0], terms[1]
  50. pkg_name = pkg_name.strip()
  51. pkg_level = logging.getLevelName(pkg_level.strip().upper())
  52. if not isinstance(pkg_level, int):
  53. pkg_level = logging.INFO
  54. pkg_levels[pkg_name] = logging.getLevelName(pkg_level)
  55. for pkg_name in ['peewee', 'pdfminer']:
  56. if pkg_name not in pkg_levels:
  57. pkg_levels[pkg_name] = logging.getLevelName(logging.WARNING)
  58. if 'root' not in pkg_levels:
  59. pkg_levels['root'] = logging.getLevelName(logging.INFO)
  60. for pkg_name, pkg_level in pkg_levels.items():
  61. pkg_logger = logging.getLogger(pkg_name)
  62. pkg_logger.setLevel(pkg_level)
  63. msg = f"{logfile_basename} log path: {log_path}, log levels: {pkg_levels}"
  64. logger.info(msg)