Fixing logging issues

Change-Id: I9b0adf017b9e00e26daa903921ea36aa1c6d80c1
diff --git a/xos/synchronizers/new_base/modelaccessor.py b/xos/synchronizers/new_base/modelaccessor.py
index 2167ca5..27d1f51 100644
--- a/xos/synchronizers/new_base/modelaccessor.py
+++ b/xos/synchronizers/new_base/modelaccessor.py
@@ -233,6 +233,8 @@
 def config_accessor_grpcapi():
     global orig_sigint
 
+    log.info("Connecting to the gRPC API")
+
     grpcapi_endpoint = Config.get("accessor.endpoint")
     grpcapi_username = Config.get("accessor.username")
     grpcapi_password = Config.get("accessor.password")
diff --git a/xos/synchronizers/new_base/xos-synchronizer.py b/xos/synchronizers/new_base/xos-synchronizer.py
index 447954c..d8ee593 100644
--- a/xos/synchronizers/new_base/xos-synchronizer.py
+++ b/xos/synchronizers/new_base/xos-synchronizer.py
@@ -22,15 +22,13 @@
 sys.path.append('/opt/xos')
 
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
+import time
+from synchronizers.new_base.modelaccessor import *
 from xosconfig import Config
 from multistructlog import create_logger
-
-log = create_logger(Config().get('logging'))
-import time
-
-from synchronizers.new_base.modelaccessor import *
 from synchronizers.new_base.backend import Backend
 
+log = create_logger(Config().get('logging'))
 def main():
 
     models_active = False
diff --git a/xos/xos_client/xosapi/convenience/serviceinstance.py b/xos/xos_client/xosapi/convenience/serviceinstance.py
index 5ed3883..b6523d8 100644
--- a/xos/xos_client/xosapi/convenience/serviceinstance.py
+++ b/xos/xos_client/xosapi/convenience/serviceinstance.py
@@ -13,7 +13,10 @@
 
 from xosapi.orm import ORMWrapper, register_convenience_wrapper
 
-import logging as log
+from xosconfig import Config
+from multistructlog import create_logger
+
+log = create_logger(Config().get('logging'))
 
 class ORMWrapperServiceInstance(ORMWrapper):
 
diff --git a/xos/xos_client/xosapi/orm.py b/xos/xos_client/xosapi/orm.py
index 4f46e0c..32f8128 100644
--- a/xos/xos_client/xosapi/orm.py
+++ b/xos/xos_client/xosapi/orm.py
@@ -39,7 +39,10 @@
 import sys
 import time
 import imp
-import logging as log
+from xosconfig import Config
+from multistructlog import create_logger
+
+log = create_logger(Config().get('logging'))
 
 convenience_wrappers = {}
 
diff --git a/xos/xos_client/xosapi/xos_grpc_client.py b/xos/xos_client/xosapi/xos_grpc_client.py
index c5e259d..47849da 100644
--- a/xos/xos_client/xosapi/xos_grpc_client.py
+++ b/xos/xos_client/xosapi/xos_grpc_client.py
@@ -35,6 +35,11 @@
 from twisted.internet import reactor
 from google.protobuf.empty_pb2 import Empty
 
+from xosconfig import Config
+from multistructlog import create_logger
+
+log = create_logger(Config().get('logging'))
+
 SERVER_CA="/usr/local/share/ca-certificates/local_certs.crt"
 
 class UsernamePasswordCallCredentials(grpc.AuthMetadataPlugin):
@@ -72,14 +77,14 @@
             response = self.dynamicload.GetConvenienceMethods(Empty())
 
             if response:
-                print "Loading convenience methods: %s" % [m.filename for m in response.convenience_methods]
+                log.info("Loading convenience methods",  methods=[m.filename for m in response.convenience_methods])
 
                 for cm in response.convenience_methods:
-                    print "Saving %s" % cm.filename
+                    log.debug("Saving convenience method", method=cm.filename)
                     save_path = os.path.join(convenience_methods_dir, cm.filename)
                     file(save_path, "w").write(cm.contents)
             else:
-                print 'Cannot load convenience methods, restarting the synchronzier'
+                log.exception("Cannot load convenience methods, restarting the synchronzier")
                 os.execv(sys.executable, ['python'] + sys.argv)
 
         except grpc._channel._Rendezvous, e:
@@ -89,7 +94,6 @@
                 os.execv(sys.executable, ['python'] + sys.argv)
 
     def reconnected(self):
-
         for api in ['modeldefs', 'utility', 'xos', 'dynamicload']:
             pb2_file_name = os.path.join(self.work_dir, api + "_pb2.py")
             pb2_grpc_file_name = os.path.join(self.work_dir, api + "_pb2_grpc.py")
@@ -215,27 +219,8 @@
 
     return args
 
-def setup_logging(args):
-    import os
-
-    if os.path.isfile(args.config):
-        from xosconfig import Config
-        from multistructlog import create_logger
-        Config.init(args.config, 'synchronizer-config-schema.yaml')
-        log = create_logger(Config().get('logging'))
-    else:
-        import logging
-        import structlog
-
-        verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
-        logging.basicConfig()
-        logger = logging.getLogger()
-        logger.setLevel(logging.DEBUG - 10 * verbosity_adjust)
-
-        def logger_factory():
-            return logger
-
-        structlog.configure(logger_factory=logger_factory)
+def setup_logging():
+    log = create_logger(Config().get('logging'))
 
 
 def coreclient_reconnect(client, reconnect_callback, *args, **kwargs):
@@ -268,7 +253,7 @@
 
     args = parse_args()
 
-    setup_logging(args)
+    setup_logging()
 
     if args.username:
         start_api(reconnect_callback, endpoint=args.grpc_secure_endpoint, username=args.username, password=args.password)
@@ -292,7 +277,7 @@
     reactor.callLater(0, start_secure_test)
 
 def start_insecure_test():
-    client = InsecureClient(endpoint="xos-core.cord.lab:50055")
+    client = InsecureClient(endpoint="xos-core:50055")
     client.set_reconnect_callback(functools.partial(insecure_callback, client))
     client.start()
 
@@ -303,7 +288,7 @@
     reactor.stop()
 
 def start_secure_test():
-    client = SecureClient(endpoint="xos-core.cord.lab:50051", username="xosadmin@opencord.org", password="BQSPdpRsR0MrrZ9u7SPe")
+    client = SecureClient(endpoint="xos-core:50051", username="admin@opencord.org", password="letmein")
     client.set_reconnect_callback(functools.partial(secure_callback, client))
     client.start()
 
diff --git a/xos/xos_client/xossh b/xos/xos_client/xossh
index 61bc651..955eac0 100644
--- a/xos/xos_client/xossh
+++ b/xos/xos_client/xossh
@@ -12,7 +12,6 @@
 
 from twisted.internet import reactor
 from xosapi.version import __version__
-from xosapi.xos_grpc_client import InsecureClient, SecureClient, setup_logging, Empty
 
 current_client = None
 
@@ -90,6 +89,7 @@
     return args
 
 def login(username=None, password=None):
+    from xosapi.xos_grpc_client import InsecureClient, SecureClient
     if current_client:
         current_client.stop()
         current_client.session_change = True
@@ -256,7 +256,14 @@
     global args
     args = parse_args()
 
-    setup_logging(args)
+    from xosconfig import Config
+
+    config_file = args.config
+    Config.init(config_file, 'synchronizer-config-schema.yaml')
+
+    from xosapi.xos_grpc_client import setup_logging
+
+    setup_logging()
 
     if args.version:
         print __version__