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
Fixes #133
  • Loading branch information
alvarolopez committed Jun 11, 2024
1 parent 8029845 commit 207fb73
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 44 deletions.
4 changes: 2 additions & 2 deletions deepaas/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,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
11 changes: 3 additions & 8 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,15 +273,10 @@ 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)

log.register_options(CONF)
log.set_defaults(default_log_levels=log.get_default_log_levels())
# config.setup(sys.argv)

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

log.setup(CONF, "deepaas-cli")

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

# put all variables in dict, makes life easier...
Expand Down
6 changes: 3 additions & 3 deletions deepaas/cmd/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,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 @@ -90,8 +90,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_path = CONF.base_path

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)
51 changes: 51 additions & 0 deletions deepaas/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- 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: 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: 1 addition & 1 deletion deepaas/tests/test_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def test_run_custom_ip_port(
m_get_app, m_run_app, m_handle_signals, cfg_fixture, monkeypatch
):

monkeypatch.setattr(deepaas.config, "config_and_logging", lambda x: None)
monkeypatch.setattr(deepaas.config, "setup", lambda x: None)
m = mock.MagicMock()
m_get_app.return_value = m
ip = "1.1.1.1"
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 pytest

import deepaas
Expand All @@ -33,7 +32,6 @@
from deepaas.tests import fake_v2_model

CONF = cfg.CONF
logging.register_options(CONF)


def test_loading_ok_with_missing_schema():
Expand Down
2 changes: 0 additions & 2 deletions doc/source/cli/deepaas-cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ Description
by a user file;
``--deepaas_with_multiprocessing`` is to activate multiprocessing
support, default is True.
oslo_log package is used for logging information, which provides
additional options for the script.
If several models are available for loading, one has to provide
which one to load via DEEPAAS_V2_MODEL environment setting.

Expand Down
5 changes: 0 additions & 5 deletions doc/source/install/configuration/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,3 @@ For a sample configuration file, refer to :doc:`sample`.

.. show-options::
deepaas

Logging options
---------------
.. show-options::
oslo.log
1 change: 0 additions & 1 deletion etc/deepaas-config-generator.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
output_file = etc/deepaas.conf.sample
wrap_width = 79
namespace = deepaas
namespace = oslo.log
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 207fb73

Please sign in to comment.