Skip to content

Commit

Permalink
improve the format of error, always json
Browse files Browse the repository at this point in the history
  • Loading branch information
wjo1212 committed Dec 25, 2017
1 parent 94f4443 commit a4452df
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 27 deletions.
17 changes: 12 additions & 5 deletions aliyun/log/logexception.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Copyright (C) Alibaba Cloud Computing
# All rights reserved.

import json


class LogException(Exception):
"""The Exception of the log request & response.
Expand All @@ -27,13 +29,18 @@ def __init__(self, errorCode, errorMessage, requestId='', resp_status=200, resp_
self.resp_body = resp_body

if not self.resp_body:
self.resp_body = r'{"ErrorCode": "%s", "ErrorMessage": "%s"}, "RequestID": "%s"' % (errorCode,
errorMessage,
requestId)
self.resp_body = json.dumps({
"errorCode": self._errorCode,
"errorMessage": self._errorMessage,
"requestId": self._requestId
})

def __str__(self):
return "LogException: \n{\n ErrorCode: %s\n ErrorMessage: %s\n RequestId: %s\n}\n" \
% (self._errorCode, self._errorMessage, self._requestId)
return json.dumps({
"errorCode": self._errorCode,
"errorMessage": self._errorMessage,
"requestId": self._requestId
})

def get_error_code(self):
""" return error code of exception
Expand Down
75 changes: 55 additions & 20 deletions aliyun/log/logtail_config_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import time

from .util import Util
import copy


class LogtailConfigDetail(object):
Expand All @@ -18,7 +19,7 @@ class LogtailConfigDetail(object):
:param logstore_name: the logstore name for the config
:type endpoint: string
:param endpoint: log data endpoint, it should base on the region of this project
:param endpoint: deprecated, set it as empty
:type log_path: string
:param log_path: the log file dir path
Expand All @@ -44,13 +45,20 @@ class LogtailConfigDetail(object):
:type logSample: string
:param logSample: sample strings for the log, up to 1000 bytes
:type log_type: string
:param log_type: common_reg_log, delimiter_log, apsara_log etc.
:type extended_items: dict
:param extended_items: extended items in dict format e.g. enableRawLog etc. refer to wiki page.
https://help.aliyun.com/document_detail/29042.html?spm=5176.doc28997.6.744.C583Jg
"""

def __init__(self, config_name, logstore_name, endpoint, log_path, file_pattern, log_begin_regex, topic_format,
filter_keys, filter_keys_reg, logSample=''):
filter_keys, filter_keys_reg, logSample='', log_type='common_reg_log', **extended_items):
self.config_name = config_name
self.logstore_name = logstore_name
self.endpoint = endpoint
self.endpoint = endpoint or ''
self.log_path = log_path
self.file_pattern = file_pattern

Expand All @@ -61,13 +69,19 @@ def __init__(self, config_name, logstore_name, endpoint, log_path, file_pattern,
self.create_time = int(time.time())
self.last_modify_time = int(time.time())
self.logSample = logSample
self.log_type = log_type or 'common_reg_log'
self.extended_items = extended_items

def set_create_time(self, create_time):
self.create_time = create_time

def set_last_modify_time(self, last_modify_time):
self.last_modify_time = last_modify_time

@staticmethod
def from_json(json_value):
return LogtailConfigHelper.generate_logtail_config(json_value)


class CommonRegLogConfigDetail(LogtailConfigDetail):
"""The logtail config for common_reg_log
Expand All @@ -79,7 +93,7 @@ class CommonRegLogConfigDetail(LogtailConfigDetail):
:param logstore_name: the logstore name for the config
:type endpoint: string
:param endpoint: log data endpoint, it should base on the region of this project
:param endpoint: log data endpoint, deprecated, set it as empty
:type log_path: string
:param log_path: the log file dir path
Expand Down Expand Up @@ -111,20 +125,27 @@ class CommonRegLogConfigDetail(LogtailConfigDetail):
:type logSample: string
:param logSample: sample strings for the log, up to 1000 bytes
:type log_type: string
:param log_type: common_reg_log, delimiter_log, apsara_log etc.
:type extended_items: dict
:param extended_items: extended items in dict format e.g. enableRawLog etc. refer to wiki page.
https://help.aliyun.com/document_detail/29042.html?spm=5176.doc28997.6.744.C583Jg
"""

def __init__(self, config_name, logstore_name, endpoint, log_path, file_pattern, time_format, log_begin_regex,
log_parse_regex, reg_keys,
topic_format="none", filter_keys=None, filter_keys_reg=None, logSample=''):
topic_format="none", filter_keys=None, filter_keys_reg=None, logSample='',
log_type='common_reg_log', **extended_items):
if filter_keys is None:
filter_keys = []
if filter_keys_reg is None:
filter_keys_reg = []

LogtailConfigDetail.__init__(self, config_name, logstore_name, endpoint, log_path, file_pattern,
log_begin_regex,
topic_format, filter_keys, filter_keys_reg, logSample)
topic_format, filter_keys, filter_keys_reg, logSample, log_type, **extended_items)

self.time_format = time_format
self.log_parse_regex = log_parse_regex
Expand All @@ -137,14 +158,15 @@ def to_json(self):
if self.logSample:
json_value["logSample"] = self.logSample

detail = {'logType': 'common_reg_log', 'logPath': self.log_path, 'filePattern': self.file_pattern,
detail = {'logType': self.log_type, 'logPath': self.log_path, 'filePattern': self.file_pattern,
'localStorage': True, 'timeFormat': self.time_format, 'logBeginRegex': self.log_begin_regex,
'regex': self.log_parse_regex, 'key': self.keys, 'filterKey': self.filter_keys,
'filterRegex': self.filter_keys_reg, 'topicFormat': self.topic_format}
detail.update(self.extended_items)
json_value["inputDetail"] = detail
json_value["outputType"] = "LogService"
output_detail = {}
if self.endpoint is not None:
if self.endpoint:
output_detail["endpoint"] = self.endpoint
output_detail["logstoreName"] = self.logstore_name
json_value['outputDetail'] = output_detail
Expand All @@ -160,7 +182,7 @@ class ApsaraLogConfigDetail(LogtailConfigDetail):
:param logstore_name: the logstore name for the config
:type endpoint: string
:param endpoint: log data endpoint, it should base on the region of this project
:param endpoint: deprecated, set it as empty
:type log_path: string
:param log_path: the log file dir path
Expand All @@ -179,18 +201,24 @@ class ApsaraLogConfigDetail(LogtailConfigDetail):
:type filter_keys_reg: string list
:param filter_keys_reg: the regex for filter_keys to filter the log, filter_keys_reg[i] is for filter_keys[i]. The size of filter_keys_reg and filter_keys should be same. If a log is matched only if the size of filter_keys is 0, or all the value of the related keys in filter_keys, match the regex set in filter_keys_reg
:type extended_items: dict
:param extended_items: extended items in dict format e.g. enableRawLog etc. refer to wiki page.
https://help.aliyun.com/document_detail/29042.html?spm=5176.doc28997.6.744.C583Jg
"""

def __init__(self, config_name, logstore_name, endpoint, log_path, file_pattern,
log_begin_regex=r'\[\d+-\d+-\d+ \d+:\d+:\d+\.\d+.*', topic_format="none", filter_keys=None,
filter_keys_reg=None, logSample=''):
filter_keys_reg=None, logSample='', **extended_items):
if filter_keys_reg is None:
filter_keys_reg = []
if filter_keys is None:
filter_keys = []
LogtailConfigDetail.__init__(self, config_name, logstore_name, endpoint, log_path, file_pattern,
log_begin_regex,
topic_format, filter_keys, filter_keys_reg, logSample)
topic_format, filter_keys, filter_keys_reg, logSample,
'apsara_log', **extended_items)

def to_json(self):
json_value = {"configName": self.config_name, "inputType": "file"}
Expand All @@ -201,6 +229,7 @@ def to_json(self):
detail = {'logType': 'apsara_log', 'logPath': self.log_path, 'filePattern': self.file_pattern,
'localStorage': True, 'logBeginRegex': self.log_begin_regex, 'timeFormat': '',
'filterKey': self.filter_keys, 'filterRegex': self.filter_keys_reg, 'topicFormat': self.topic_format}
detail.update(self.extended_items) # add more extended items
json_value["inputDetail"] = detail
json_value["outputType"] = "log"
output_detail = {}
Expand All @@ -223,27 +252,33 @@ def generate_common_reg_log_config(json_value):
:param json_value:
:return:
"""
input_detail = json_value['inputDetail']
input_detail = copy.deepcopy(json_value['inputDetail'])
output_detail = json_value['outputDetail']
logSample = json_value.get('logSample', '')
config_name = json_value['configName']
logstore_name = output_detail['logstoreName']
endpoint = output_detail.get('endpoint', None)
endpoint = output_detail.get('endpoint', '')

log_path = input_detail['logPath']
file_pattern = input_detail['filePattern']

time_format = input_detail['timeFormat']
log_begin_regex = input_detail.get('logBeginRegex', '')
log_parse_regex = input_detail['regex']
log_parse_regex = input_detail.get('regex', '')
reg_keys = input_detail['key']
topic_format = input_detail['topicFormat']
filter_keys = input_detail['filterKey']
filter_keys_reg = input_detail['filterRegex']
log_type = input_detail.get('logType')

for item in ('logPath', 'filePattern', 'timeFormat', 'logBeginRegex', 'regex', 'key',
'topicFormat', 'filterKey', 'filterRegex', 'logType'):
if item in input_detail:
del input_detail[item]

config = CommonRegLogConfigDetail(config_name, logstore_name, endpoint, log_path, file_pattern, time_format,
log_begin_regex, log_parse_regex, reg_keys,
topic_format, filter_keys, filter_keys_reg, logSample)
topic_format, filter_keys, filter_keys_reg, logSample,
log_type, **input_detail)
return config

@staticmethod
Expand All @@ -259,7 +294,7 @@ def generate_apsara_log_config(json_value):
logSample = json_value.get('logSample', '')

logstore_name = output_detail['logstoreName']
endpoint = output_detail.get('endpoint')
endpoint = output_detail.get('endpoint', '')
log_path = input_detail['logPath']
file_pattern = input_detail['filePattern']

Expand All @@ -279,6 +314,6 @@ def generate_logtail_config(json_value):
:param json_value:
:return:
"""
if json_value['inputDetail']['logType'] == 'common_reg_log':
return LogtailConfigHelper.generate_common_reg_log_config(json_value)
return LogtailConfigHelper.generate_apsara_log_config(json_value)
if json_value['inputDetail']['logType'] == 'apsara_log':
return LogtailConfigHelper.generate_apsara_log_config(json_value)
return LogtailConfigHelper.generate_common_reg_log_config(json_value)
2 changes: 1 addition & 1 deletion aliyun/log/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.6.15'
__version__ = '0.6.16'
USER_AGENT = 'log-python-sdk-v-' + __version__
API_VERSION = '0.6.0'

Expand Down
2 changes: 1 addition & 1 deletion tests/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def sample_index(client, project, logstore):
def sample_logtail_config(client, project, logstore):
logtail_config_name = logstore + "-stt1-logtail"
logtail_config = CommonRegLogConfigDetail(logtail_config_name, logstore,
"http://cn-hangzhou-devcommon-intranet.sls.aliyuncs.com", "/apsara/xxx",
"", "/apsara/xxx",
"*.LOG",
r"%Y-%m-%d %H:%M:%S", "xxx.*", "xxx ([\w\-]+\s[\d\:]+)\s+(.*)", ["time", "value"],
logSample="xxx 2017-11-11 11:11:11 hello alicloud.")
Expand Down

0 comments on commit a4452df

Please sign in to comment.