Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

log_utils.py 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. msg = f"{logfile_basename} log path: {log_path}"
  46. logger.info(msg)