Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_levels: 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. pkg_levels = {}
  44. for pkg_name_level in log_levels.split(","):
  45. terms = pkg_name_level.split("=")
  46. if len(terms)!= 2:
  47. continue
  48. pkg_name, pkg_level = terms[0], terms[1]
  49. pkg_name = pkg_name.strip()
  50. pkg_level = logging.getLevelName(pkg_level.strip().upper())
  51. if not isinstance(pkg_level, int):
  52. pkg_level = logging.INFO
  53. pkg_levels[pkg_name] = logging.getLevelName(pkg_level)
  54. for pkg_name in ['peewee', 'pdfminer']:
  55. if pkg_name not in pkg_levels:
  56. pkg_levels[pkg_name] = logging.getLevelName(logging.WARNING)
  57. if 'root' not in pkg_levels:
  58. pkg_levels['root'] = logging.getLevelName(logging.INFO)
  59. for pkg_name, pkg_level in pkg_levels.items():
  60. pkg_logger = logging.getLogger(pkg_name)
  61. pkg_logger.setLevel(pkg_level)
  62. msg = f"{logfile_basename} log path: {log_path}, log levels: {pkg_levels}"
  63. logger.info(msg)