Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

log_utils.py 1.8KB

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(script_path: 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. script_name = os.path.basename(script_path)
  34. log_path = os.path.abspath(os.path.join(get_project_base_directory(), "logs", f"{os.path.splitext(script_name)[0]}.log"))
  35. os.makedirs(os.path.dirname(log_path), exist_ok=True)
  36. logger.setLevel(log_level)
  37. formatter = logging.Formatter(log_format)
  38. handler1 = RotatingFileHandler(log_path, maxBytes=10*1024*1024, backupCount=5)
  39. handler1.setLevel(log_level)
  40. handler1.setFormatter(formatter)
  41. logger.addHandler(handler1)
  42. handler2 = logging.StreamHandler()
  43. handler2.setLevel(log_level)
  44. handler2.setFormatter(formatter)
  45. logger.addHandler(handler2)
  46. msg = f"{script_name} log path: {log_path}"
  47. logger.info(msg)