Skip to content

Commit

Permalink
Removed custom logging module
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAyd committed Sep 22, 2018
1 parent c57fdbc commit 51475b7
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 117 deletions.
34 changes: 14 additions & 20 deletions tabpy-client/tabpy_client/rest.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import logging as _logging
import logging
from future.utils import with_metaclass
import requests
_logger = _logging.getLogger(__name__)

from collections import MutableMapping as _MutableMapping

import json as json

from tabpy_server.common.tabpy_logging import (
PYLogging,
log_error,
log_info,
log_debug,
log_warning,
)
PYLogging.initialize(_logger)

logger = logging.getLogger('tabpy.tabpy_client.rest')


class ResponseError(Exception):
"""Raised when we get an unexpected response."""
Expand Down Expand Up @@ -65,9 +59,9 @@ def __init__(self, session=None):
self.session = session

def raise_error(self, response):
_logger.error("Error with server response. code=%s; text=%s",
response.status_code,
response.text)
logger.error("Error with server response. code=%s; text=%s",
response.status_code,
response.text)

raise ResponseError(response)

Expand All @@ -89,12 +83,12 @@ def GET(self, url, data, timeout=None):
object that is parsed from the response JSON."""
self._remove_nones(data)

_logger.info("GET %s with %r", url, data)
logger.info("GET %s with %r", url, data)

response = self.session.get(url, params=data, timeout=timeout)
if response.status_code != 200:
self.raise_error(response)
_logger.info("response=%r", response.text)
logger.info("response=%r", response.text)

if response.text == '':
return dict()
Expand All @@ -106,7 +100,7 @@ def POST(self, url, data, timeout=None):
object that is parsed from the response JSON."""
data = self._encode_request(data)

_logger.info("POST %s with %r", url, data)
logger.info("POST %s with %r", url, data)
response = self.session.post(url,
data=data,
headers={
Expand All @@ -123,7 +117,7 @@ def PUT(self, url, data, timeout=None):
object that is parsed from the response JSON."""
data = self._encode_request(data)

_logger.info("PUT %s with %r", url, data)
logger.info("PUT %s with %r", url, data)

response = self.session.put(url,
data=data,
Expand All @@ -142,7 +136,7 @@ def DELETE(self, url, data, timeout=None):
if data is not None:
data = json.dumps(data)

_logger.info("DELETE %s with %r", url, data)
logger.info("DELETE %s with %r", url, data)

response = self.session.delete(url, data=data, timeout=timeout)

Expand All @@ -167,7 +161,7 @@ def __init__(self, endpoint, network_wrapper=None):
self.network_wrapper = network_wrapper

if not endpoint.endswith('/'):
_logger.warning("endpoint %s does not end with '/': appending.", endpoint)
logger.warning("endpoint %s does not end with '/': appending.", endpoint)
endpoint = endpoint + '/'

self.endpoint = endpoint
Expand Down Expand Up @@ -271,7 +265,7 @@ def __init__(self, **kwargs):
are ignored.
"""
_logger.info("Initializing %s from %r",
logger.info("Initializing %s from %r",
self.__class__.__name__,
kwargs)
for attr in self.__rest__:
Expand Down
10 changes: 4 additions & 6 deletions tabpy-server/tabpy_server/common/endpoint_file_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@
/tmp/query_objects
'''
import logging
import os
import sys
import shutil
import time
from re import compile as _compile

from tabpy_server.common.tabpy_logging import (
PYLogging, log_error, log_info, log_debug, log_warning)

import logging
logger = logging.getLogger('tabpy.tabpy_server.common.endpoint_file_mgr')

logger = logging.getLogger(__name__)
PYLogging.initialize(logger)

_name_checker = _compile('^[a-zA-Z0-9-_\ ]+$')


def _check_endpoint_name(name):
"""Checks that the endpoint name is valid by comparing it with an RE and
checking that it is not reserved."""
Expand Down Expand Up @@ -86,7 +84,7 @@ def cleanup_endpoint_files(name, query_path, retain_versions = None):
else:
retain_folders = [os.path.join(local_dir, str(version)) \
for version in retain_versions]
log_info("Retain folder: %s" % retain_folders)
logger.info("Retain folder: %s" % retain_folders)

for file_or_dir in os.listdir(local_dir):
candidate_dir = os.path.join(local_dir, file_or_dir)
Expand Down
34 changes: 16 additions & 18 deletions tabpy-server/tabpy_server/management/state.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
try:
from ConfigParser import ConfigParser
except ImportError:
Expand All @@ -14,12 +15,9 @@
from tabpy_server.management.util import (
write_state_config, load_state_from_config_file)

from tabpy_server.common.tabpy_logging import (
PYLogging, log_error, log_info, log_debug, log_warning)

import logging
logger = logging.getLogger(__name__)
PYLogging.initialize(logger)
logger = logging.getLogger('tabpy.tabpy_server.management.state')


if sys.version_info.major == 3:
unicode = str
Expand Down Expand Up @@ -77,7 +75,7 @@ def save_state_to_str(config):
config.write(string_f)
value = string_f.getvalue()
except:
log_error("Cannot convert config to string")
logger.error("Cannot convert config to string")
finally:
string_f.close()
return value
Expand Down Expand Up @@ -161,7 +159,7 @@ def get_endpoints(self, name=None):
try:
endpoint_names = self._get_config_value(_DEPLOYMENT_SECTION_NAME, name)
except Exception as e:
log_error("error in get_endpoints: %s" % str(e))
logger.error("error in get_endpoints: %s" % str(e))
return {}

if name:
Expand Down Expand Up @@ -250,7 +248,7 @@ def add_endpoint(self, name, description=None,
endpoints[name] = endpoint_info
self._add_update_endpoints_config(endpoints)
except Exception as e:
log_error("Error in add_endpoint: %s" % e)
logger.error("Error in add_endpoint: %s" % e)
raise

def _add_update_endpoints_config(self, endpoints):
Expand All @@ -271,7 +269,7 @@ def _add_update_endpoints_config(self, endpoints):
self._set_config_value(_DEPLOYMENT_SECTION_NAME,
endpoint_name, simplejson.dumps(info))
except Exception as e:
log_error("Unable to write endpoints config: %s" % e)
logger.error("Unable to write endpoints config: %s" % e)
raise

@state_lock
Expand Down Expand Up @@ -356,7 +354,7 @@ def update_endpoint(self, name, description=None,
endpoints[name] = endpoint_info
self._add_update_endpoints_config(endpoints)
except Exception as e:
log_error("Error in update_endpoint: %s" % e)
logger.error("Error in update_endpoint: %s" % e)
raise

@state_lock
Expand Down Expand Up @@ -409,7 +407,7 @@ def delete_endpoint(self, name):

return endpoint_to_delete
except Exception as e:
log_error("Unable to delete endpoint %s" % e)
logger.error("Unable to delete endpoint %s" % e)
raise ValueError("Unable to delete endpoint: %s" % e)

@property
Expand All @@ -421,7 +419,7 @@ def name(self):
try:
name = self._get_config_value(_SERVICE_INFO_SECTION_NAME, 'Name')
except Exception as e:
log_error("Unable to get name: %s" % e)
logger.error("Unable to get name: %s" % e)
return name

@property
Expand All @@ -433,7 +431,7 @@ def creation_time(self):
try:
creation_time = self._get_config_value(_SERVICE_INFO_SECTION_NAME, 'Creation Time')
except Exception as e:
log_error("Unable to get name: %s" % e)
logger.error("Unable to get name: %s" % e)
return creation_time

@state_lock
Expand All @@ -451,7 +449,7 @@ def set_name(self, name):
try:
self._set_config_value(_SERVICE_INFO_SECTION_NAME, 'Name', name)
except Exception as e:
log_error("Unable to set name: %s" % e)
logger.error("Unable to set name: %s" % e)

def get_description(self):
'''
Expand All @@ -461,7 +459,7 @@ def get_description(self):
try:
description = self._get_config_value(_SERVICE_INFO_SECTION_NAME, 'Description')
except Exception as e:
log_error("Unable to get description: %s" % e)
logger.error("Unable to get description: %s" % e)
return description

@state_lock
Expand All @@ -479,7 +477,7 @@ def set_description(self, description):
try:
self._set_config_value(_SERVICE_INFO_SECTION_NAME, 'Description', description)
except Exception as e:
log_error("Unable to set description: %s" % e)
logger.error("Unable to set description: %s" % e)

def get_revision_number(self):
'''
Expand All @@ -489,7 +487,7 @@ def get_revision_number(self):
try:
rev = int(self._get_config_value(_META_SECTION_NAME, 'Revision Number'))
except Exception as e:
log_error("Unable to get revision number: %s" % e)
logger.error("Unable to get revision number: %s" % e)
return rev

def get_cors_origin(self):
Expand All @@ -513,7 +511,7 @@ def _set_revision_number(self, revision_number):
self._set_config_value(_META_SECTION_NAME,
'Revision Number', revision_number)
except Exception as e:
log_error("Unable to set revision number: %s" % e)
logger.error("Unable to set revision number: %s" % e)

def _remove_config_option(self, section_name, option_name,
_update_revision=True):
Expand Down
13 changes: 6 additions & 7 deletions tabpy-server/tabpy_server/management/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import concurrent.futures
import logging
import os
import tempfile
import shutil
Expand All @@ -14,23 +15,21 @@
from datetime import datetime, timedelta, tzinfo
from time import mktime

from tabpy_server.common.tabpy_logging import (
PYLogging, log_error, log_info, log_debug, log_warning)
import logging
logger = logging.getLogger(__name__)
PYLogging.initialize(logger)

logger = logging.getLogger('tabpy.tabpy_server.management.util')


def load_state_from_config_file(ps_state):
'''
Fill initial TabPy state from state file path
'''
try:
log_info("Loading state from state file")
logger.info("Loading state from state file")
state_file_path = os.environ['TABPY_STATE_PATH']
config = util._get_state_from_file(state_file_path)
ps_state.set_config(config)
except Exception as e:
log_error("Unable to get state file:", str(e.message))
logger.error("Unable to get state file:", str(e.message))

def write_state_config(state):
state_path = os.environ['TABPY_STATE_PATH']
Expand Down
28 changes: 11 additions & 17 deletions tabpy-server/tabpy_server/psws/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,20 @@

from tabpy_server.management import util

from tabpy_server.common.tabpy_logging import (
PYLogging, log_error, log_info, log_warning)


logger = logging.getLogger(__name__)


PYLogging.initialize(logger)
logger = logging.getLogger('tabpy.tabpy_server.psws.callbacks')


def wait_for_endpoint_loaded(py_handler, object_uri):
'''
This method waits for the object to be loaded.
'''
log_info('Waiting for object to be loaded...')
logger.info('Waiting for object to be loaded...')
while True:
msg = ListObjects()
list_object_msg = py_handler.manage_request(msg)
if not isinstance(list_object_msg, ObjectList):
log_error("Error loading endpoint %s: %s" % (
logger.error("Error loading endpoint %s: %s" % (
object_uri, list_object_msg))
return

Expand All @@ -40,7 +34,7 @@ def wait_for_endpoint_loaded(py_handler, object_uri):
else list_object_msg.objects.iteritems()):
if uri == object_uri:
if info['status'] != 'LoadInProgress':
log_info("Object load status: %s" % info['status'])
logger.info("Object load status: %s" % info['status'])
return

sleep(0.1)
Expand All @@ -59,8 +53,8 @@ def init_ps_server(settings):
settings['state_file_path'],
object_name, object_version)
except Exception as e:
log_error('Exception encounted when downloading object: %s'
', error: %s' % (object_name, e))
logger.error('Exception encounted when downloading object: %s'
', error: %s' % (object_name, e))


@gen.coroutine
Expand All @@ -83,7 +77,7 @@ def init_model_evaluator(settings):
settings['state_file_path'],
object_name, object_version)

log_info('Load endpoint: %s, version: %s, type: %s' %
logger.info('Load endpoint: %s, version: %s, type: %s' %
(object_name, object_version, object_type))
if object_type == 'alias':
msg = LoadObject(object_name, obj_info['target'],
Expand Down Expand Up @@ -143,14 +137,14 @@ def on_state_change(settings):
try:
py_handler = settings['py_handler']

log_info("Loading state from state file")
logger.info("Loading state from state file")
config = util._get_state_from_file(settings['state_file_path'])
new_ps_state = TabPyState(config=config)

(has_changes, changes) = _get_latest_service_state(settings,
new_ps_state)
if not has_changes:
log_info("Nothing changed, return.")
logger.info("Nothing changed, return.")
return

new_endpoints = new_ps_state.get_endpoints()
Expand All @@ -159,7 +153,7 @@ def on_state_change(settings):
object_name]

if not object_path and not object_version: # removal
log_info("Removing object", uri=object_name)
logger.info("Removing object", uri=object_name)

py_handler.manage_request(DeleteObjects([object_name]))

Expand Down Expand Up @@ -187,4 +181,4 @@ def on_state_change(settings):

except Exception as e:
err_msg = format_exception(e, 'on_state_change')
log_warning("Error submitting update model request", error=err_msg)
logger.warning("Error submitting update model request", error=err_msg)
Loading

0 comments on commit 51475b7

Please sign in to comment.