Skip to content

Commit

Permalink
enabled logging for core namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
cannatag committed May 6, 2015
1 parent 9e32bf1 commit f1d67ec
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 44 deletions.
64 changes: 32 additions & 32 deletions ldap3/core/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def __init__(self,
if client_strategy not in CLIENT_STRATEGIES:
self.last_error = 'unknown client connection strategy'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPUnknownStrategyError(self.last_error)

self.strategy_type = client_strategy
Expand All @@ -173,7 +173,7 @@ def __init__(self,
else:
self.last_error = 'unknown authentication method'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPUnknownAuthenticationMethodError(self.last_error)
self.version = version
self.auto_referrals = True if auto_referrals else False
Expand Down Expand Up @@ -244,7 +244,7 @@ def __init__(self,
else:
self.last_error = 'unknown strategy'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPUnknownStrategyError(self.last_error)

# map strategy functions to connection functions
Expand All @@ -257,7 +257,7 @@ def __init__(self,
if not self.strategy.no_real_dsa:
if self.auto_bind and self.auto_bind != AUTO_BIND_NONE:
if log_enabled(VERBOSITY_CHATTY):
log(VERBOSITY_CHATTY, 'performing auto_bind for %s', self)
log(VERBOSITY_CHATTY, 'performing automatic bind for <%s>', self)
self.open(read_server_info=False)
if self.auto_bind == AUTO_BIND_NO_TLS:
self.bind(read_server_info=True)
Expand All @@ -270,11 +270,11 @@ def __init__(self,
if not self.bound:
self.last_error = 'automatic bind not successful' + (' - ' + self.last_error if self.last_error else '')
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPBindError(self.last_error)

if log_enabled(VERBOSITY_CHATTY):
log(VERBOSITY_CHATTY, 'instantiated Connection: %r', self)
log(VERBOSITY_CHATTY, 'instantiated Connection: <%r>', self)

def __str__(self):
s = [
Expand Down Expand Up @@ -367,7 +367,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):

if exc_type is not None:
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', exc_type, self)
log(VERBOSITY_SEVERE, '%s for <%s>', exc_type, self)
return False # re-raise LDAPExceptionError

def bind(self,
Expand All @@ -390,7 +390,7 @@ def bind(self,
self._bind_controls = controls
self.bound = True
if log_enabled(VERBOSITY_CHATTY):
log(VERBOSITY_CHATTY, 'deferring bind for %s', self)
log(VERBOSITY_CHATTY, 'deferring bind for <%s>', self)
else:
self._deferred_bind = False
self._bind_controls = None
Expand All @@ -408,20 +408,20 @@ def bind(self,
else:
self.last_error = 'requested SASL mechanism not supported'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPSASLMechanismNotSupportedError(self.last_error)
elif self.authentication == NTLM:
if self.user and self.password:
response = self.do_ntlm_bind(controls)
else: # user or password missing
self.last_error = 'NTLM needs domain\\username and a password'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPUnknownAuthenticationMethodError(self.last_error)
else:
self.last_error = 'unknown authentication method'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPUnknownAuthenticationMethodError(self.last_error)

if not self.strategy.sync and self.authentication not in (SASL, NTLM): # get response if async except for SASL and NTLM that return the bind result even for async
Expand All @@ -435,7 +435,7 @@ def bind(self,
else:
self.last_error = 'unknown authentication method'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPUnknownAuthenticationMethodError(self.last_error)

if result is None:
Expand Down Expand Up @@ -526,7 +526,7 @@ def search(self,

if isinstance(paged_size, int):
if log_enabled(VERBOSITY_CHATTY):
log(VERBOSITY_CHATTY, 'performing paged search for %d items with cookie %s for %s', paged_size, paged_cookie, self)
log(VERBOSITY_CHATTY, 'performing paged search for %d items with cookie %s for <%s>', paged_size, paged_cookie, self)
real_search_control_value = RealSearchControlValue()
real_search_control_value['size'] = Size(paged_size)
real_search_control_value['cookie'] = Cookie(paged_cookie) if paged_cookie else Cookie('')
Expand Down Expand Up @@ -614,7 +614,7 @@ def add(self,
if not attributes[object_class_attr_name]:
self.last_error = 'ObjectClass attribute is mandatory'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPObjectClassError(self.last_error)

request = add_operation(dn, attributes, self.server.schema if self.server else None)
Expand Down Expand Up @@ -645,7 +645,7 @@ def delete(self,
if self.read_only:
self.last_error = 'connection is read-only'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPConnectionIsReadOnlyError(self.last_error)

request = delete_operation(dn)
Expand Down Expand Up @@ -681,31 +681,31 @@ def modify(self,
if self.read_only:
self.last_error = 'connection is read-only'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPConnectionIsReadOnlyError(self.last_error)

if not isinstance(changes, dict):
self.last_error = 'changes must be a dictionary'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPChangesError(self.last_error)

if not changes:
self.last_error = 'no changes in modify request'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPChangesError(self.last_error)

for change in changes:
if len(changes[change]) != 2:
self.last_error = 'malformed change'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPChangesError(self.last_error)
elif changes[change][0] not in [MODIFY_ADD, MODIFY_DELETE, MODIFY_REPLACE, MODIFY_INCREMENT, 0, 1, 2, 3]:
self.last_error = 'unknown change type'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPChangesError(self.last_error)

request = modify_operation(dn, changes, self.server.schema if self.server else None)
Expand Down Expand Up @@ -740,13 +740,13 @@ def modify_dn(self,
if self.read_only:
self.last_error = 'connection is read-only'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPConnectionIsReadOnlyError(self.last_error)

if new_superior and not dn.startswith(relative_dn): # as per RFC4511 (4.9)
self.last_error = 'DN cannot change while performing moving'
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', self.last_error, self)
log(VERBOSITY_SEVERE, '%s for <%s>', self.last_error, self)
raise LDAPChangesError(self.last_error)

request = modify_dn_operation(dn, relative_dn, delete_old_dn, new_superior)
Expand Down Expand Up @@ -797,7 +797,7 @@ def extended(self,
Performs an extended operation
"""
if log_enabled(VERBOSITY_NORMAL):
log(VERBOSITY_NORMAL, 'start EXTENDED operation <via> %s', self)
log(VERBOSITY_NORMAL, 'start EXTENDED operation via <%s>', self)

with self.lock:
self._fire_deferred()
Expand Down Expand Up @@ -829,7 +829,7 @@ def start_tls(self, read_server_info=True): # as per RFC4511. Removal of TLS is
self.tls_started = True
return_value = True
if log_enabled(VERBOSITY_CHATTY):
log(VERBOSITY_CHATTY, 'deferring START TLS for %s', self)
log(VERBOSITY_CHATTY, 'deferring START TLS for <%s>', self)
else:
self._deferred_start_tls = False
if self.server.tls.start_tls(self) and self.strategy.sync: # for async connections _start_tls is run by the strategy
Expand Down Expand Up @@ -924,7 +924,7 @@ def refresh_server_info(self):
with self.lock:
if not self.closed:
if log_enabled(VERBOSITY_CHATTY):
log(VERBOSITY_CHATTY, 'refreshing server info for %s', self)
log(VERBOSITY_CHATTY, 'refreshing server info for <%s>', self)
previous_response = self.response
previous_result = self.result
previous_entries = self._entries
Expand All @@ -934,7 +934,7 @@ def refresh_server_info(self):
self._entries = previous_entries
else:
if log_enabled(VERBOSITY_CHATTY):
log(VERBOSITY_CHATTY, 'refreshing server info from pool for %s', self)
log(VERBOSITY_CHATTY, 'refreshing server info from pool for <%s>', self)
self.strategy.pool.get_info_from_server()

def response_to_ldif(self,
Expand All @@ -958,7 +958,7 @@ def response_to_ldif(self,
stream.write(prepare_for_stream(header + line_separator + line_separator))
stream.write(prepare_for_stream(ldif_output + line_separator + line_separator))
if log_enabled(VERBOSITY_CHATTY):
log(VERBOSITY_CHATTY, 'building LDIF output %s for %s', ldif_output, self)
log(VERBOSITY_CHATTY, 'building LDIF output <%s> for <%s>', ldif_output, self)
return ldif_output

return None
Expand Down Expand Up @@ -993,7 +993,7 @@ def response_to_json(self,
json_output = json.dumps(json_dict, ensure_ascii=True, sort_keys=sort, indent=indent, check_circular=True, default=format_json, separators=(',', ': '))

if log_enabled(VERBOSITY_CHATTY):
log(VERBOSITY_CHATTY, 'building JSON output %s for %s', json_output, self)
log(VERBOSITY_CHATTY, 'building JSON output <%s> for <%s>', json_output, self)
if stream:
stream.write(json_output)

Expand All @@ -1010,7 +1010,7 @@ def response_to_file(self,
target = open(target, 'w+')

if log_enabled(VERBOSITY_CHATTY):
log(VERBOSITY_CHATTY, 'writing response to file for %s', self)
log(VERBOSITY_CHATTY, 'writing response to file for <%s>', self)

target.writelines(self.response_to_json(raw=raw, indent=indent, sort=sort))
target.close()
Expand All @@ -1021,7 +1021,7 @@ def _fire_deferred(self):
self._executing_deferred = True

if log_enabled(VERBOSITY_NORMAL):
log(VERBOSITY_NORMAL, 'executing deferred (open: %s, start_tls: %s, bind: %s) for %s', self._deferred_open, self._deferred_start_tls, self._deferred_bind, self)
log(VERBOSITY_NORMAL, 'executing deferred (open: %s, start_tls: %s, bind: %s) for <%s>', self._deferred_open, self._deferred_start_tls, self._deferred_bind, self)
try:
if self._deferred_open:
self.open(read_server_info=False)
Expand All @@ -1032,7 +1032,7 @@ def _fire_deferred(self):
self.refresh_server_info()
except LDAPExceptionError as e:
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, '%s for %s', e, self)
log(VERBOSITY_SEVERE, '%s for <%s>', e, self)
raise # re-raise LDAPExceptionError
finally:
self._executing_deferred = False
Expand Down Expand Up @@ -1089,7 +1089,7 @@ def _get_entries(self, search_response):
break
else:
if log_enabled(VERBOSITY_SEVERE):
log(VERBOSITY_SEVERE, 'attribute set not found for %s in %s', resp_attr_set, self)
log(VERBOSITY_SEVERE, 'attribute set not found for %s in <%s>', resp_attr_set, self)
raise LDAPObjectError('attribute set not found for ' + str(resp_attr_set))

return entries

0 comments on commit f1d67ec

Please sign in to comment.