123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- """
- @File : log_config.py
- @Author: lee
- @Date : 2022/7/13/0013 11:08:55
- @Desc :
- """
- import logging
- import os
- import sys
- LOGGING_CONFIG = dict(
- version=1,
- disable_existing_loggers=False,
- loggers={
- # 新曾自定义日志,用于数据采集程序
- "general": {
- "level": "INFO",
- "handlers": ["console", "general"],
- "propagate": True,
- "qualname": "general.debug",
- },
- "modbus_connector": {
- "level": "INFO",
- "handlers": ["console", "modbus_connector"],
- "propagate": True,
- "qualname": "modbus_connector.debug",
- },
- "shuizhi_tcp_connector": {
- "level": "INFO",
- "handlers": ["console", "shuizhi_tcp_connector"],
- "propagate": True,
- "qualname": "shuizhi_tcp_connector.debug",
- },
- "tcp_connector": {
- "level": "INFO",
- "handlers": ["console", "tcp_connector"],
- "propagate": True,
- "qualname": "tcp_connector.debug",
- },
- "sm140_converter": {
- "level": "DEBUG",
- "handlers": ["console", "sm140_converter"],
- "propagate": True,
- "qualname": "sm140_converter.debug",
- },
- "wxt536_converter": {
- "level": "DEBUG",
- "handlers": ["console", "wxt536_converter"],
- "propagate": True,
- "qualname": "wxt536_converter.debug",
- },
- "adcp_converter": {
- "level": "DEBUG",
- "handlers": ["console", "adcp_converter"],
- "propagate": True,
- "qualname": "adcp_converter.debug",
- },
- "cec21_converter": {
- "level": "DEBUG",
- "handlers": ["console", "cec21_converter"],
- "propagate": True,
- "qualname": "cec21_converter.debug",
- },
- "td266_converter": {
- "level": "DEBUG",
- "handlers": ["console", "td266_converter"],
- "propagate": True,
- "qualname": "td266_converter.debug",
- },
- "modbus_converter": {
- "level": "DEBUG",
- "handlers": ["console", "modbus_converter"],
- "propagate": True,
- "qualname": "modbus_converter.debug",
- },
- "shuizhi_converter": {
- "level": "DEBUG",
- "handlers": ["console", "shuizhi_converter"],
- "propagate": True,
- "qualname": "shuizhi_converter.debug",
- },
- },
- handlers={
- # 数据采集程序控制台输出handler
- "console": {
- "class": "logging.StreamHandler",
- "formatter": "generic",
- "stream": sys.stdout,
- },
- "general": {
- 'class': 'logging.handlers.RotatingFileHandler',
- 'filename': 'log/general/general.log',
- 'maxBytes': 10 * 1024 * 1024,
- 'delay': True,
- "formatter": "generic",
- "backupCount": 20,
- "encoding": "utf-8"
- },
- "modbus_connector": {
- 'class': 'logging.handlers.RotatingFileHandler',
- 'filename': 'log/modbus_connector/modbus_connector.log',
- 'maxBytes': 10 * 1024 * 1024,
- 'delay': True,
- "formatter": "generic",
- "backupCount": 20,
- "encoding": "utf-8"
- },
- "shuizhi_tcp_connector": {
- 'class': 'logging.handlers.RotatingFileHandler',
- 'filename': 'log/shuizhi_tcp_connector/shuizhi_tcp_connector.log',
- 'maxBytes': 10 * 1024 * 1024,
- 'delay': True,
- "formatter": "generic",
- "backupCount": 20,
- "encoding": "utf-8"
- },
- "tcp_connector": {
- 'class': 'logging.handlers.RotatingFileHandler',
- 'filename': 'log/tcp_connector/tcp_connector.log',
- 'maxBytes': 10 * 1024 * 1024,
- 'delay': True,
- "formatter": "generic",
- "backupCount": 20,
- "encoding": "utf-8"
- },
- "sm140_converter": {
- 'class': 'logging.handlers.RotatingFileHandler',
- 'filename': 'log/sm140_converter/sm140_converter.log',
- 'maxBytes': 10 * 1024 * 1024,
- 'delay': True,
- "formatter": "generic",
- "backupCount": 20,
- "encoding": "utf-8"
- },
- "wxt536_converter": {
- 'class': 'logging.handlers.RotatingFileHandler',
- 'filename': 'log/wxt536_converter/wxt536_converter.log',
- 'maxBytes': 10 * 1024 * 1024,
- 'delay': True,
- "formatter": "generic",
- "backupCount": 20,
- "encoding": "utf-8"
- },
- "adcp_converter": {
- 'class': 'logging.handlers.RotatingFileHandler',
- 'filename': 'log/adcp_converter/adcp_converter.log',
- 'maxBytes': 10 * 1024 * 1024,
- 'delay': True,
- "formatter": "generic",
- "backupCount": 20,
- "encoding": "utf-8"
- },
- "cec21_converter": {
- 'class': 'logging.handlers.RotatingFileHandler',
- 'filename': 'log/cec21_converter/cec21_converter.log',
- 'maxBytes': 10 * 1024 * 1024,
- 'delay': True,
- "formatter": "generic",
- "backupCount": 20,
- "encoding": "utf-8"
- },
- "td266_converter": {
- 'class': 'logging.handlers.RotatingFileHandler',
- 'filename': 'log/td266_converter/td266_converter.log',
- 'maxBytes': 10 * 1024 * 1024,
- 'delay': True,
- "formatter": "generic",
- "backupCount": 20,
- "encoding": "utf-8"
- },
- "modbus_converter": {
- 'class': 'logging.handlers.RotatingFileHandler',
- 'filename': 'log/modbus_converter/modbus_converter.log',
- 'maxBytes': 10 * 1024 * 1024,
- 'delay': True,
- "formatter": "generic",
- "backupCount": 20,
- "encoding": "utf-8"
- },
- "shuizhi_converter": {
- 'class': 'logging.handlers.RotatingFileHandler',
- 'filename': 'log/shuizhi_converter/shuizhi_converter.log',
- 'maxBytes': 10 * 1024 * 1024,
- 'delay': True,
- "formatter": "generic",
- "backupCount": 20,
- "encoding": "utf-8"
- },
- },
- formatters={
- # 自定义文件格式化器
- "generic": {
- "format": "%(asctime)s {%(process)d(%(thread)d)} [%(filename)s:%(lineno)d] [%(levelname)s] %(message)s",
- "datefmt": "[%Y-%m-%d %H:%M:%S]",
- "class": "logging.Formatter",
- },
- },
- )
- general = logging.getLogger("general")
- modbus_connector = logging.getLogger("modbus_connector")
- tcp_connector = logging.getLogger("tcp_connector")
- shuizhi_tcp_connector = logging.getLogger("shuizhi_tcp_connector")
- wxt536_converter = logging.getLogger("wxt536_converter")
- sm140_converter = logging.getLogger("sm140_converter")
- td266_converter = logging.getLogger("td266_converter")
- shuizhi_converter = logging.getLogger("shuizhi_converter")
- adcp_converter = logging.getLogger("adcp_converter")
- cec21_converter = logging.getLogger("cec21_converter")
- modbus_converter = logging.getLogger("modbus_converter")
- # 根据连接器、解析器生成日志目录---------------------------------------
- from event_storage import EventStorage
- gateway_storage = EventStorage()
- connector_config = gateway_storage.get_connector_config()
- handlers = LOGGING_CONFIG['handlers']
- modules = []
- for module in connector_config:
- connector_module = module['connector_module']
- converter_module = module['converter_module']
- modules.append(converter_module)
- modules.append(connector_module)
- for handler in handlers:
- if handler in modules or handler == 'general' or handler == 'sm140_converter':
- print(handler)
- item = handlers[handler]
- if 'filename' in item:
- filename = item['filename']
- dirname = os.path.dirname(filename)
- if not os.path.exists(dirname):
- os.makedirs(dirname)
- print("新生成日志目录:", dirname)
|