logging.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. #
  3. import os
  4. from ..const import PROJECT_DIR, CONFIG, LOG_DIR
  5. MAX_KB_LOG_FILE = os.path.join(LOG_DIR, 'maxkb.log')
  6. DRF_EXCEPTION_LOG_FILE = os.path.join(LOG_DIR, 'drf_exception.log')
  7. UNEXPECTED_EXCEPTION_LOG_FILE = os.path.join(LOG_DIR, 'unexpected_exception.log')
  8. LOG_LEVEL = CONFIG.get_log_level()
  9. LOGGING = {
  10. 'version': 1,
  11. 'disable_existing_loggers': False,
  12. 'formatters': {
  13. 'verbose': {
  14. 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
  15. },
  16. 'main': {
  17. 'datefmt': '%Y-%m-%d %H:%M:%S',
  18. 'format': '%(asctime)s [%(module)s %(levelname)s] %(message)s',
  19. },
  20. 'exception': {
  21. 'datefmt': '%Y-%m-%d %H:%M:%S',
  22. 'format': '\n%(asctime)s [%(levelname)s] %(message)s',
  23. },
  24. 'simple': {
  25. 'format': '%(levelname)s %(message)s'
  26. },
  27. 'syslog': {
  28. 'format': 'maxkb: %(message)s'
  29. },
  30. 'msg': {
  31. 'format': '%(message)s'
  32. }
  33. },
  34. 'handlers': {
  35. 'null': {
  36. 'level': 'DEBUG',
  37. 'class': 'logging.NullHandler',
  38. },
  39. 'console': {
  40. 'level': 'DEBUG',
  41. 'class': 'logging.StreamHandler',
  42. 'formatter': 'main'
  43. },
  44. 'file': {
  45. 'encoding': 'utf8',
  46. 'level': 'DEBUG',
  47. 'class': 'common.utils.logger.DailyTimedRotatingFileHandler',
  48. 'when': 'midnight',
  49. 'interval': 1,
  50. 'backupCount': 7,
  51. 'formatter': 'main',
  52. 'filename': MAX_KB_LOG_FILE,
  53. },
  54. 'drf_exception': {
  55. 'encoding': 'utf8',
  56. 'level': 'DEBUG',
  57. 'class': 'common.utils.logger.DailyTimedRotatingFileHandler',
  58. 'formatter': 'exception',
  59. 'when': 'midnight',
  60. 'interval': 1,
  61. 'backupCount': 7,
  62. 'filename': DRF_EXCEPTION_LOG_FILE,
  63. },
  64. 'unexpected_exception': {
  65. 'encoding': 'utf8',
  66. 'level': 'DEBUG',
  67. 'class': 'common.utils.logger.DailyTimedRotatingFileHandler',
  68. 'when': 'midnight',
  69. 'interval': 1,
  70. 'backupCount': 7,
  71. 'formatter': 'exception',
  72. 'filename': UNEXPECTED_EXCEPTION_LOG_FILE,
  73. },
  74. 'syslog': {
  75. 'level': 'INFO',
  76. 'class': 'logging.NullHandler',
  77. 'formatter': 'syslog'
  78. },
  79. },
  80. 'loggers': {
  81. 'django': {
  82. 'handlers': ['null'],
  83. 'propagate': False,
  84. 'level': LOG_LEVEL,
  85. },
  86. 'django.request': {
  87. 'handlers': ['console', 'file', 'syslog'],
  88. 'level': LOG_LEVEL,
  89. 'propagate': False,
  90. },
  91. 'sqlalchemy': {
  92. 'handlers': ['console', 'file', 'syslog'],
  93. 'level': "ERROR",
  94. 'propagate': False,
  95. },
  96. 'django.db.backends': {
  97. 'handlers': ['console', 'file', 'syslog'],
  98. 'propagate': False,
  99. 'level': LOG_LEVEL,
  100. },
  101. 'django.server': {
  102. 'handlers': ['console', 'file', 'syslog'],
  103. 'level': 'ERROR',
  104. 'propagate': False,
  105. },
  106. 'max_kb': {
  107. 'handlers': ['console', 'file'],
  108. 'level': LOG_LEVEL,
  109. 'propagate': False,
  110. },
  111. 'common.event': {
  112. 'handlers': ['console', 'file'],
  113. 'level': "DEBUG",
  114. 'propagate': False,
  115. },
  116. }
  117. }
  118. SYSLOG_ENABLE = CONFIG.SYSLOG_ENABLE
  119. if not os.path.isdir(LOG_DIR):
  120. os.makedirs(LOG_DIR, mode=0o700, exist_ok=True)