Fixing logging
Change-Id: I3bd5168110985fd06faa6a4bc2fe3d713a208337
diff --git a/src/grpc_client/main.py b/src/grpc_client/main.py
index fc64ef3..bf866bf 100644
--- a/src/grpc_client/main.py
+++ b/src/grpc_client/main.py
@@ -21,6 +21,10 @@
from xosconfig import Config
from twisted.internet import reactor
+from xosconfig import Config
+from multistructlog import create_logger
+log = create_logger(Config().get('logging'))
+
class GRPC_Client:
def __init__(self):
self.client = None
@@ -32,7 +36,7 @@
self.grpc_insecure_endpoint = insecure + ":50055"
def setup_resources(self, client, key, deferred, recipe):
- print "[XOS-TOSCA] Loading resources"
+ log.info("[XOS-TOSCA] Loading resources for authenticated user")
if key not in RESOURCES:
RESOURCES[key] = {}
for k in client.xos_orm.all_model_names:
@@ -40,7 +44,7 @@
reactor.callLater(0, deferred.callback, recipe)
def start(self):
- print "[XOS-TOSCA] Connecting to xos-core"
+ log.info("[XOS-TOSCA] Connecting to xos-core")
deferred = defer.Deferred()
diff --git a/src/grpc_client/models_accessor.py b/src/grpc_client/models_accessor.py
index 5d54644..2c96c48 100644
--- a/src/grpc_client/models_accessor.py
+++ b/src/grpc_client/models_accessor.py
@@ -13,6 +13,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+from xosconfig import Config
+from multistructlog import create_logger
+log = create_logger(Config().get('logging'))
from resources import RESOURCES
@@ -69,15 +72,15 @@
models = cls.objects.filter(**filter)
if len(models) == 1:
- print "[XOS-Tosca] Model of class %s and properties %s already exist, retrieving instance..." % (class_name, str(filter))
model = models[0]
+ log.info("[XOS-Tosca] Model of class %s and properties %s already exist, retrieving instance..." % (class_name, str(filter)), model=model)
elif len(models) == 0:
if 'must-exist' in data and data['must-exist']:
raise Exception("[XOS-TOSCA] Model of class %s and properties %s has property 'must-exist' but cannot be found" % (class_name, str(filter)))
model = cls.objects.new()
- print "[XOS-Tosca] Model (%s) is new, creating new instance..." % str(filter)
+ log.info("[XOS-Tosca] Model (%s) is new, creating new instance..." % str(filter))
else:
raise Exception("[XOS-Tosca] Model of class %s and properties %s has multiple instances, I can't handle it" % (class_name, str(filter)))
diff --git a/src/main.py b/src/main.py
index 0d56392..6b0359d 100644
--- a/src/main.py
+++ b/src/main.py
@@ -15,17 +15,21 @@
import os
-from grpc_client.main import GRPC_Client
-from tosca.generator import TOSCA_Generator
-from web_server.main import TOSCA_WebServer
-from twisted.internet import defer
from xosconfig import Config
+from multistructlog import create_logger
current_dir = os.path.dirname(os.path.realpath(__file__))
config_file = os.path.join(current_dir, 'xos-tosca.config.yaml')
config_schema = os.path.join(current_dir, 'xos-tosca-config-schema.yaml')
Config.init(config_file, config_schema)
+log = create_logger(Config().get('logging'))
+
+from grpc_client.main import GRPC_Client
+from tosca.generator import TOSCA_Generator
+from web_server.main import TOSCA_WebServer
+from twisted.internet import defer
+
class Main:
@@ -41,7 +45,7 @@
return deferred
def start(self):
- print "[XOS-TOSCA] Starting"
+ log.info("[XOS-TOSCA] Starting")
# Remove generated TOSCA and KEYS that may have been downloaded by a previous session. This is done here, rather
# than in the generator, to cover the case where the TOSCA engine is restarted and a web request is received
diff --git a/src/tosca/generator.py b/src/tosca/generator.py
index 13472f9..3f54a77 100644
--- a/src/tosca/generator.py
+++ b/src/tosca/generator.py
@@ -13,6 +13,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+from xosconfig import Config
+from multistructlog import create_logger
+log = create_logger(Config().get('logging'))
import os
from default import TOSCA_DEFS_DIR, TOSCA_KEYS_DIR
@@ -38,7 +41,7 @@
os.remove(keys_fn)
def generate(self, client):
- print "[XOS-TOSCA] Generating TOSCA"
+ log.info("[XOS-TOSCA] Generating TOSCA")
try:
xproto = client.utility.GetXproto(Empty())
@@ -48,10 +51,9 @@
args.target = os.path.join(current_dir, 'xtarget/tosca.xtarget')
args.write_to_file = 'target'
XOSProcessor.process(args)
- print "[XOS-TOSCA] Recipes generated in %s" % args.output
+ log.info("[XOS-TOSCA] Recipes generated in %s" % args.output)
except Exception as e:
- print "[XOS-TOSCA] Failed to generate TOSCA"
- print e
+ log.exception("[XOS-TOSCA] Failed to generate TOSCA")
try:
xproto = client.utility.GetXproto(Empty())
@@ -62,8 +64,7 @@
args.write_to_file = 'single'
args.dest_file = 'KEYS.py'
XOSProcessor.process(args)
- print "[XOS-TOSCA] TOSCA Keys generated in %s" % args.output
+ log.info("[XOS-TOSCA] TOSCA Keys generated in %s" % args.output)
except Exception as e:
- print "[XOS-TOSCA] Failed to generate TOSCA Keys"
- print e
+ log.exception("[XOS-TOSCA] Failed to generate TOSCA Keys")
diff --git a/src/tosca/parser.py b/src/tosca/parser.py
index b691166..fe8382b 100644
--- a/src/tosca/parser.py
+++ b/src/tosca/parser.py
@@ -14,6 +14,10 @@
# limitations under the License.
+from xosconfig import Config
+from multistructlog import create_logger
+log = create_logger(Config().get('logging'))
+
from toscaparser.tosca_template import ToscaTemplate, ValidationError
from default import TOSCA_RECIPES_DIR
from grpc_client.resources import RESOURCES
@@ -227,17 +231,16 @@
reference_only = True
if self.delete and not model.is_new and not reference_only:
- print "[XOS-Tosca] Deleting model %s[%s]" % (class_name, model.id)
+ log.info("[XOS-Tosca] Deleting model %s[%s]" % (class_name, model.id))
model.delete()
elif not self.delete:
- print "[XOS-Tosca] Saving model %s[%s]" % (class_name, model.id)
+ log.info("[XOS-Tosca] Saving model %s[%s]" % (class_name, model.id))
model.save()
self.saved_model_by_name[recipe.name] = model
except Exception, e:
- print "[XOS-TOSCA] Failed to save model: %s [%s]" % (class_name, recipe.name)
- traceback.print_exc()
+ log.exception("[XOS-TOSCA] Failed to save model: %s [%s]" % (class_name, recipe.name))
raise e
except ValidationError as e:
@@ -257,7 +260,7 @@
exception_msg = e._state.details
raise Exception(exception_msg)
except Exception, e:
- print e
+ log.exception(e)
raise Exception(e)
diff --git a/src/web_server/main.py b/src/web_server/main.py
index f5d8043..3e2cf8f 100644
--- a/src/web_server/main.py
+++ b/src/web_server/main.py
@@ -13,6 +13,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+from xosconfig import Config
+from multistructlog import create_logger
+log = create_logger(Config().get('logging'))
from grpc_client.main import GRPC_Client
from klein import Klein
@@ -48,10 +51,10 @@
request.setResponseCode(500)
try:
f = failure.getErrorMessage()
- print "[XOS-TOSCA] Error while loading TOSCA: \n\n", f
+ log.info("[XOS-TOSCA] Error while loading TOSCA: \n\n", failure=f)
return f
- except:
- print failure
+ except Exception:
+ log.info("[XOS-TOSCA] Fatal Error: \n\n", failure=failure)
return "Internal server error, please report this along with the failed recipe."
@app.route('/', methods=['GET'])
diff --git a/test/helpers.py b/test/helpers.py
new file mode 100644
index 0000000..2a07499
--- /dev/null
+++ b/test/helpers.py
@@ -0,0 +1,21 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import os
+from xosconfig import Config
+current_dir = os.path.dirname(os.path.realpath(__file__))
+config_file = os.path.join(current_dir, 'test_config.yaml')
+config_schema = os.path.join(current_dir, '../src/xos-tosca-config-schema.yaml')
+Config.clear()
+Config.init(config_file, config_schema)
\ No newline at end of file
diff --git a/test/test_config.yaml b/test/test_config.yaml
new file mode 100644
index 0000000..698538c
--- /dev/null
+++ b/test/test_config.yaml
@@ -0,0 +1,13 @@
+name: xos-tosca
+gprc_endpoint: "xos-core"
+local_cert: /usr/local/share/ca-certificates/local_certs.crt
+logging:
+ version: 1
+ handlers:
+ console:
+ class: logging.StreamHandler
+ loggers:
+ 'multistructlog':
+ handlers:
+ - console
+ level: ERROR
\ No newline at end of file
diff --git a/test/test_grpc_models_accessor.py b/test/test_grpc_models_accessor.py
index 60e86e0..a385f86 100644
--- a/test/test_grpc_models_accessor.py
+++ b/test/test_grpc_models_accessor.py
@@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-
+from helpers import *
import unittest
from mock import patch, MagicMock
from grpc_client.models_accessor import GRPCModelsAccessor
diff --git a/test/test_tosca_generator.py b/test/test_tosca_generator.py
index 1d560d2..5e74214 100644
--- a/test/test_tosca_generator.py
+++ b/test/test_tosca_generator.py
@@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-
+from helpers import *
import unittest
import os
from xosgenx.generator import XOSProcessor
diff --git a/test/test_tosca_parser.py b/test/test_tosca_parser.py
index c8ad4bf..b16817a 100644
--- a/test/test_tosca_parser.py
+++ b/test/test_tosca_parser.py
@@ -14,6 +14,7 @@
# limitations under the License.
+from helpers import *
import unittest
import os
from tosca.parser import TOSCA_Parser
diff --git a/test/test_tosca_parser_e2e.py b/test/test_tosca_parser_e2e.py
index 2d2649f..124e160 100644
--- a/test/test_tosca_parser_e2e.py
+++ b/test/test_tosca_parser_e2e.py
@@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-
+from helpers import *
import unittest
from mock import patch, MagicMock
from tosca.parser import TOSCA_Parser