Skip to content

Commit

Permalink
Add logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
heavenshell committed Mar 7, 2015
1 parent 2d4352c commit 5b3714d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion mackerel/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
:copyright: (c) 2015 Shinya Ohyanagi, All rights reserved.
:license: BSD, see LICENSE for more details.
"""
import logging
import requests
import simplejson as json
from mackerel.host import Host
Expand All @@ -22,9 +23,15 @@ class MackerelClientError(Exception):


class Client(object):

#: Mackerel apikey error message.
ERROR_MESSAGE_FOR_API_KEY_ABSENCE = 'API key is absent. Set your API key in a environment variable called MACKEREL_APIKEY.'
#: Log format.
debug_log_format = (
'[%(asctime)s %(levelname)s][%(pathname)s:%(lineno)d]: %(message)s'
)

def __init__(self, **kwargs):
def __init__(self, logger=None, **kwargs):
"""Construct a Mackerel client.
:param mackerel_origin: API endpoint
Expand All @@ -36,6 +43,12 @@ def __init__(self, **kwargs):
raise MackerelClientError(self.ERROR_MESSAGE_FOR_API_KEY_ABSENCE)

self.api_key = api_key
if logger is None:
logging.basicConfig(level=logging.INFO,
format=self.debug_log_format)
self.logger = logging.getLogger('mackerel.client')
else:
self.logger = logger

def get_host(self, host_id):
"""Get registered host.
Expand Down Expand Up @@ -148,6 +161,7 @@ def _request(self, uri, method='GET', headers=None, params=None):
else:
headers.update({'X-Api-Key': self.api_key})

self.logger.debug('{0} {1} {2}'.format(method, uri, params))
if method == 'GET':
res = requests.get(uri, headers=headers, params=params)
elif method == 'POST':
Expand All @@ -156,6 +170,8 @@ def _request(self, uri, method='GET', headers=None, params=None):
message = '{0} is not supported.'.format(method)
raise NotImplementedError(message)

self.logger.debug('Response from {0} is {1}'.format(self.origin,
res.status_code))
if res.status_code != 200:
message = '{0} {1} failed: {2}'.format(method, uri, res.status_code)
raise MackerelClientError(message)
Expand Down

0 comments on commit 5b3714d

Please sign in to comment.