Browse Source

add log to sls

liqian 3 năm trước cách đây
mục cha
commit
3ce31c8c41
2 tập tin đã thay đổi với 44 bổ sung6 xóa
  1. 12 4
      config.py
  2. 32 2
      log.py

+ 12 - 4
config.py

@@ -1,6 +1,6 @@
 import os
-from log import Log
-log_ = Log()
+# from log import Log
+# log_ = Log()
 
 
 class BaseConfig(object):
@@ -144,6 +144,14 @@ class DevelopmentConfig(BaseConfig):
     # 获取视频在流量池中的剩余可分发数接口地址
     GET_REMAIN_VIEW_COUNT_URL = 'http://testapi-internal.piaoquantv.com/flowpool/video/remainViewCount'
 
+    # 日志服务配置
+    ALIYUN_LOG = {
+        'ENDPOINT': 'cn-hangzhou.log.aliyuncs.com',
+        'ACCESSID': 'LTAIWYUujJAm7CbH',
+        'ACCESSKEY': 'RfSjdiWwED1sGFlsjXv0DlfTnZTG1P',
+        'PROJECT': 'rov-server-test',
+    }
+
 
 class TestConfig(BaseConfig):
     """测试环境配置"""
@@ -273,7 +281,7 @@ def set_config():
     env = os.environ.get('ROV_SERVER_ENV')
     # env = 'dev'
     if env is None:
-        log_.error('ENV ERROR: is None!')
+        # log_.error('ENV ERROR: is None!')
         return
     if env == 'dev':
         return DevelopmentConfig()
@@ -284,5 +292,5 @@ def set_config():
     elif env == 'pro':
         return ProductionConfig()
     else:
-        log_.error('ENV ERROR: is {}'.format(env))
+        # log_.error('ENV ERROR: is {}'.format(env))
         return

+ 32 - 2
log.py

@@ -1,7 +1,37 @@
-import os
 import logging
+import logging.config
+import os
 import time
 
+from config import set_config
+
+config_ = set_config()
+
+# 配置
+conf = {'version': 1,
+        'formatters': {'rawformatter': {'class': 'logging.Formatter',
+                                        'format': '%(message)s'}
+                       },
+        'handlers': {'sls_handler': {'()':
+                                     'aliyun.log.QueuedLogHandler',
+                                     'level': 'INFO',
+                                     'formatter': 'rawformatter',
+
+                                     # custom args:
+                                     'end_point': config_.ALIYUN_LOG.get('ENDPOINT', ''),
+                                     'access_key_id': config_.ALIYUN_LOG.get('ACCESSID', ''),
+                                     'access_key': config_.ALIYUN_LOG.get('ACCESSKEY', ''),
+                                     'project': config_.ALIYUN_LOG.get('PROJECT', ''),
+                                     'log_store': "info"
+                                     }
+                     },
+        'loggers': {'sls': {'handlers': ['sls_handler', ],
+                            'level': 'INFO',
+                            'propagate': False}
+                    }
+        }
+logging.config.dictConfig(conf)
+
 
 class Log(object):
     # def __init__(self, pag_source, log_path=os.path.join(os.path.dirname(os.path.realpath(__file__)), "logs")):
@@ -12,7 +42,7 @@ class Log(object):
         # 文件的命名
         # self.logname = os.path.join(log_path, '{}_{}.log'.format(pag_source, time.strftime('%Y%m%d')))
         self.logname = os.path.join(log_path, '{}.log'.format(time.strftime('%Y%m%d')))
-        self.logger = logging.getLogger()
+        self.logger = logging.getLogger('sls')
         self.logger.setLevel(logging.DEBUG)
         # 日志输出格式
         self.formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')