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

added option to handle logging scheme externally #173

Merged
merged 1 commit into from
Feb 3, 2022
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
80 changes: 48 additions & 32 deletions meraki/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
API_KEY_ENVIRONMENT_VARIABLE, DEFAULT_BASE_URL, SINGLE_REQUEST_TIMEOUT, CERTIFICATE_PATH, REQUESTS_PROXY,
WAIT_ON_RATE_LIMIT, NGINX_429_RETRY_WAIT_TIME, ACTION_BATCH_RETRY_WAIT_TIME, RETRY_4XX_ERROR,
RETRY_4XX_ERROR_WAIT_TIME, MAXIMUM_RETRIES, OUTPUT_LOG, LOG_PATH, LOG_FILE_PREFIX, PRINT_TO_CONSOLE,
SUPPRESS_LOGGING, SIMULATE_API_CALLS, BE_GEO_ID, MERAKI_PYTHON_SDK_CALLER
SUPPRESS_LOGGING, SIMULATE_API_CALLS, BE_GEO_ID, MERAKI_PYTHON_SDK_CALLER, INHERIT_LOGGING_CONFIG
)

__version__ = '1.15.0'
Expand All @@ -48,20 +48,34 @@ class DashboardAPI(object):
- log_file_prefix (string): log file name appended with date and timestamp
- print_console (boolean): print logging output to console?
- suppress_logging (boolean): disable all logging? you're on your own then!
- inherit_logging_config (boolean): Inherits you're own logging scheme
- simulate (boolean): simulate POST/PUT/DELETE calls to prevent changes?
- be_geo_id (string): optional partner identifier for API usage tracking; can also be set as an environment variable BE_GEO_ID
- caller (string): optional identifier for API usage tracking; can also be set as an environment variable MERAKI_PYTHON_SDK_CALLER
- use_iterator_for_get_pages (boolean): list* methods will return an iterator with each object instead of a complete list with all items
"""

def __init__(self, api_key=None, base_url=DEFAULT_BASE_URL, single_request_timeout=SINGLE_REQUEST_TIMEOUT,
certificate_path=CERTIFICATE_PATH, requests_proxy=REQUESTS_PROXY,
wait_on_rate_limit=WAIT_ON_RATE_LIMIT, nginx_429_retry_wait_time=NGINX_429_RETRY_WAIT_TIME,
action_batch_retry_wait_time=ACTION_BATCH_RETRY_WAIT_TIME, retry_4xx_error=RETRY_4XX_ERROR,
retry_4xx_error_wait_time=RETRY_4XX_ERROR_WAIT_TIME, maximum_retries=MAXIMUM_RETRIES,
output_log=OUTPUT_LOG, log_path=LOG_PATH, log_file_prefix=LOG_FILE_PREFIX,
print_console=PRINT_TO_CONSOLE, suppress_logging=SUPPRESS_LOGGING, simulate=SIMULATE_API_CALLS,
be_geo_id=BE_GEO_ID, caller=MERAKI_PYTHON_SDK_CALLER, use_iterator_for_get_pages=False):
def __init__(self, api_key=None,
base_url=DEFAULT_BASE_URL,
single_request_timeout=SINGLE_REQUEST_TIMEOUT,
certificate_path=CERTIFICATE_PATH,
requests_proxy=REQUESTS_PROXY,
wait_on_rate_limit=WAIT_ON_RATE_LIMIT,
nginx_429_retry_wait_time=NGINX_429_RETRY_WAIT_TIME,
action_batch_retry_wait_time=ACTION_BATCH_RETRY_WAIT_TIME,
retry_4xx_error=RETRY_4XX_ERROR,
retry_4xx_error_wait_time=RETRY_4XX_ERROR_WAIT_TIME,
maximum_retries=MAXIMUM_RETRIES,
output_log=OUTPUT_LOG, log_path=LOG_PATH,
log_file_prefix=LOG_FILE_PREFIX,
print_console=PRINT_TO_CONSOLE,
suppress_logging=SUPPRESS_LOGGING,
simulate=SIMULATE_API_CALLS,
be_geo_id=BE_GEO_ID,
caller=MERAKI_PYTHON_SDK_CALLER,
inherit_logging_config= INHERIT_LOGGING_CONFIG,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unnecessary space, will remove.

use_iterator_for_get_pages=False):

# Check API key
api_key = api_key or os.environ.get(API_KEY_ENVIRONMENT_VARIABLE)
if not api_key:
Expand All @@ -76,31 +90,33 @@ def __init__(self, api_key=None, base_url=DEFAULT_BASE_URL, single_request_timeo
# Configure logging
if not suppress_logging:
self._logger = logging.getLogger(__name__)
self._logger.setLevel(logging.DEBUG)

formatter = logging.Formatter(
fmt='%(asctime)s %(name)12s: %(levelname)8s > %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
handler_console = logging.StreamHandler()
handler_console.setFormatter(formatter)

if output_log:
if log_path and log_path[-1] != '/':
log_path += '/'
self._log_file = f'{log_path}{log_file_prefix}_log__{datetime.now():%Y-%m-%d_%H-%M-%S}.log'
handler_log = logging.FileHandler(
filename=self._log_file
)
handler_log.setFormatter(formatter)

if not inherit_logging_config:
self._logger.setLevel(logging.DEBUG)

if output_log and not self._logger.hasHandlers():
self._logger.addHandler(handler_log)
if print_console:
handler_console.setLevel(logging.INFO)
formatter = logging.Formatter(
fmt='%(asctime)s %(name)12s: %(levelname)8s > %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
handler_console = logging.StreamHandler()
handler_console.setFormatter(formatter)

if output_log:
if log_path and log_path[-1] != '/':
log_path += '/'
self._log_file = f'{log_path}{log_file_prefix}_log__{datetime.now():%Y-%m-%d_%H-%M-%S}.log'
handler_log = logging.FileHandler(
filename=self._log_file
)
handler_log.setFormatter(formatter)

if output_log and not self._logger.hasHandlers():
self._logger.addHandler(handler_log)
if print_console:
handler_console.setLevel(logging.INFO)
self._logger.addHandler(handler_console)
elif print_console and not self._logger.hasHandlers():
self._logger.addHandler(handler_console)
elif print_console and not self._logger.hasHandlers():
self._logger.addHandler(handler_console)
else:
self._logger = None

Expand Down
49 changes: 27 additions & 22 deletions meraki/aio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
SINGLE_REQUEST_TIMEOUT,
SUPPRESS_LOGGING,
WAIT_ON_RATE_LIMIT,
INHERIT_LOGGING_CONFIG
)
from .api.appliance import AsyncAppliance
from .api.camera import AsyncCamera
Expand Down Expand Up @@ -60,6 +61,7 @@ class AsyncDashboardAPI:
- log_file_prefix (string): log file name appended with date and timestamp
- print_console (boolean): print logging output to console?
- suppress_logging (boolean): disable all logging? you're on your own then!
- inherit_logging_config (boolean): Inherits you're own logging scheme
Copy link
Collaborator

Choose a reason for hiding this comment

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

typo: should be your instead of you're. will fix

- simulate (boolean): simulate POST/PUT/DELETE calls to prevent changes?
- maximum_concurrent_requests (integer): number of concurrent API requests for asynchronous class
- be_geo_id (string): optional partner identifier for API usage tracking; can also be set as an environment variable BE_GEO_ID
Expand Down Expand Up @@ -90,6 +92,7 @@ def __init__(
be_geo_id=BE_GEO_ID,
caller=MERAKI_PYTHON_SDK_CALLER,
use_iterator_for_get_pages=False,
inherit_logging_config=INHERIT_LOGGING_CONFIG
Copy link
Collaborator

Choose a reason for hiding this comment

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

This can be at the same order in the kwarg list in both init files. Will fix.

):
# Check API key
api_key = api_key or os.environ.get(API_KEY_ENVIRONMENT_VARIABLE)
Expand All @@ -105,29 +108,31 @@ def __init__(
# Configure logging
if not suppress_logging:
self._logger = logging.getLogger(__name__)
self._logger.setLevel(logging.DEBUG)

formatter = logging.Formatter(
fmt="%(asctime)s %(name)12s: %(levelname)8s > %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
handler_console = logging.StreamHandler()
handler_console.setFormatter(formatter)

if output_log:
if log_path and log_path[-1] != "/":
log_path += "/"
self._log_file = f"{log_path}{log_file_prefix}_log__{datetime.now():%Y-%m-%d_%H-%M-%S}.log"
handler_log = logging.FileHandler(filename=self._log_file)
handler_log.setFormatter(formatter)

if output_log and not self._logger.hasHandlers():
self._logger.addHandler(handler_log)
if print_console:
handler_console.setLevel(logging.INFO)

if not inherit_logging_config:
self._logger.setLevel(logging.DEBUG)

formatter = logging.Formatter(
fmt="%(asctime)s %(name)12s: %(levelname)8s > %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
handler_console = logging.StreamHandler()
handler_console.setFormatter(formatter)

if output_log:
if log_path and log_path[-1] != "/":
log_path += "/"
self._log_file = f"{log_path}{log_file_prefix}_log__{datetime.now():%Y-%m-%d_%H-%M-%S}.log"
handler_log = logging.FileHandler(filename=self._log_file)
handler_log.setFormatter(formatter)

if output_log and not self._logger.hasHandlers():
self._logger.addHandler(handler_log)
if print_console:
handler_console.setLevel(logging.INFO)
self._logger.addHandler(handler_console)
elif print_console and not self._logger.hasHandlers():
self._logger.addHandler(handler_console)
elif print_console and not self._logger.hasHandlers():
self._logger.addHandler(handler_console)
else:
self._logger = None

Expand Down
5 changes: 5 additions & 0 deletions meraki/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
# Disable all logging? You're on your own then!
SUPPRESS_LOGGING = False

# Some use cases might integrate the library where a logging scheme is already
# defined, so no handlers, formatters etc, are needed, just the logger instance
# itself
INHERIT_LOGGING_CONFIG = False

# Simulate POST/PUT/DELETE calls to prevent changes?
SIMULATE_API_CALLS = False

Expand Down