Skip to content

Commit 12979a2

Browse files
GameMaster47ricardo.marques
andauthored
added option to handle logging scheme externally (meraki#173)
Co-authored-by: ricardo.marques <ricardo.marques@hoistgroup.com>
1 parent aef5e6f commit 12979a2

3 files changed

Lines changed: 80 additions & 54 deletions

File tree

meraki/__init__.py

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
API_KEY_ENVIRONMENT_VARIABLE, DEFAULT_BASE_URL, SINGLE_REQUEST_TIMEOUT, CERTIFICATE_PATH, REQUESTS_PROXY,
2323
WAIT_ON_RATE_LIMIT, NGINX_429_RETRY_WAIT_TIME, ACTION_BATCH_RETRY_WAIT_TIME, RETRY_4XX_ERROR,
2424
RETRY_4XX_ERROR_WAIT_TIME, MAXIMUM_RETRIES, OUTPUT_LOG, LOG_PATH, LOG_FILE_PREFIX, PRINT_TO_CONSOLE,
25-
SUPPRESS_LOGGING, SIMULATE_API_CALLS, BE_GEO_ID, MERAKI_PYTHON_SDK_CALLER
25+
SUPPRESS_LOGGING, SIMULATE_API_CALLS, BE_GEO_ID, MERAKI_PYTHON_SDK_CALLER, INHERIT_LOGGING_CONFIG
2626
)
2727

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

57-
def __init__(self, api_key=None, base_url=DEFAULT_BASE_URL, single_request_timeout=SINGLE_REQUEST_TIMEOUT,
58-
certificate_path=CERTIFICATE_PATH, requests_proxy=REQUESTS_PROXY,
59-
wait_on_rate_limit=WAIT_ON_RATE_LIMIT, nginx_429_retry_wait_time=NGINX_429_RETRY_WAIT_TIME,
60-
action_batch_retry_wait_time=ACTION_BATCH_RETRY_WAIT_TIME, retry_4xx_error=RETRY_4XX_ERROR,
61-
retry_4xx_error_wait_time=RETRY_4XX_ERROR_WAIT_TIME, maximum_retries=MAXIMUM_RETRIES,
62-
output_log=OUTPUT_LOG, log_path=LOG_PATH, log_file_prefix=LOG_FILE_PREFIX,
63-
print_console=PRINT_TO_CONSOLE, suppress_logging=SUPPRESS_LOGGING, simulate=SIMULATE_API_CALLS,
64-
be_geo_id=BE_GEO_ID, caller=MERAKI_PYTHON_SDK_CALLER, use_iterator_for_get_pages=False):
58+
def __init__(self, api_key=None,
59+
base_url=DEFAULT_BASE_URL,
60+
single_request_timeout=SINGLE_REQUEST_TIMEOUT,
61+
certificate_path=CERTIFICATE_PATH,
62+
requests_proxy=REQUESTS_PROXY,
63+
wait_on_rate_limit=WAIT_ON_RATE_LIMIT,
64+
nginx_429_retry_wait_time=NGINX_429_RETRY_WAIT_TIME,
65+
action_batch_retry_wait_time=ACTION_BATCH_RETRY_WAIT_TIME,
66+
retry_4xx_error=RETRY_4XX_ERROR,
67+
retry_4xx_error_wait_time=RETRY_4XX_ERROR_WAIT_TIME,
68+
maximum_retries=MAXIMUM_RETRIES,
69+
output_log=OUTPUT_LOG, log_path=LOG_PATH,
70+
log_file_prefix=LOG_FILE_PREFIX,
71+
print_console=PRINT_TO_CONSOLE,
72+
suppress_logging=SUPPRESS_LOGGING,
73+
simulate=SIMULATE_API_CALLS,
74+
be_geo_id=BE_GEO_ID,
75+
caller=MERAKI_PYTHON_SDK_CALLER,
76+
inherit_logging_config= INHERIT_LOGGING_CONFIG,
77+
use_iterator_for_get_pages=False):
78+
6579
# Check API key
6680
api_key = api_key or os.environ.get(API_KEY_ENVIRONMENT_VARIABLE)
6781
if not api_key:
@@ -76,31 +90,33 @@ def __init__(self, api_key=None, base_url=DEFAULT_BASE_URL, single_request_timeo
7690
# Configure logging
7791
if not suppress_logging:
7892
self._logger = logging.getLogger(__name__)
79-
self._logger.setLevel(logging.DEBUG)
80-
81-
formatter = logging.Formatter(
82-
fmt='%(asctime)s %(name)12s: %(levelname)8s > %(message)s',
83-
datefmt='%Y-%m-%d %H:%M:%S'
84-
)
85-
handler_console = logging.StreamHandler()
86-
handler_console.setFormatter(formatter)
87-
88-
if output_log:
89-
if log_path and log_path[-1] != '/':
90-
log_path += '/'
91-
self._log_file = f'{log_path}{log_file_prefix}_log__{datetime.now():%Y-%m-%d_%H-%M-%S}.log'
92-
handler_log = logging.FileHandler(
93-
filename=self._log_file
94-
)
95-
handler_log.setFormatter(formatter)
93+
94+
if not inherit_logging_config:
95+
self._logger.setLevel(logging.DEBUG)
9696

97-
if output_log and not self._logger.hasHandlers():
98-
self._logger.addHandler(handler_log)
99-
if print_console:
100-
handler_console.setLevel(logging.INFO)
97+
formatter = logging.Formatter(
98+
fmt='%(asctime)s %(name)12s: %(levelname)8s > %(message)s',
99+
datefmt='%Y-%m-%d %H:%M:%S'
100+
)
101+
handler_console = logging.StreamHandler()
102+
handler_console.setFormatter(formatter)
103+
104+
if output_log:
105+
if log_path and log_path[-1] != '/':
106+
log_path += '/'
107+
self._log_file = f'{log_path}{log_file_prefix}_log__{datetime.now():%Y-%m-%d_%H-%M-%S}.log'
108+
handler_log = logging.FileHandler(
109+
filename=self._log_file
110+
)
111+
handler_log.setFormatter(formatter)
112+
113+
if output_log and not self._logger.hasHandlers():
114+
self._logger.addHandler(handler_log)
115+
if print_console:
116+
handler_console.setLevel(logging.INFO)
117+
self._logger.addHandler(handler_console)
118+
elif print_console and not self._logger.hasHandlers():
101119
self._logger.addHandler(handler_console)
102-
elif print_console and not self._logger.hasHandlers():
103-
self._logger.addHandler(handler_console)
104120
else:
105121
self._logger = None
106122

meraki/aio/__init__.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
SINGLE_REQUEST_TIMEOUT,
2424
SUPPRESS_LOGGING,
2525
WAIT_ON_RATE_LIMIT,
26+
INHERIT_LOGGING_CONFIG
2627
)
2728
from .api.appliance import AsyncAppliance
2829
from .api.camera import AsyncCamera
@@ -60,6 +61,7 @@ class AsyncDashboardAPI:
6061
- log_file_prefix (string): log file name appended with date and timestamp
6162
- print_console (boolean): print logging output to console?
6263
- suppress_logging (boolean): disable all logging? you're on your own then!
64+
- inherit_logging_config (boolean): Inherits you're own logging scheme
6365
- simulate (boolean): simulate POST/PUT/DELETE calls to prevent changes?
6466
- maximum_concurrent_requests (integer): number of concurrent API requests for asynchronous class
6567
- be_geo_id (string): optional partner identifier for API usage tracking; can also be set as an environment variable BE_GEO_ID
@@ -90,6 +92,7 @@ def __init__(
9092
be_geo_id=BE_GEO_ID,
9193
caller=MERAKI_PYTHON_SDK_CALLER,
9294
use_iterator_for_get_pages=False,
95+
inherit_logging_config=INHERIT_LOGGING_CONFIG
9396
):
9497
# Check API key
9598
api_key = api_key or os.environ.get(API_KEY_ENVIRONMENT_VARIABLE)
@@ -105,29 +108,31 @@ def __init__(
105108
# Configure logging
106109
if not suppress_logging:
107110
self._logger = logging.getLogger(__name__)
108-
self._logger.setLevel(logging.DEBUG)
109-
110-
formatter = logging.Formatter(
111-
fmt="%(asctime)s %(name)12s: %(levelname)8s > %(message)s",
112-
datefmt="%Y-%m-%d %H:%M:%S",
113-
)
114-
handler_console = logging.StreamHandler()
115-
handler_console.setFormatter(formatter)
116-
117-
if output_log:
118-
if log_path and log_path[-1] != "/":
119-
log_path += "/"
120-
self._log_file = f"{log_path}{log_file_prefix}_log__{datetime.now():%Y-%m-%d_%H-%M-%S}.log"
121-
handler_log = logging.FileHandler(filename=self._log_file)
122-
handler_log.setFormatter(formatter)
123-
124-
if output_log and not self._logger.hasHandlers():
125-
self._logger.addHandler(handler_log)
126-
if print_console:
127-
handler_console.setLevel(logging.INFO)
111+
112+
if not inherit_logging_config:
113+
self._logger.setLevel(logging.DEBUG)
114+
115+
formatter = logging.Formatter(
116+
fmt="%(asctime)s %(name)12s: %(levelname)8s > %(message)s",
117+
datefmt="%Y-%m-%d %H:%M:%S",
118+
)
119+
handler_console = logging.StreamHandler()
120+
handler_console.setFormatter(formatter)
121+
122+
if output_log:
123+
if log_path and log_path[-1] != "/":
124+
log_path += "/"
125+
self._log_file = f"{log_path}{log_file_prefix}_log__{datetime.now():%Y-%m-%d_%H-%M-%S}.log"
126+
handler_log = logging.FileHandler(filename=self._log_file)
127+
handler_log.setFormatter(formatter)
128+
129+
if output_log and not self._logger.hasHandlers():
130+
self._logger.addHandler(handler_log)
131+
if print_console:
132+
handler_console.setLevel(logging.INFO)
133+
self._logger.addHandler(handler_console)
134+
elif print_console and not self._logger.hasHandlers():
128135
self._logger.addHandler(handler_console)
129-
elif print_console and not self._logger.hasHandlers():
130-
self._logger.addHandler(handler_console)
131136
else:
132137
self._logger = None
133138

meraki/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
# Disable all logging? You're on your own then!
4949
SUPPRESS_LOGGING = False
5050

51+
# Some use cases might integrate the library where a logging scheme is already
52+
# defined, so no handlers, formatters etc, are needed, just the logger instance
53+
# itself
54+
INHERIT_LOGGING_CONFIG = False
55+
5156
# Simulate POST/PUT/DELETE calls to prevent changes?
5257
SIMULATE_API_CALLS = False
5358

0 commit comments

Comments
 (0)