Skip to content

Commit ce55113

Browse files
committed
Start using logging
Instead of using simple print statements, lets start use logging. As a side effect, some modules where loading plugins when the module was imported, printing (thus logging) things before the logger was set up. This change also fixes this, loading things after logging is properly configured.
1 parent bc3cb78 commit ce55113

File tree

6 files changed

+75
-30
lines changed

6 files changed

+75
-30
lines changed

deepaas/api/__init__.py

+25-12
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,37 @@
1616

1717
import flask
1818
import flask_restplus
19+
from oslo_log import log as logging
1920

2021
import deepaas
2122
from deepaas.api import v1
2223
from deepaas import model
2324

24-
app = flask.Flask(__name__)
25-
app.config.SWAGGER_UI_DOC_EXPANSION = 'list'
25+
LOG = logging.getLogger(__name__)
2626

27-
api = flask_restplus.Api(
28-
app,
29-
version=deepaas.__version__,
30-
title='DEEP as a Service API endpoint',
31-
description='DEEP as a Service (DEEPaaS) API endpoint.',
32-
)
33-
34-
api.add_namespace(v1.api, path="/models")
27+
APP = None
3528

3629

3730
def get_app():
38-
print("Loaded models: %s" % model.MODELS.keys())
39-
return app
31+
global APP
32+
33+
if APP:
34+
return APP
35+
36+
model.register_models()
37+
38+
APP = flask.Flask(__name__)
39+
APP.config.SWAGGER_UI_DOC_EXPANSION = 'list'
40+
41+
api = flask_restplus.Api(
42+
APP,
43+
version=deepaas.__version__,
44+
title='DEEP as a Service API endpoint',
45+
description='DEEP as a Service (DEEPaaS) API endpoint.',
46+
)
47+
48+
api.add_namespace(v1.api, path="/models")
49+
50+
LOG.info("Serving loaded models: %s", model.MODELS.keys())
51+
52+
return APP

deepaas/cmd/run.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import sys
1919

2020
from oslo_config import cfg
21+
from oslo_log import log as logging
2122

23+
import deepaas
2224
from deepaas import api
2325
from deepaas import config
2426

@@ -47,11 +49,16 @@
4749

4850
def main():
4951
config.parse_args(sys.argv)
52+
logging.setup(CONF, "deepaas")
53+
log = logging.getLogger(__name__)
54+
55+
log.info("Starting DEEPaaS version %s", deepaas.__version__)
56+
5057
app = api.get_app()
5158
app.run(
52-
debug=True,
5359
host=CONF.listen_ip,
5460
port=CONF.listen_port,
61+
debug=CONF.debug,
5562
)
5663

5764

deepaas/config.py

+11
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,23 @@
1414
# License for the specific language governing permissions and limitations
1515
# under the License.
1616

17+
import logging
18+
import warnings
19+
1720
from oslo_config import cfg
21+
from oslo_log import log
1822

1923
import deepaas
2024

25+
logging.captureWarnings(True)
26+
warnings.simplefilter("default", DeprecationWarning)
27+
2128

2229
def parse_args(argv, default_config_files=None):
30+
log.register_options(cfg.CONF)
31+
32+
log.set_defaults(default_log_levels=log.get_default_log_levels())
33+
2334
cfg.CONF(argv[1:],
2435
project='deepaas',
2536
version=deepaas.__version__,

deepaas/model.py

+27-16
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,37 @@
1414
# License for the specific language governing permissions and limitations
1515
# under the License.
1616

17+
from oslo_log import log
1718
import werkzeug.exceptions as exceptions
1819

1920
from deepaas import loading
2021

22+
LOG = log.getLogger(__name__)
23+
24+
# Model registry
25+
MODELS = {}
26+
MODELS_LOADED = False
27+
28+
29+
def register_models():
30+
global MODELS
31+
global MODELS_LOADED
32+
33+
if MODELS_LOADED:
34+
return
35+
36+
MODELS = {}
37+
try:
38+
for name, model in loading.get_available_models().items():
39+
MODELS[name] = BaseModel(name, model)
40+
except Exception as e:
41+
LOG.warning("Error loading models %s", e)
42+
43+
if not MODELS:
44+
LOG.info("No models found, loading test model")
45+
MODELS["deepaas-test"] = TestModel("deepaas-test")
46+
MODELS_LOADED = True
47+
2148

2249
class BaseModel(object):
2350
def __init__(self, name, model):
@@ -74,19 +101,3 @@ def get_metadata(self):
74101
"version": "0.0.1",
75102
}
76103
return d
77-
78-
79-
def populate_models():
80-
models = {}
81-
try:
82-
for name, model in loading.get_available_models().items():
83-
models[name] = BaseModel(name, model)
84-
except Exception as e:
85-
# TODO(aloga): use logging, not prints
86-
print("Cannot load any models: %s" % e)
87-
if not models:
88-
print("Loading test model")
89-
models["deepaas-test"] = TestModel("deepaas-test")
90-
return models
91-
92-
MODELS = populate_models()

deepaas/tests/test_api.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131

3232
class TestApi(base.TestCase):
33-
@mock.patch('deepaas.api.app')
33+
@mock.patch('deepaas.api.APP')
3434
def test_get_app(self, mock_app):
3535
self.assertEqual(mock_app, api.get_app())
3636

@@ -62,6 +62,8 @@ def setUp(self):
6262
api = flask_restplus.Api(app, doc=False)
6363
api.add_namespace(v1.api)
6464

65+
deepaas.model.register_models()
66+
6567
self.app = app.test_client()
6668
self.assertEqual(app.debug, True)
6769

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# process, which may cause wedges in the gate later.
44
pbr>=1.8
55

6+
oslo.log>=1.8.0 # Apache-2.0
67
oslo.config>=2.3.0 # Apache-2.0
78

89
enum34

0 commit comments

Comments
 (0)