Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging filename, level and format #12

Merged
merged 8 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
jesusbv marked this conversation as resolved.
Show resolved Hide resolved
def setup_adapter(config: Config):
log_to_file = logging.FileHandler(CSP_LOG_FILEPATH)
csp_log = logging.getLogger(LOGGER_NAME)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should either be csp_log = log or log = logging.getLogger(LOGGER_NAME) on line 37 should be removed. Why create the module variable at the top level when it is not being used?

csp_log.addHandler(log_to_file)
logging_level = config.get('logging', {}).get('level', 'INFO')
jesusbv marked this conversation as resolved.
Show resolved Hide resolved
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:
jesusbv marked this conversation as resolved.
Show resolved Hide resolved
level: 'WARN'
smarlowucf marked this conversation as resolved.
Show resolved Hide resolved
version: 1.1.1
41 changes: 40 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,37 @@ 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')
Loading