Skip to content

Commit d023774

Browse files
committed
Add logger to all modules
1 parent a5ac35f commit d023774

File tree

6 files changed

+40
-22
lines changed

6 files changed

+40
-22
lines changed

src/cisco_gnmi/builder.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
from .util import gen_target_netloc, get_cert_from_target, get_cn_from_cert
3232

3333

34+
LOGGER = logging.getLogger(__name__)
35+
36+
3437
class ClientBuilder(object):
3538
"""Builder for the creation of a gNMI client.
3639
Supports construction of base Client and XRClient.
@@ -134,8 +137,8 @@ def set_os(self, name=None):
134137
if name not in self.os_class_map.keys():
135138
raise Exception("OS not supported!")
136139
else:
140+
LOGGER.debug("Using %s wrapper.", name or "Client")
137141
self.__client_class = self.os_class_map[name]
138-
logging.debug("Using %s wrapper.", name or "Client")
139142
return self
140143

141144
def set_secure(
@@ -257,7 +260,7 @@ def set_channel_option(self, name, value):
257260
found_index = index
258261
break
259262
if found_index is not None:
260-
logging.warning("Found existing channel option %s, overwriting!", name)
263+
LOGGER.warning("Found existing channel option %s, overwriting!", name)
261264
self.__channel_options[found_index] = new_option
262265
else:
263266
self.__channel_options.append(new_option)
@@ -279,26 +282,26 @@ def construct(self):
279282
self.__root_certificates, self.__private_key, self.__certificate_chain
280283
)
281284
if self.__username and self.__password:
285+
LOGGER.debug("Using username/password call authentication.")
282286
channel_metadata_creds = grpc.metadata_call_credentials(
283287
CiscoAuthPlugin(self.__username, self.__password)
284288
)
285-
logging.debug("Using username/password call authentication.")
286289
if channel_ssl_creds and channel_metadata_creds:
290+
LOGGER.debug("Using SSL/metadata authentication composite credentials.")
287291
channel_creds = grpc.composite_channel_credentials(
288292
channel_ssl_creds, channel_metadata_creds
289293
)
290-
logging.debug("Using SSL/metadata authentication composite credentials.")
291294
else:
295+
LOGGER.debug("Using SSL credentials, no metadata authentication.")
292296
channel_creds = channel_ssl_creds
293-
logging.debug("Using SSL credentials, no metadata authentication.")
294297
if self.__ssl_target_name_override is not False:
295298
if self.__ssl_target_name_override is None:
296299
if not self.__root_certificates:
297300
raise Exception("Deriving override requires root certificate!")
298301
self.__ssl_target_name_override = get_cn_from_cert(
299302
self.__root_certificates
300303
)
301-
logging.warning(
304+
LOGGER.warning(
302305
"Overriding SSL option from certificate could increase MITM susceptibility!"
303306
)
304307
self.set_channel_option(

src/cisco_gnmi/client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from . import util
3232

3333

34-
logger = logging.getLogger(__name__)
34+
LOGGER = logging.getLogger(__name__)
3535

3636

3737
class Client(object):
@@ -112,7 +112,7 @@ def capabilities(self):
112112
proto.gnmi_pb2.CapabilityResponse
113113
"""
114114
message = proto.gnmi_pb2.CapabilityRequest()
115-
logger.debug(message)
115+
LOGGER.debug(message)
116116
response = self.service.Capabilities(message)
117117
return response
118118

@@ -168,7 +168,7 @@ def get(
168168
if extension:
169169
request.extension = extension
170170

171-
logger.debug(str(request))
171+
LOGGER.debug(str(request))
172172

173173
get_response = self.service.Get(request)
174174
return get_response
@@ -215,7 +215,7 @@ def set(
215215
if extensions:
216216
request.extension.extend(extensions)
217217

218-
logger.debug(str(request))
218+
LOGGER.debug(str(request))
219219

220220
response = self.service.Set(request)
221221
return response
@@ -255,7 +255,7 @@ def validate_request(request):
255255
if extensions:
256256
subscribe_request.extensions.extend(extensions)
257257

258-
logger.debug(str(subscribe_request))
258+
LOGGER.debug(str(subscribe_request))
259259

260260
return subscribe_request
261261

src/cisco_gnmi/nx.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
from .client import Client, proto, util
3131

3232

33+
LOGGER = logging.getLogger(__name__)
34+
35+
3336
class NXClient(Client):
3437
"""NX-OS-specific wrapper for gNMI functionality.
3538

src/cisco_gnmi/util.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
from urlparse import urlparse
3838

3939

40+
LOGGER = logging.getLogger(__name__)
41+
42+
4043
def gen_target_netloc(target, netloc_prefix="//", default_port=9339):
4144
"""Parses and validates a supplied target URL for gRPC calls.
4245
Uses urllib to parse the netloc property from the URL.
@@ -51,11 +54,11 @@ def gen_target_netloc(target, netloc_prefix="//", default_port=9339):
5154
if not parsed_target.netloc:
5255
raise ValueError("Unable to parse netloc from target URL %s!" % target)
5356
if parsed_target.scheme:
54-
logging.debug("Scheme identified in target, ignoring and using netloc.")
57+
LOGGER.debug("Scheme identified in target, ignoring and using netloc.")
5558
target_netloc = parsed_target
5659
if parsed_target.port is None:
5760
ported_target = "%s:%i" % (parsed_target.hostname, default_port)
58-
logging.debug("No target port detected, reassembled to %s.", ported_target)
61+
LOGGER.debug("No target port detected, reassembled to %s.", ported_target)
5962
target_netloc = gen_target_netloc(ported_target)
6063
return target_netloc
6164

@@ -120,11 +123,11 @@ def get_cn_from_cert(cert_pem):
120123
cert_cns = cert_parsed.subject.get_attributes_for_oid(x509.oid.NameOID.COMMON_NAME)
121124
if len(cert_cns) > 0:
122125
if len(cert_cns) > 1:
123-
logging.warning(
126+
LOGGER.warning(
124127
"Multiple CNs found for certificate, defaulting to the first one."
125128
)
126129
cert_cn = cert_cns[0].value
127-
logging.debug("Using %s as certificate CN.", cert_cn)
130+
LOGGER.debug("Using %s as certificate CN.", cert_cn)
128131
else:
129-
logging.warning("No CN found for certificate.")
132+
LOGGER.warning("No CN found for certificate.")
130133
return cert_cn

src/cisco_gnmi/xe.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from .client import Client, proto, util
3131

3232

33-
logger = logging.getLogger(__name__)
33+
LOGGER = logging.getLogger(__name__)
3434

3535

3636
class XEClient(Client):
@@ -111,7 +111,13 @@ def delete_xpaths(self, xpaths, prefix=None):
111111
paths.append(self.parse_xpath_to_gnmi_path(xpath))
112112
return self.set(deletes=paths)
113113

114-
def set_json(self, update_json_configs=None, replace_json_configs=None, ietf=True, prefix=None):
114+
def set_json(
115+
self,
116+
update_json_configs=None,
117+
replace_json_configs=None,
118+
ietf=True,
119+
prefix=None,
120+
):
115121
"""A convenience wrapper for set() which assumes JSON payloads and constructs desired messages.
116122
All parameters are optional, but at least one must be present.
117123
@@ -136,14 +142,14 @@ def set_json(self, update_json_configs=None, replace_json_configs=None, ietf=Tru
136142

137143
def check_configs(name, configs):
138144
if isinstance(configs, string_types):
139-
logger.debug("Handling %s as JSON string.", name)
145+
LOGGER.debug("Handling %s as JSON string.", name)
140146
try:
141147
configs = json.loads(configs)
142148
except:
143149
raise Exception("{name} is invalid JSON!".format(name=name))
144150
configs = [configs]
145151
elif isinstance(configs, dict):
146-
logger.debug("Handling %s as already serialized JSON object.", name)
152+
LOGGER.debug("Handling %s as already serialized JSON object.", name)
147153
configs = [configs]
148154
elif not isinstance(configs, (list, set)):
149155
raise Exception(

src/cisco_gnmi/xr.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
from .client import Client, proto, util
3131

3232

33+
LOGGER = logging.getLogger(__name__)
34+
35+
3336
class XRClient(Client):
3437
"""IOS XR-specific wrapper for gNMI functionality.
3538
@@ -130,14 +133,14 @@ def set_json(self, update_json_configs=None, replace_json_configs=None, ietf=Tru
130133

131134
def check_configs(name, configs):
132135
if isinstance(name, string_types):
133-
logging.debug("Handling %s as JSON string.", name)
136+
LOGGER.debug("Handling %s as JSON string.", name)
134137
try:
135138
configs = json.loads(configs)
136139
except:
137140
raise Exception("{name} is invalid JSON!".format(name=name))
138141
configs = [configs]
139142
elif isinstance(name, dict):
140-
logging.debug("Handling %s as already serialized JSON object.", name)
143+
LOGGER.debug("Handling %s as already serialized JSON object.", name)
141144
configs = [configs]
142145
elif not isinstance(configs, (list, set)):
143146
raise Exception(

0 commit comments

Comments
 (0)