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 all 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
13 changes: 11 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,13 @@ def get_local_path(filename):
return local_storage_path


@csp_billing_adapter.hookimpl
def setup_adapter(config: Config):
log_to_file = logging.FileHandler(CSP_LOG_FILEPATH)
log.addHandler(log_to_file)
log.info(f'Logger file handler set to {CSP_LOG_FILEPATH}')


@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
smarlowucf marked this conversation as resolved.
Show resolved Hide resolved
version: 1.1.1
27 changes: 26 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 Down Expand Up @@ -295,3 +298,25 @@ def test_local_csp_config_save():
)

assert get_csp_config(config=local_config) == test_data2


@patch('csp_billing_adapter_local.plugin.logging.Logger.info')
@patch('csp_billing_adapter_local.plugin.logging.Logger.addHandler')
@patch('csp_billing_adapter_local.plugin.logging.FileHandler')
def test_local_csp_setup_adapter_log_with_config_settings(
mock_logging_file_handler, mock_logger_add_handler,
mock_logging_info
):
file_handler = logging.FileHandler('foo')
log = logging.getLogger('CSPBillingAdapter')
mock_logging_file_handler.return_value = file_handler

setup_adapter(config=local_config)

log.addHandler.assert_called_with(file_handler)
mock_logging_file_handler.assert_called_with(
'/var/log/csp_billing_adapter.log'
)
mock_logging_info.assert_called_with(
'Logger file handler set to /var/log/csp_billing_adapter.log'
)
Loading