logging_config.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. """
  2. @File : log_config.py
  3. @Author: lee
  4. @Date : 2022/7/13/0013 11:08:55
  5. @Desc :
  6. """
  7. import logging
  8. import os
  9. import sys
  10. LOGGING_CONFIG = dict(
  11. version=1,
  12. disable_existing_loggers=False,
  13. loggers={
  14. # 新曾自定义日志,用于数据采集程序
  15. "general": {
  16. "level": "INFO",
  17. "handlers": ["console", "general"],
  18. "propagate": True,
  19. "qualname": "general.debug",
  20. },
  21. "modbus_connector": {
  22. "level": "INFO",
  23. "handlers": ["console", "modbus_connector"],
  24. "propagate": True,
  25. "qualname": "modbus_connector.debug",
  26. },
  27. "shuizhi_tcp_connector": {
  28. "level": "INFO",
  29. "handlers": ["console", "shuizhi_tcp_connector"],
  30. "propagate": True,
  31. "qualname": "shuizhi_tcp_connector.debug",
  32. },
  33. "tcp_connector": {
  34. "level": "INFO",
  35. "handlers": ["console", "tcp_connector"],
  36. "propagate": True,
  37. "qualname": "tcp_connector.debug",
  38. },
  39. "sm140_converter": {
  40. "level": "DEBUG",
  41. "handlers": ["console", "sm140_converter"],
  42. "propagate": True,
  43. "qualname": "sm140_converter.debug",
  44. },
  45. "wxt536_converter": {
  46. "level": "DEBUG",
  47. "handlers": ["console", "wxt536_converter"],
  48. "propagate": True,
  49. "qualname": "wxt536_converter.debug",
  50. },
  51. "adcp_converter": {
  52. "level": "DEBUG",
  53. "handlers": ["console", "adcp_converter"],
  54. "propagate": True,
  55. "qualname": "adcp_converter.debug",
  56. },
  57. "cec21_converter": {
  58. "level": "DEBUG",
  59. "handlers": ["console", "cec21_converter"],
  60. "propagate": True,
  61. "qualname": "cec21_converter.debug",
  62. },
  63. "td266_converter": {
  64. "level": "DEBUG",
  65. "handlers": ["console", "td266_converter"],
  66. "propagate": True,
  67. "qualname": "td266_converter.debug",
  68. },
  69. "modbus_converter": {
  70. "level": "DEBUG",
  71. "handlers": ["console", "modbus_converter"],
  72. "propagate": True,
  73. "qualname": "modbus_converter.debug",
  74. },
  75. "shuizhi_converter": {
  76. "level": "DEBUG",
  77. "handlers": ["console", "shuizhi_converter"],
  78. "propagate": True,
  79. "qualname": "shuizhi_converter.debug",
  80. },
  81. },
  82. handlers={
  83. # 数据采集程序控制台输出handler
  84. "console": {
  85. "class": "logging.StreamHandler",
  86. "formatter": "generic",
  87. "stream": sys.stdout,
  88. },
  89. "general": {
  90. 'class': 'logging.handlers.RotatingFileHandler',
  91. 'filename': 'log/general/general.log',
  92. 'maxBytes': 10 * 1024 * 1024,
  93. 'delay': True,
  94. "formatter": "generic",
  95. "backupCount": 20,
  96. "encoding": "utf-8"
  97. },
  98. "modbus_connector": {
  99. 'class': 'logging.handlers.RotatingFileHandler',
  100. 'filename': 'log/modbus_connector/modbus_connector.log',
  101. 'maxBytes': 10 * 1024 * 1024,
  102. 'delay': True,
  103. "formatter": "generic",
  104. "backupCount": 20,
  105. "encoding": "utf-8"
  106. },
  107. "shuizhi_tcp_connector": {
  108. 'class': 'logging.handlers.RotatingFileHandler',
  109. 'filename': 'log/shuizhi_tcp_connector/shuizhi_tcp_connector.log',
  110. 'maxBytes': 10 * 1024 * 1024,
  111. 'delay': True,
  112. "formatter": "generic",
  113. "backupCount": 20,
  114. "encoding": "utf-8"
  115. },
  116. "tcp_connector": {
  117. 'class': 'logging.handlers.RotatingFileHandler',
  118. 'filename': 'log/tcp_connector/tcp_connector.log',
  119. 'maxBytes': 10 * 1024 * 1024,
  120. 'delay': True,
  121. "formatter": "generic",
  122. "backupCount": 20,
  123. "encoding": "utf-8"
  124. },
  125. "sm140_converter": {
  126. 'class': 'logging.handlers.RotatingFileHandler',
  127. 'filename': 'log/sm140_converter/sm140_converter.log',
  128. 'maxBytes': 10 * 1024 * 1024,
  129. 'delay': True,
  130. "formatter": "generic",
  131. "backupCount": 20,
  132. "encoding": "utf-8"
  133. },
  134. "wxt536_converter": {
  135. 'class': 'logging.handlers.RotatingFileHandler',
  136. 'filename': 'log/wxt536_converter/wxt536_converter.log',
  137. 'maxBytes': 10 * 1024 * 1024,
  138. 'delay': True,
  139. "formatter": "generic",
  140. "backupCount": 20,
  141. "encoding": "utf-8"
  142. },
  143. "adcp_converter": {
  144. 'class': 'logging.handlers.RotatingFileHandler',
  145. 'filename': 'log/adcp_converter/adcp_converter.log',
  146. 'maxBytes': 10 * 1024 * 1024,
  147. 'delay': True,
  148. "formatter": "generic",
  149. "backupCount": 20,
  150. "encoding": "utf-8"
  151. },
  152. "cec21_converter": {
  153. 'class': 'logging.handlers.RotatingFileHandler',
  154. 'filename': 'log/cec21_converter/cec21_converter.log',
  155. 'maxBytes': 10 * 1024 * 1024,
  156. 'delay': True,
  157. "formatter": "generic",
  158. "backupCount": 20,
  159. "encoding": "utf-8"
  160. },
  161. "td266_converter": {
  162. 'class': 'logging.handlers.RotatingFileHandler',
  163. 'filename': 'log/td266_converter/td266_converter.log',
  164. 'maxBytes': 10 * 1024 * 1024,
  165. 'delay': True,
  166. "formatter": "generic",
  167. "backupCount": 20,
  168. "encoding": "utf-8"
  169. },
  170. "modbus_converter": {
  171. 'class': 'logging.handlers.RotatingFileHandler',
  172. 'filename': 'log/modbus_converter/modbus_converter.log',
  173. 'maxBytes': 10 * 1024 * 1024,
  174. 'delay': True,
  175. "formatter": "generic",
  176. "backupCount": 20,
  177. "encoding": "utf-8"
  178. },
  179. "shuizhi_converter": {
  180. 'class': 'logging.handlers.RotatingFileHandler',
  181. 'filename': 'log/shuizhi_converter/shuizhi_converter.log',
  182. 'maxBytes': 10 * 1024 * 1024,
  183. 'delay': True,
  184. "formatter": "generic",
  185. "backupCount": 20,
  186. "encoding": "utf-8"
  187. },
  188. },
  189. formatters={
  190. # 自定义文件格式化器
  191. "generic": {
  192. "format": "%(asctime)s {%(process)d(%(thread)d)} [%(filename)s:%(lineno)d] [%(levelname)s] %(message)s",
  193. "datefmt": "[%Y-%m-%d %H:%M:%S]",
  194. "class": "logging.Formatter",
  195. },
  196. },
  197. )
  198. general = logging.getLogger("general")
  199. modbus_connector = logging.getLogger("modbus_connector")
  200. tcp_connector = logging.getLogger("tcp_connector")
  201. shuizhi_tcp_connector = logging.getLogger("shuizhi_tcp_connector")
  202. wxt536_converter = logging.getLogger("wxt536_converter")
  203. sm140_converter = logging.getLogger("sm140_converter")
  204. td266_converter = logging.getLogger("td266_converter")
  205. shuizhi_converter = logging.getLogger("shuizhi_converter")
  206. adcp_converter = logging.getLogger("adcp_converter")
  207. cec21_converter = logging.getLogger("cec21_converter")
  208. modbus_converter = logging.getLogger("modbus_converter")
  209. # 根据连接器、解析器生成日志目录---------------------------------------
  210. from event_storage import EventStorage
  211. gateway_storage = EventStorage()
  212. connector_config = gateway_storage.get_connector_config()
  213. handlers = LOGGING_CONFIG['handlers']
  214. modules = []
  215. for module in connector_config:
  216. connector_module = module['connector_module']
  217. converter_module = module['converter_module']
  218. modules.append(converter_module)
  219. modules.append(connector_module)
  220. for handler in handlers:
  221. if handler in modules or handler == 'general' or handler == 'sm140_converter':
  222. item = handlers[handler]
  223. if 'filename' in item:
  224. filename = item['filename']
  225. dirname = os.path.dirname(filename)
  226. if not os.path.exists(dirname):
  227. os.makedirs(dirname)
  228. print("新生成日志目录:", dirname)