Skip to content

Commit

Permalink
Add logging filename, level and format
Browse files Browse the repository at this point in the history
Set the logging on setup_adapter and tests
  • Loading branch information
jesusbv committed Aug 7, 2023
1 parent 8ad9f2a commit 0d8fd73
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
15 changes: 13 additions & 2 deletions csp_billing_adapter_local/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@

from csp_billing_adapter.config import Config

log = logging.getLogger('CSPBillingAdapter')

ADAPTER_DATA_DIR = '/var/lib/csp-billing-adapter'
CACHE_FILE = 'cache.json'
CSP_CONFIG_FILE = 'csp-config.json'
CSP_LOG_FILEPATH = '/var/log/csp_billing_adapter.log'
LOGGER_NAME = 'CSPBillingAdapter'

log = logging.getLogger(LOGGER_NAME)


def get_local_path(filename):
Expand All @@ -44,6 +46,15 @@ def get_local_path(filename):
return local_storage_path


@csp_billing_adapter.hookimpl(trylast=True)
def setup_adapter(config: Config):
log_to_file = logging.FileHandler(CSP_LOG_FILEPATH)
csp_log = logging.getLogger(LOGGER_NAME)
csp_log.addHandler(log_to_file)
logging_level = config.get('logging', {}).get('level', 'INFO')
csp_log.setLevel(logging_level)


@csp_billing_adapter.hookimpl(trylast=True)
def save_cache(config: Config, cache: dict):
"""Save specified content as new local cache contents."""
Expand Down
4 changes: 3 additions & 1 deletion tests/data/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ usage_metrics:
min: 1001
min_consumption: 5
usage_aggregation: average
version: 1.1.1
logging:
level: 'WARN'
version: 1.1.1
42 changes: 41 additions & 1 deletion tests/unit/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
test_plugin.py is part of csp-billing-adapter-local and provides units tests
for the local plugin functions.
"""
import logging

from pathlib import Path
from tempfile import NamedTemporaryFile
from unittest.mock import patch
Expand All @@ -36,7 +38,8 @@
update_cache,
update_csp_config,
save_cache,
save_csp_config
save_csp_config,
setup_adapter
)

config_file = 'tests/data/config.yaml'
Expand All @@ -45,6 +48,8 @@
config_file,
pm.hook
)
with open('tests/data/good/csp_config.json', 'r', encoding='utf-8') as f:
content = f.read()


def test_local_get_cache():
Expand Down Expand Up @@ -295,3 +300,38 @@ def test_local_csp_config_save():
)

assert get_csp_config(config=local_config) == test_data2


@patch('csp_billing_adapter_local.plugin.logging.getLogger')
@patch('csp_billing_adapter_local.plugin.logging.FileHandler')
def test_local_csp_setup_adapter_log_with_config_settings(
mock_logging_file_handler, mock_logging_get_logger,
):
file_handler = logging.FileHandler('foo')
log = logging.getLogger('csp_test')
mock_logging_get_logger.return_value = log
mock_logging_file_handler.return_value = file_handler

setup_adapter(config=local_config)

log.addHandler.assert_called_with(file_handler)
log.setLevel.assert_called_with('WARN')
mock_logging_file_handler.assert_called_with(
'/var/log/csp_billing_adapter.log'
)
mock_logging_get_logger.assert_called_with('CSPBillingAdapter')


@patch('csp_billing_adapter_local.plugin.logging.Logger')
@patch('csp_billing_adapter_local.plugin.logging.getLogger')
@patch('csp_billing_adapter_local.plugin.logging.FileHandler')
def test_local_csp_setup_adapter_log_without_config_settings(
mock_logging_file_handler, mock_logging_get_logger,
mock_logger
):
log = logging.getLogger('csp_test')

setup_adapter(config=Config({}))

log.setLevel.assert_called_with('INFO')

0 comments on commit 0d8fd73

Please sign in to comment.