From c1f9e57c68e7d77967bf94400afb228d72819eb7 Mon Sep 17 00:00:00 2001 From: Ross Brattain Date: Wed, 21 Jun 2017 21:51:49 -0700 Subject: switch logging to proper usage The logging methods do string interpolation themselves The first arg to logging.debug() is formally defined to be a message format string. From the reference: https://docs.python.org/2/library/logging.html#logging.Logger.debug Logger.debug(msg, *args, **kwargs) Logs a message with level DEBUG on this logger. The msg is the message format string, and the args are the arguments which are merged into msg using the string formatting operator. (Note that this means that you can use keywords in the format string, together with a single dictionary argument.) There are two keyword arguments in kwargs which are inspected: exc_info which, if it does not evaluate as false, causes exception information to be added to the logging message. If an exception tuple (in the format returned by sys.exc_info()) is provided, it is used; otherwise, sys.exc_info() is called to get the exception information The reason logging does string interpolation it self is to implement deferred interpolation. String interpolation involves evaluating arguments, so it can introduce significant computation. The logging module tries to be smart about deferring interpolation until the last possible moment. The logging methods check isEnabledFor for the log level and won't interpolate if the level is not enabled. https://github.com/python/cpython/blob/2.7/Lib/logging/__init__.py#L1178 def warning(self, msg, *args, **kwargs): if self.isEnabledFor(WARNING): self._log(WARNING, msg, args, **kwargs) logging actually waits to interpolate the string in LogRecord.getMessage() https://github.com/python/cpython/blob/2.7/Lib/logging/__init__.py#L328 if self.args: msg = msg % self.args Change-Id: I0090dcbc408200b6f2471748eae0c5a763da2e37 Signed-off-by: Ross Brattain --- anteater/main.py | 2 +- anteater/src/get_lists.py | 12 ++++-------- anteater/src/patch_scan.py | 36 ++++++++++++++---------------------- anteater/src/project_scan.py | 42 ++++++++++++++++-------------------------- 4 files changed, 35 insertions(+), 57 deletions(-) (limited to 'anteater') diff --git a/anteater/main.py b/anteater/main.py index 3a23ceb..a8bd034 100644 --- a/anteater/main.py +++ b/anteater/main.py @@ -44,7 +44,7 @@ def check_dir(): """ Creates a directory for scan reports """ try: os.makedirs(reports_dir) - logger.info('Creating reports directory: {0}'.format(reports_dir)) + logger.info('Creating reports directory: %s', reports_dir) except OSError as e: if not os.path.isdir(reports_dir): logger.error(e) diff --git a/anteater/src/get_lists.py b/anteater/src/get_lists.py index e27335a..024cef3 100644 --- a/anteater/src/get_lists.py +++ b/anteater/src/get_lists.py @@ -87,8 +87,7 @@ class GetLists(object): binary_hash = (yl['binaries'][project][file_name]) return binary_hash except KeyError: - logger.info('No checksum entries found for {0}'. - format(file_name)) + logger.info('No checksum entries found for %s', file_name) binary_hash = 'null' return binary_hash @@ -102,11 +101,9 @@ class GetLists(object): logger.error('Key Error processing file_names list values') try: project_list = set((yl['file_audits'][project]['file_names'])) - logger.info('file_names waivers found for {0}'. - format(project)) + logger.info('file_names waivers found for %s', project) except KeyError: - logger.info('No file_names waivers found for {0}'. - format(project)) + logger.info('No file_names waivers found for %s', project) file_names_re = re.compile("|".join(default_list), flags=re.IGNORECASE) @@ -129,8 +126,7 @@ class GetLists(object): try: project_list = set((yl['file_audits'][project]['file_contents'])) except KeyError: - logger.info('No file_contents waivers found for {0}'. - format(project)) + logger.info('No file_contents waivers found for %s', project) file_contents_re = re.compile("|".join(default_list), flags=re.IGNORECASE) diff --git a/anteater/src/patch_scan.py b/anteater/src/patch_scan.py index 71604a8..fffb246 100644 --- a/anteater/src/patch_scan.py +++ b/anteater/src/patch_scan.py @@ -84,13 +84,13 @@ def scan_patch(project, patch_file, binary_list, file_audit_list, buf = afile.read() hasher.update(buf) if hasher.hexdigest() in binary_hash: - logger.info('Found matching file hash for file: {0}'. - format(patch_file)) + logger.info('Found matching file hash for file: %s', + patch_file) else: - logger.error('Non Whitelisted Binary file: {0}'. - format(patch_file)) - logger.error('Submit patch with the following hash: {0}'. - format(hasher.hexdigest())) + logger.error('Non Whitelisted Binary file: %s', + patch_file) + logger.error('Submit patch with the following hash: %s', + hasher.hexdigest()) failure = True with open(reports_dir + "binaries-" + project + ".log", "a") \ as gate_report: @@ -101,10 +101,8 @@ def scan_patch(project, patch_file, binary_list, file_audit_list, if file_audit_list.search(patch_file) and not \ file_audit_project_list.search(patch_file): match = file_audit_list.search(patch_file) - logger.error('Blacklisted file: {0}'. - format(patch_file)) - logger.error('Matched String: {0}'. - format(match.group())) + logger.error('Blacklisted file: %s', patch_file) + logger.error('Matched String: %s', match.group()) failure = True with open(reports_dir + "file-names_" + project + ".log", "a") \ as gate_report: @@ -121,12 +119,9 @@ def scan_patch(project, patch_file, binary_list, file_audit_list, if file_content_list.search(line) and not \ file_content_project_list.search(line): match = file_content_list.search(line) - logger.error('File contains violation: {0}'. - format(patch_file)) - logger.error('Flagged Content: {0}'. - format(line.rstrip())) - logger.error('Matched String: {0}'. - format(match.group())) + logger.error('File contains violation: %s', patch_file) + logger.error('Flagged Content: %s', line.rstrip()) + logger.error('Matched String: %s', match.group()) failure = True with open(reports_dir + "contents_" + project + ".log", "a") as gate_report: @@ -153,14 +148,11 @@ def licence_check(project, licence_ext, # of a decision made at 2017 plugfest to limit searches to # just these two strings. if re.search("copyright", content, re.IGNORECASE): - logger.info('Contains needed Licence string: {0}'. - format(patch_file)) + logger.info('Contains needed Licence string: %s', patch_file) elif re.search("spdx", content, re.IGNORECASE): - logger.info('Contains needed Licence string: {0}'. - format(patch_file)) + logger.info('Contains needed Licence string: %s', patch_file) else: - logger.error('Licence header missing in file: {0}'. - format(patch_file)) + logger.error('Licence header missing in file: %s', patch_file) failure = True with open(reports_dir + "licence-" + project + ".log", "a") \ as gate_report: diff --git a/anteater/src/project_scan.py b/anteater/src/project_scan.py index 647c256..0b5c80b 100644 --- a/anteater/src/project_scan.py +++ b/anteater/src/project_scan.py @@ -76,10 +76,8 @@ def scan_file(project_dir, project, binary_list, file_audit_list, if file_audit_list.search(full_path) and not \ file_audit_project_list.search(full_path): match = file_audit_list.search(full_path) - logger.error('Blacklisted filename: {0}'. - format(full_path)) - logger.error('Matched String: {0}'. - format(match.group())) + logger.error('Blacklisted filename: %s', full_path) + logger.error('Matched String: %s', match.group()) with open(reports_dir + "file-names_" + project + ".log", "a") as gate_report: gate_report. \ @@ -97,12 +95,9 @@ def scan_file(project_dir, project, binary_list, file_audit_list, if file_content_list.search(line) and not \ project_content_list.search(line): match = file_content_list.search(line) - logger.error('File contains violation: {0}'. - format(full_path)) - logger.error('Flagged Content: {0}'. - format(line.rstrip())) - logger.error('Matched String: {0}'. - format(match.group())) + logger.error('File contains violation: %s', full_path) + logger.error('Flagged Content: %s', line.rstrip()) + logger.error('Matched String: %s', match.group()) with open(reports_dir + "contents-" + project + ".log", "a") \ as gate_report: @@ -124,13 +119,13 @@ def scan_file(project_dir, project, binary_list, file_audit_list, buf = afile.read() hasher.update(buf) if hasher.hexdigest() in binary_hash: - logger.info('Found matching file hash for file: {0}'. - format(full_path)) + logger.info('Found matching file hash for file: %s', + full_path) else: - logger.error('Non Whitelisted Binary file: {0}'. - format(full_path)) - logger.error('Please submit patch with this hash: {0}'. - format(hasher.hexdigest())) + logger.error('Non Whitelisted Binary file: %s', + full_path) + logger.error('Please submit patch with this hash: %s', + hasher.hexdigest()) with open(reports_dir + "binaries-" + project + ".log", "a") \ as gate_report: @@ -140,11 +135,9 @@ def scan_file(project_dir, project, binary_list, file_audit_list, def licence_root_check(project_dir, project): if os.path.isfile(project_dir + '/LICENSE'): - logger.info('LICENSE file present in: {0}'. - format(project_dir)) + logger.info('LICENSE file present in: %s', project_dir) else: - logger.error('LICENSE file missing in: {0}'. - format(project_dir)) + logger.error('LICENSE file missing in: %s', project_dir) with open(reports_dir + "licence-" + project + ".log", "a") \ as gate_report: @@ -167,14 +160,11 @@ def licence_check(licence_ext, licence_ignore, project, project_dir): # of a decision made at 2017 plugfest to limit searches to # just these two strings. if re.search("copyright", content, re.IGNORECASE): - logger.info('Licence string present: {0}'. - format(full_path)) + logger.info('Licence string present: %s', full_path) elif re.search("spdx", content, re.IGNORECASE): - logger.info('Licence string present: {0}'. - format(full_path)) + logger.info('Licence string present: %s', full_path) else: - logger.error('Licence header missing: {0}'. - format(full_path)) + logger.error('Licence header missing: %s', full_path) with open(reports_dir + "licence-" + project + ".log", "a") \ as gate_report: -- cgit 1.2.3-korg