Skip to content

Commit

Permalink
feat! remove oslo.log and use standard python logging
Browse files Browse the repository at this point in the history
This commit addresses, partially #133
  • Loading branch information
alvarolopez committed Jun 10, 2024
1 parent b753631 commit 6e57829
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 35 deletions.
4 changes: 2 additions & 2 deletions deepaas/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
from aiohttp import web
import aiohttp_apispec
from oslo_config import cfg
from oslo_log import log as logging

import deepaas
from deepaas.api import v2
from deepaas.api import versions
from deepaas import log
from deepaas import model

LOG = logging.getLogger(__name__)
LOG = log.getLogger(__name__)

APP = None

Expand Down
2 changes: 1 addition & 1 deletion deepaas/api/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
from aiohttp import web
import aiohttp_apispec
from oslo_config import cfg
from oslo_log import log

from deepaas.api.v2 import debug as v2_debug
from deepaas.api.v2 import models as v2_model
from deepaas.api.v2 import predict as v2_predict
from deepaas.api.v2 import responses
from deepaas.api.v2 import train as v2_train
from deepaas import log

CONF = cfg.CONF
LOG = log.getLogger("deepaas.api.v2")
Expand Down
3 changes: 2 additions & 1 deletion deepaas/api/v2/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
from aiohttp import web
import aiohttp_apispec
from oslo_config import cfg
from oslo_log import log

from deepaas import log

CONF = cfg.CONF

Expand Down
2 changes: 1 addition & 1 deletion deepaas/api/v2/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

from aiohttp import web
import aiohttp_apispec
from oslo_log import log
from webargs import aiohttpparser
import webargs.core

from deepaas.api.v2 import responses
from deepaas.api.v2 import utils
from deepaas import log
from deepaas import model

LOG = log.getLogger("deepaas.api.v2.train")
Expand Down
14 changes: 8 additions & 6 deletions deepaas/cmd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
from datetime import datetime
from marshmallow import fields
from oslo_config import cfg
from oslo_log import log

from deepaas import config
from deepaas import log
from deepaas.model import loading
from deepaas.model.v2 import wrapper as v2_wrapper

Expand Down Expand Up @@ -247,7 +247,7 @@ def _add_methods(subparsers):
CONF = cfg.CONF
CONF.register_cli_opts(cli_opts)

LOG = log.getLogger(__name__)
LOG = log.get_logger(__name__)


# store DEEPAAS_METHOD output in a file
Expand All @@ -273,14 +273,16 @@ def main():
"""Executes model's methods with corresponding parameters"""

# we may add deepaas config, but then too many options...
# config.config_and_logging(sys.argv)
# config.setup(sys.argv)

log.register_options(CONF)
log.set_defaults(default_log_levels=log.get_default_log_levels())
# FIXME(aloga): review this
# log.register_options(CONF)
# log.set_defaults(default_log_levels=log.get_default_log_levels())

CONF(sys.argv[1:], project="deepaas", version=deepaas.__version__)

log.setup(CONF, "deepaas-cli")
# FIXME(aloga): review this
# log.setup(CONF, "deepaas-cli")

LOG.info("[INFO, Method] {} was called.".format(CONF.methods.name))

Expand Down
6 changes: 3 additions & 3 deletions deepaas/cmd/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

from aiohttp import web
from oslo_config import cfg
from oslo_log import log as oslo_log

import deepaas
from deepaas import api
import deepaas.log
from deepaas.cmd import _shutdown
from deepaas import config

Expand Down Expand Up @@ -89,8 +89,8 @@
def main():
_shutdown.handle_signals()

config.config_and_logging(sys.argv)
log = oslo_log.getLogger("deepaas")
config.setup(sys.argv)
log = deepaas.log.LOG

base = "http://{}:{}".format(CONF.listen_ip, CONF.listen_port)
spec = "{}/swagger.json".format(base)
Expand Down
41 changes: 30 additions & 11 deletions deepaas/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
# License for the specific language governing permissions and limitations
# under the License.

import logging
import os
import warnings

from oslo_config import cfg
from oslo_log import log

import deepaas
from deepaas import log

logging.captureWarnings(True)
warnings.simplefilter("default", DeprecationWarning)

opts = [
Expand Down Expand Up @@ -94,6 +92,30 @@
help="""
Specify the model to be used. If not specified, DEEPaaS will fail if there are
more than only one models available.
""",
),
cfg.BoolOpt(
"debug",
default=False,
help="""
Enable debug mode in logging. This will provide more information about what
is happening in the API. Default is to not provide this information.
""",
),
cfg.StrOpt(
"log-level",
default="INFO",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
help="""
Specify the log level for the API. Default is INFO. Possible values are DEBUG,
INFO, WARNING, ERROR, CRITICAL.
""",
),
cfg.StrOpt(
"log-file",
default="",
help="""
Specify the log file to use. If not specified, logs will be sent to stdout.
""",
),
]
Expand All @@ -102,11 +124,6 @@
CONF.register_cli_opts(opts)


def prepare_logging():
log.register_options(cfg.CONF)
log.set_defaults(default_log_levels=log.get_default_log_levels())


def parse_args(argv, default_config_files=None):
cfg.CONF(
argv[1:],
Expand All @@ -116,7 +133,9 @@ def parse_args(argv, default_config_files=None):
)


def config_and_logging(argv, default_config_files=None):
prepare_logging()
def setup(argv, default_config_files=None):
parse_args(argv, default_config_files=default_config_files)
log.setup(cfg.CONF, "deepaas")

log_level = (CONF.debug and "DEBUG") or CONF.log_level

log.setup(log_level, CONF.log_file)
52 changes: 52 additions & 0 deletions deepaas/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-

# Copyright 2018 Spanish National Research Council (CSIC)
#
# 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 logging

logging.captureWarnings(True)

LOG = logging.getLogger("deepaas")


log_format = (
"%(asctime)s.%(msecs)03d %(process)d %(levelname)s %(name)s [-] "
"%(message)s"
)
log_format_debug_suffix = "%(funcName)s %(pathname)s:%(lineno)d"


def getLogger(name): # noqa
return LOG.getChild(name)


def setup(log_level, log_file=None):
LOG.setLevel(log_level)

if log_level == "DEBUG":
format_ = log_format + " [-] " + log_format_debug_suffix
else:
format_ = log_format

logging.basicConfig(level=log_level, format=format_)

if log_file:
fh = logging.FileHandler(log_file)
fh.setLevel(log_level)
LOG.addHandler(fh)
else:
ch = logging.StreamHandler()
ch.setLevel(log_level)
LOG.addHandler(ch)
3 changes: 1 addition & 2 deletions deepaas/model/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
# License for the specific language governing permissions and limitations
# under the License.

from oslo_log import log

from deepaas import config
from deepaas import exceptions
from deepaas import log
from deepaas.model import loading
from deepaas.model.v2 import wrapper

Expand Down
4 changes: 2 additions & 2 deletions deepaas/model/v2/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

from aiohttp import web
import marshmallow

from oslo_config import cfg
from oslo_log import log

from deepaas import log

LOG = log.getLogger(__name__)

Expand Down
2 changes: 0 additions & 2 deletions deepaas/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
import fixtures
from oslo_config import cfg
from oslo_config import fixture as config_fixture
from oslo_log import log as logging
import testtools

CONF = cfg.CONF
logging.register_options(CONF)


_TRUE_VALUES = ("True", "true", "1", "yes")
Expand Down
2 changes: 1 addition & 1 deletion deepaas/tests/fake_v2_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import base64
import time

from oslo_log import log
from webargs import fields
from webargs import validate

from deepaas import log
from deepaas.model.v2 import base

LOG = log.getLogger(__name__)
Expand Down
2 changes: 0 additions & 2 deletions deepaas/tests/test_v2_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

from aiohttp import web
from oslo_config import cfg
from oslo_log import log as logging

import deepaas
from deepaas.api import v2
Expand All @@ -31,7 +30,6 @@
from deepaas.tests import fake_responses

CONF = cfg.CONF
logging.register_options(CONF)


def test_loading_ok_with_missing_schema():
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ deepaas = "deepaas.opts:list_opts"

[tool.poetry.dependencies]
python = "^3.10"
"oslo.log" = "^6.0.0"
"oslo.config" = "^9.4.0"
stevedore = "^5.2.0"
aiohttp = "^3.9.5"
Expand Down

0 comments on commit 6e57829

Please sign in to comment.