Skip to content

Commit

Permalink
refactor log_setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodong-Yang committed Jun 6, 2024
1 parent 3fc05d4 commit a890253
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/otaclient/app/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class BaseConfig(_InternalSettings):
"otaclient": INFO,
"otaclient_api": INFO,
"otaclient_common": INFO,
"otaproxy": INFO,
}
LOG_FORMAT = (
"[%(asctime)s][%(levelname)s]-%(name)s:%(funcName)s:%(lineno)d,%(message)s"
Expand Down
2 changes: 1 addition & 1 deletion src/otaclient/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from otaclient import __version__
from otaclient.app.configs import config as cfg
from otaclient.app.configs import ecu_info, server_cfg
from otaclient.app.log_setting import configure_logging
from otaclient.log_setting import configure_logging
from otaclient.app.ota_client_stub import OTAClientServiceStub
from otaclient_api.v2 import otaclient_v2_pb2_grpc as v2_grpc
from otaclient_api.v2.api_stub import OtaClientServiceV2
Expand Down
2 changes: 1 addition & 1 deletion src/otaclient/app/ota_client_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from ota_proxy import OTAProxyContextProto
from ota_proxy import config as local_otaproxy_cfg
from ota_proxy import subprocess_otaproxy_launcher
from otaclient.app import log_setting
from otaclient import log_setting
from otaclient.configs.ecu_info import ECUContact
from otaclient_api.v2 import types as api_types
from otaclient_api.v2.api_caller import ECUNoResponse, OTAClientCall
Expand Down
44 changes: 18 additions & 26 deletions src/otaclient/app/log_setting.py → src/otaclient/log_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# 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.
"""Configure the logging for otaclient."""


from __future__ import annotations
Expand All @@ -24,10 +25,7 @@

import requests

import otaclient

from .configs import config as cfg
from .configs import ecu_info, proxy_info
from otaclient.app.configs import config as cfg, ecu_info, proxy_info


class _LogTeeHandler(logging.Handler):
Expand Down Expand Up @@ -69,36 +67,30 @@ def _thread_exit():
atexit.register(_thread_exit)


def configure_logging():
def configure_logging() -> None:
"""Configure logging with http handler."""
# configure the root logger
# ------ suppress logging from non-first-party modules ------ #
# NOTE: force to reload the basicConfig, this is for overriding setting
# when launching subprocess.
# NOTE: for the root logger, set to CRITICAL to filter away logs from other
# external modules unless reached CRITICAL level.
logging.basicConfig(level=logging.CRITICAL, format=cfg.LOG_FORMAT, force=True)
# NOTE: set the <loglevel> to the otaclient package root logger
_otaclient_logger = logging.getLogger(otaclient.__name__)
_otaclient_logger.setLevel(cfg.DEFAULT_LOG_LEVEL)

# configure each sub loggers
for _module_name, _log_level in cfg.LOG_LEVEL_TABLE.items():
_logger = logging.getLogger(_module_name)
_logger.setLevel(_log_level)

if iot_logger_url := proxy_info.logging_server:
iot_logger_url = f"{str(iot_logger_url).strip('/')}/"
# ------ configure each sub loggers and attach ota logging handler ------ #
log_upload_handler = None
if logging_upload_endpoint := proxy_info.logging_server:
logging_upload_endpoint = f"{str(logging_upload_endpoint).strip('/')}/"

ch = _LogTeeHandler()
log_upload_handler = _LogTeeHandler()
fmt = logging.Formatter(fmt=cfg.LOG_FORMAT)
ch.setFormatter(fmt)
log_upload_handler.setFormatter(fmt)

# star the logging thread
log_upload_endpoint = urljoin(iot_logger_url, ecu_info.ecu_id)
ch.start_upload_thread(log_upload_endpoint)

# NOTE: "otaclient" logger will be the root logger for all loggers name
# starts with "otaclient.", and the settings will affect its child loggers.
# For example, settings for "otaclient" logger will also be effective to
# "otaclient.app.*" logger and "ota_proxy.*" logger.
_otaclient_logger.addHandler(ch)
log_upload_endpoint = urljoin(logging_upload_endpoint, ecu_info.ecu_id)
log_upload_handler.start_upload_thread(log_upload_endpoint)

for logger_name, loglevel in cfg.LOG_LEVEL_TABLE.items():
_logger = logging.getLogger(logger_name)
_logger.setLevel(loglevel)
if log_upload_handler:
_logger.addHandler(log_upload_handler)
2 changes: 1 addition & 1 deletion tests/test_otaclient/test_log_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import logging

from otaclient.app import log_setting
from otaclient import log_setting

MODULE = log_setting.__name__
logger = logging.getLogger(__name__)
Expand Down

0 comments on commit a890253

Please sign in to comment.