1import logging
2try:
3    import simplejson as json
4except ImportError:
5    import json
6
7from ..exceptions import TransportError, HTTP_EXCEPTIONS
8
9logger = logging.getLogger('elasticsearch')
10
11# create the elasticsearch.trace logger, but only set propagate to False if the
12# logger hasn't already been configured
13_tracer_already_configured = 'elasticsearch.trace' in logging.Logger.manager.loggerDict
14tracer = logging.getLogger('elasticsearch.trace')
15if not _tracer_already_configured:
16    tracer.propagate = False
17
18
19class Connection(object):
20    """
21    Class responsible for maintaining a connection to an Elasticsearch node. It
22    holds persistent connection pool to it and it's main interface
23    (`perform_request`) is thread-safe.
24
25    Also responsible for logging.
26    """
27    transport_schema = 'http'
28
29    def __init__(self, host='localhost', port=9200, use_ssl=False, url_prefix='', timeout=10, **kwargs):
30        """
31        :arg host: hostname of the node (default: localhost)
32        :arg port: port to use (integer, default: 9200)
33        :arg url_prefix: optional url prefix for elasticsearch
34        :arg timeout: default timeout in seconds (float, default: 10)
35        """
36        scheme = self.transport_schema
37        if use_ssl:
38            scheme += 's'
39        self.host = '%s://%s:%s' % (scheme, host, port)
40        if url_prefix:
41            url_prefix = '/' + url_prefix.strip('/')
42        self.url_prefix = url_prefix
43        self.timeout = timeout
44
45    def __repr__(self):
46        return '<%s: %s>' % (self.__class__.__name__, self.host)
47
48    def _pretty_json(self, data):
49        # pretty JSON in tracer curl logs
50        try:
51            return json.dumps(json.loads(data), sort_keys=True, indent=2, separators=(',', ': ')).replace("'", r'\u0027')
52        except (ValueError, TypeError):
53            # non-json data or a bulk request
54            return data
55
56    def _log_trace(self, method, path, body, status_code, response, duration):
57        if not tracer.isEnabledFor(logging.INFO) or not tracer.handlers:
58            return
59
60        # include pretty in trace curls
61        path = path.replace('?', '?pretty&', 1) if '?' in path else path + '?pretty'
62        if self.url_prefix:
63            path = path.replace(self.url_prefix, '', 1)
64        tracer.info("curl -X%s 'http://localhost:9200%s' -d '%s'", method, path, self._pretty_json(body) if body else '')
65
66        if tracer.isEnabledFor(logging.DEBUG):
67            tracer.debug('#[%s] (%.3fs)\n#%s', status_code, duration, self._pretty_json(response).replace('\n', '\n#') if response else '')
68
69    def log_request_success(self, method, full_url, path, body, status_code, response, duration):
70        """ Log a successful API call.  """
71        #  TODO: optionally pass in params instead of full_url and do urlencode only when needed
72
73        # body has already been serialized to utf-8, deserialize it for logging
74        # TODO: find a better way to avoid (de)encoding the body back and forth
75        if body:
76            body = body.decode('utf-8')
77
78        logger.info(
79            '%s %s [status:%s request:%.3fs]', method, full_url,
80            status_code, duration
81        )
82        logger.debug('> %s', body)
83        logger.debug('< %s', response)
84
85        self._log_trace(method, path, body, status_code, response, duration)
86
87    def log_request_fail(self, method, full_url, path, body, duration, status_code=None, response=None, exception=None):
88        """ Log an unsuccessful API call.  """
89        # do not log 404s on HEAD requests
90        if method == 'HEAD' and status_code == 404:
91            return
92        logger.warning(
93            '%s %s [status:%s request:%.3fs]', method, full_url,
94            status_code or 'N/A', duration, exc_info=exception is not None
95        )
96
97        # body has already been serialized to utf-8, deserialize it for logging
98        # TODO: find a better way to avoid (de)encoding the body back and forth
99        if body:
100            body = body.decode('utf-8')
101
102        logger.debug('> %s', body)
103
104        self._log_trace(method, path, body, status_code, response, duration)
105
106        if response is not None:
107            logger.debug('< %s', response)
108
109    def _raise_error(self, status_code, raw_data):
110        """ Locate appropriate exception and raise it. """
111        error_message = raw_data
112        additional_info = None
113        try:
114            if raw_data:
115                additional_info = json.loads(raw_data)
116                error_message = additional_info.get('error', error_message)
117                if isinstance(error_message, dict) and 'type' in error_message:
118                    error_message = error_message['type']
119        except (ValueError, TypeError) as err:
120            logger.warning('Undecodable raw error response from server: %s', err)
121
122        raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
123
124
125